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

read(22301, Bin0) ->
    <<ReceiveId:64, Bin1/binary>> = Bin0, 
    <<GoodsId:32, Bin2/binary>> = Bin1, 
    <<Num:8, Bin3/binary>> = Bin2, 
    <<Anonymous:8, _Bin4/binary>> = Bin3, 
    {ok, [ReceiveId, GoodsId, Num, Anonymous]};
read(22302, _) ->
    {ok, []};
read(22303, _) ->
    {ok, []};
read(22304, _) ->
    {ok, []};
read(22305, Bin0) ->
    <<RoleId:64, Bin1/binary>> = Bin0, 
    <<EggId:8, _Bin2/binary>> = Bin1, 
    {ok, [RoleId, EggId]};
read(22306, Bin0) ->
    <<RoleId:64, _Bin1/binary>> = Bin0, 
    {ok, [RoleId]};
read(22307, _) ->
    {ok, []};
read(22308, _) ->
    {ok, []};
read(22309, Bin0) ->
    <<Id:64, _Bin1/binary>> = Bin0, 
    {ok, [Id]};
read(_Cmd, _R) -> {error, no_match}.

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

write (22301,[
    Code,
    ReceiveId,
    WeekCharm,
    Intimacy
] = Src_Data) ->
    Data = <<
        Code:32,
        ReceiveId:64,
        WeekCharm:32,
        Intimacy:32
    >>,
    {ok, pt:encode(22301, Data, Src_Data)};

write (22302,[
    RecordList
] = Src_Data) ->
    BinList_RecordList = [
        item_to_bin_0(RecordList_Item) || RecordList_Item <- RecordList
    ], 

    RecordList_Len = length(RecordList), 
    Bin_RecordList = list_to_binary(BinList_RecordList),

    Data = <<
        RecordList_Len:16, Bin_RecordList/binary
    >>,
    {ok, pt:encode(22302, Data, Src_Data)};

write (22303,[
    FlowerNum,
    Charm,
    Fame
] = Src_Data) ->
    Data = <<
        FlowerNum:32,
        Charm:32,
        Fame:32
    >>,
    {ok, pt:encode(22303, Data, Src_Data)};

write (22304,[
    Code
] = Src_Data) ->
    Data = <<
        Code:8
    >>,
    {ok, pt:encode(22304, Data, Src_Data)};

write (22305,[
    Code,
    Charm,
    EggNum
] = Src_Data) ->
    Data = <<
        Code:8,
        Charm:32,
        EggNum:16
    >>,
    {ok, pt:encode(22305, Data, Src_Data)};

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

write (22307,[
    EggList
] = Src_Data) ->
    BinList_EggList = [
        item_to_bin_1(EggList_Item) || EggList_Item <- EggList
    ], 

    EggList_Len = length(EggList), 
    Bin_EggList = list_to_binary(BinList_EggList),

    Data = <<
        EggList_Len:16, Bin_EggList/binary
    >>,
    {ok, pt:encode(22307, Data, Src_Data)};

write (22308,[
    GoodsList
] = Src_Data) ->
    BinList_GoodsList = [
        item_to_bin_2(GoodsList_Item) || GoodsList_Item <- GoodsList
    ], 

    GoodsList_Len = length(GoodsList), 
    Bin_GoodsList = list_to_binary(BinList_GoodsList),

    Data = <<
        GoodsList_Len:16, Bin_GoodsList/binary
    >>,
    {ok, pt:encode(22308, Data, Src_Data)};

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

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


item_to_bin_0 ({
    Code,
    Id,
    GoodsId,
    Num,
    Anonymous,
    Time,
    Thank,
    Figure
}) ->
    Bin_Figure = pt:write_picture(Figure), 

    Data = <<
        Code:8,
        Id:64,
        GoodsId:32,
        Num:8,
        Anonymous:8,
        Time:32,
        Thank:8,
        Bin_Figure/binary
    >>,
    Data.
item_to_bin_1 ({
    EggId,
    EggNum,
    EggMax
}) ->
    Data = <<
        EggId:8,
        EggNum:16,
        EggMax:16
    >>,
    Data.
item_to_bin_2 ({
    GoodsId,
    Num
}) ->
    Data = <<
        GoodsId:32,
        Num:16
    >>,
    Data.
