-module(pt_339).
-include("common.hrl").
-export([
        read/2,
        write/2
    ]).

read(33901, _) ->
    {ok, []};
read(33902, Bin0) ->
    <<RedEnvelopesId:64, _Bin1/binary>> = Bin0, 
    {ok, [RedEnvelopesId]};
read(33903, Bin0) ->
    <<PacketId:32, Bin1/binary>> = Bin0, 
    {Greet, _Bin2} = pt:read_string(Bin1), 
    {ok, [PacketId, Greet]};
read(33905, Bin0) ->
    <<GoodsId:64, Bin1/binary>> = Bin0, 
    <<GtypeId:32, _Bin2/binary>> = Bin1, 
    {ok, [GoodsId, GtypeId]};
read(33906, _) ->
    {ok, []};
read(33907, _) ->
    {ok, []};
read(33908, _) ->
    {ok, []};
read(33909, Bin0) ->
    <<RedEnvelopesId:64, _Bin1/binary>> = Bin0, 
    {ok, [RedEnvelopesId]};
read(_Cmd, _R) -> {error, no_match}.

write (33900,[
    Errcode
] = Src_Data) ->
    Data = <<
        Errcode:32
    >>,
    {ok, pt:encode(33900, Data, Src_Data)};

write (33901,[
    RedPacketList
] = Src_Data) ->
    BinList_RedPacketList = [
        item_to_bin_0(RedPacketList_Item) || RedPacketList_Item <- RedPacketList
    ], 

    RedPacketList_Len = length(RedPacketList), 
    Bin_RedPacketList = list_to_binary(BinList_RedPacketList),

    Data = <<
        RedPacketList_Len:16, Bin_RedPacketList/binary
    >>,
    {ok, pt:encode(33901, Data, Src_Data)};

write (33902,[
    RedEnvelopesId,
    PacketId,
    RoleId,
    RoleName,
    Career,
    Sex,
    Turn,
    Picture,
    PictureVer,
    ProfilePhotoId,
    DressId,
    Status,
    ReceiveMoney,
    TotalNum,
    RecipientsNum,
    Money,
    Type,
    Greet,
    RecipientList
] = Src_Data) ->
    Bin_RoleName = pt:write_string(RoleName), 

    Bin_Picture = pt:write_string(Picture), 

    Bin_Greet = pt:write_string(Greet), 

    BinList_RecipientList = [
        item_to_bin_1(RecipientList_Item) || RecipientList_Item <- RecipientList
    ], 

    RecipientList_Len = length(RecipientList), 
    Bin_RecipientList = list_to_binary(BinList_RecipientList),

    Data = <<
        RedEnvelopesId:64,
        PacketId:32,
        RoleId:64,
        Bin_RoleName/binary,
        Career:8,
        Sex:8,
        Turn:8,
        Bin_Picture/binary,
        PictureVer:32,
        ProfilePhotoId:32,
        DressId:32,
        Status:8,
        ReceiveMoney:32,
        TotalNum:16,
        RecipientsNum:16,
        Money:32,
        Type:8,
        Bin_Greet/binary,
        RecipientList_Len:16, Bin_RecipientList/binary
    >>,
    {ok, pt:encode(33902, Data, Src_Data)};

write (33903,[
    Errcode
] = Src_Data) ->
    Data = <<
        Errcode:32
    >>,
    {ok, pt:encode(33903, Data, Src_Data)};

write (33904,[
    RedEnvelopesList
] = Src_Data) ->
    BinList_RedEnvelopesList = [
        item_to_bin_2(RedEnvelopesList_Item) || RedEnvelopesList_Item <- RedEnvelopesList
    ], 

    RedEnvelopesList_Len = length(RedEnvelopesList), 
    Bin_RedEnvelopesList = list_to_binary(BinList_RedEnvelopesList),

    Data = <<
        RedEnvelopesList_Len:16, Bin_RedEnvelopesList/binary
    >>,
    {ok, pt:encode(33904, Data, Src_Data)};

write (33905,[
    Errcode
] = Src_Data) ->
    Data = <<
        Errcode:32
    >>,
    {ok, pt:encode(33905, Data, Src_Data)};

write (33906,[
    Num
] = Src_Data) ->
    Data = <<
        Num:16
    >>,
    {ok, pt:encode(33906, Data, Src_Data)};

write (33907,[
    GoldNum,
    BgoldNum
] = Src_Data) ->
    Data = <<
        GoldNum:32,
        BgoldNum:32
    >>,
    {ok, pt:encode(33907, Data, Src_Data)};

write (33908,[
    Times,
    Limit,
    Gold,
    Bgold
] = Src_Data) ->
    Data = <<
        Times:16,
        Limit:16,
        Gold:32,
        Bgold:32
    >>,
    {ok, pt:encode(33908, Data, Src_Data)};

write (33909,[
    Errcode
] = Src_Data) ->
    Data = <<
        Errcode:32
    >>,
    {ok, pt:encode(33909, Data, Src_Data)};

write(_Cmd, _R) -> ?DEBUG("no_match:~p,~p~n", [_Cmd, _R]), {ok, pt:pack(0, <<>>)}.


item_to_bin_0 ({
    Id,
    PacketId,
    RoleId,
    RoleName,
    Career,
    Sex,
    Turn,
    Picture,
    PictureVer,
    ProfilePhotoId,
    DressId,
    Type,
    Status,
    ReceiveStatus,
    HasLook,
    ReceiveMoney
}) ->
    Bin_RoleName = pt:write_string(RoleName), 

    Bin_Picture = pt:write_string(Picture), 

    Data = <<
        Id:64,
        PacketId:32,
        RoleId:64,
        Bin_RoleName/binary,
        Career:8,
        Sex:8,
        Turn:8,
        Bin_Picture/binary,
        PictureVer:32,
        ProfilePhotoId:32,
        DressId:32,
        Type:8,
        Status:8,
        ReceiveStatus:8,
        HasLook:8,
        ReceiveMoney:32
    >>,
    Data.
item_to_bin_1 ({
    RoleId,
    RoleName,
    Career,
    Sex,
    Turn,
    Picture,
    PictureVer,
    ReceiveMoney,
    Time
}) ->
    Bin_RoleName = pt:write_string(RoleName), 

    Bin_Picture = pt:write_string(Picture), 

    Data = <<
        RoleId:64,
        Bin_RoleName/binary,
        Career:8,
        Sex:8,
        Turn:8,
        Bin_Picture/binary,
        PictureVer:32,
        ReceiveMoney:32,
        Time:32
    >>,
    Data.
item_to_bin_2 ({
    Id,
    PacketId,
    RoleId,
    RoleName,
    Career,
    Sex,
    Turn,
    Picture,
    PictureVer,
    ProfilePhotoId,
    DressId,
    Type,
    Status,
    ReceiveStatus,
    HasLook,
    ReceiveMoney
}) ->
    Bin_RoleName = pt:write_string(RoleName), 

    Bin_Picture = pt:write_string(Picture), 

    Data = <<
        Id:64,
        PacketId:32,
        RoleId:64,
        Bin_RoleName/binary,
        Career:8,
        Sex:8,
        Turn:8,
        Bin_Picture/binary,
        PictureVer:32,
        ProfilePhotoId:32,
        DressId:32,
        Type:8,
        Status:8,
        ReceiveStatus:8,
        HasLook:8,
        ReceiveMoney:32
    >>,
    Data.
