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

read(33501, Bin0) ->
    <<SubType:16, _Bin1/binary>> = Bin0, 
    {ok, [SubType]};
read(33502, Bin0) ->
    <<SubType:16, _Bin1/binary>> = Bin0, 
    {ok, [SubType]};
read(33503, Bin0) ->
    <<SubType:16, Bin1/binary>> = Bin0, 
    <<Id:8, Bin2/binary>> = Bin1, 
    <<Num:16, _Bin3/binary>> = Bin2, 
    {ok, [SubType, Id, Num]};
read(33504, Bin0) ->
    <<SubType:16, Bin1/binary>> = Bin0, 
    <<Type:8, Bin2/binary>> = Bin1, 
    <<Skip:8, _Bin3/binary>> = Bin2, 
    {ok, [SubType, Type, Skip]};
read(33505, Bin0) ->
    <<SubType:16, _Bin1/binary>> = Bin0, 
    {ok, [SubType]};
read(_Cmd, _R) -> {error, no_match}.

write (33501,[
    Errcode,
    SubType,
    Score,
    Point,
    FreeTime,
    PoolList
] = Src_Data) ->
    BinList_PoolList = [
        item_to_bin_0(PoolList_Item) || PoolList_Item <- PoolList
    ], 

    PoolList_Len = length(PoolList), 
    Bin_PoolList = list_to_binary(BinList_PoolList),

    Data = <<
        Errcode:32,
        SubType:16,
        Score:16,
        Point:16,
        FreeTime:8,
        PoolList_Len:16, Bin_PoolList/binary
    >>,
    {ok, pt:encode(33501, Data, Src_Data)};

write (33502,[
    Errcode,
    SubType,
    CurScore,
    SwapList
] = Src_Data) ->
    BinList_SwapList = [
        item_to_bin_1(SwapList_Item) || SwapList_Item <- SwapList
    ], 

    SwapList_Len = length(SwapList), 
    Bin_SwapList = list_to_binary(BinList_SwapList),

    Data = <<
        Errcode:32,
        SubType:16,
        CurScore:16,
        SwapList_Len:16, Bin_SwapList/binary
    >>,
    {ok, pt:encode(33502, Data, Src_Data)};

write (33503,[
    Errcode,
    SubType,
    Id,
    LeftTimes,
    CurScore
] = Src_Data) ->
    Data = <<
        Errcode:32,
        SubType:16,
        Id:8,
        LeftTimes:16,
        CurScore:16
    >>,
    {ok, pt:encode(33503, Data, Src_Data)};

write (33504,[
    Errcode,
    SubType,
    Score,
    Point,
    FreeTime,
    IdList
] = Src_Data) ->
    BinList_IdList = [
        item_to_bin_2(IdList_Item) || IdList_Item <- IdList
    ], 

    IdList_Len = length(IdList), 
    Bin_IdList = list_to_binary(BinList_IdList),

    Data = <<
        Errcode:32,
        SubType:16,
        Score:16,
        Point:16,
        FreeTime:8,
        IdList_Len:16, Bin_IdList/binary
    >>,
    {ok, pt:encode(33504, Data, Src_Data)};

write (33505,[
    SubType,
    List
] = Src_Data) ->
    BinList_List = [
        item_to_bin_3(List_Item) || List_Item <- List
    ], 

    List_Len = length(List), 
    Bin_List = list_to_binary(BinList_List),

    Data = <<
        SubType:16,
        List_Len:16, Bin_List/binary
    >>,
    {ok, pt:encode(33505, Data, Src_Data)};

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


item_to_bin_0 ({
    Id,
    Reward,
    Tag
}) ->
    Bin_Reward = pt:write_string(Reward), 

    Data = <<
        Id:8,
        Bin_Reward/binary,
        Tag:8
    >>,
    Data.
item_to_bin_1 ({
    Id,
    Reward,
    LeftTimes,
    Score
}) ->
    Bin_Reward = pt:write_string(Reward), 

    Data = <<
        Id:8,
        Bin_Reward/binary,
        LeftTimes:16,
        Score:16
    >>,
    Data.
item_to_bin_2 (
    Id
) ->
    Data = <<
        Id:8
    >>,
    Data.
item_to_bin_3 ({
    RoleName,
    GoodsId,
    Num
}) ->
    Bin_RoleName = pt:write_string(RoleName), 

    Data = <<
        Bin_RoleName/binary,
        GoodsId:32,
        Num:8
    >>,
    Data.
