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

read(33600, Bin0) ->
    <<SubType:16, _Bin1/binary>> = Bin0, 
    {ok, [SubType]};
read(33601, Bin0) ->
    <<SubType:16, Bin1/binary>> = Bin0, 
    <<OptionType:8, Bin2/binary>> = Bin1, 
    <<RewardId:16, _Bin3/binary>> = Bin2, 
    {ok, [SubType, OptionType, RewardId]};
read(33602, Bin0) ->
    <<SubType:16, _Bin1/binary>> = Bin0, 
    {ok, [SubType]};
read(33603, Bin0) ->
    <<SubType:16, _Bin1/binary>> = Bin0, 
    {ok, [SubType]};
read(33604, Bin0) ->
    <<SubType:16, Bin1/binary>> = Bin0, 
    FunArray0 = fun(<<RestBin0/binary>>) -> 
        <<RewardId:16, _Args01/binary>> = RestBin0, 
        {RewardId,_Args01}
        end,
    {SelectRewardList, _Bin2} = pt:read_array(FunArray0, Bin1),

    {ok, [SubType, SelectRewardList]};
read(33605, Bin0) ->
    <<SubType:16, _Bin1/binary>> = Bin0, 
    {ok, [SubType]};
read(33611, Bin0) ->
    <<BaseType:16, Bin1/binary>> = Bin0, 
    <<SubType:16, _Bin2/binary>> = Bin1, 
    {ok, [BaseType, SubType]};
read(33612, Bin0) ->
    <<BaseType:16, Bin1/binary>> = Bin0, 
    <<SubType:16, Bin2/binary>> = Bin1, 
    <<Grade:16, _Bin3/binary>> = Bin2, 
    {ok, [BaseType, SubType, Grade]};
read(33613, Bin0) ->
    <<BaseType:16, Bin1/binary>> = Bin0, 
    <<SubType:16, _Bin2/binary>> = Bin1, 
    {ok, [BaseType, SubType]};
read(_Cmd, _R) -> {error, no_match}.

write (33600,[
    SubType,
    RewardList,
    SelectRewardList,
    CfgRewardList,
    CostGoodsTypeId,
    CostNum,
    LeftResetTimes,
    IsEnsure
] = Src_Data) ->
    BinList_RewardList = [
        item_to_bin_0(RewardList_Item) || RewardList_Item <- RewardList
    ], 

    RewardList_Len = length(RewardList), 
    Bin_RewardList = list_to_binary(BinList_RewardList),

    BinList_SelectRewardList = [
        item_to_bin_1(SelectRewardList_Item) || SelectRewardList_Item <- SelectRewardList
    ], 

    SelectRewardList_Len = length(SelectRewardList), 
    Bin_SelectRewardList = list_to_binary(BinList_SelectRewardList),

    BinList_CfgRewardList = [
        item_to_bin_2(CfgRewardList_Item) || CfgRewardList_Item <- CfgRewardList
    ], 

    CfgRewardList_Len = length(CfgRewardList), 
    Bin_CfgRewardList = list_to_binary(BinList_CfgRewardList),

    Data = <<
        SubType:16,
        RewardList_Len:16, Bin_RewardList/binary,
        SelectRewardList_Len:16, Bin_SelectRewardList/binary,
        CfgRewardList_Len:16, Bin_CfgRewardList/binary,
        CostGoodsTypeId:32,
        CostNum:16,
        LeftResetTimes:8,
        IsEnsure:8
    >>,
    {ok, pt:encode(33600, Data, Src_Data)};

write (33601,[
    SubType,
    SelectRewardList
] = Src_Data) ->
    BinList_SelectRewardList = [
        item_to_bin_3(SelectRewardList_Item) || SelectRewardList_Item <- SelectRewardList
    ], 

    SelectRewardList_Len = length(SelectRewardList), 
    Bin_SelectRewardList = list_to_binary(BinList_SelectRewardList),

    Data = <<
        SubType:16,
        SelectRewardList_Len:16, Bin_SelectRewardList/binary
    >>,
    {ok, pt:encode(33601, Data, Src_Data)};

write (33602,[
    Res,
    SubType,
    LeftResetTimes
] = Src_Data) ->
    Data = <<
        Res:32,
        SubType:16,
        LeftResetTimes:8
    >>,
    {ok, pt:encode(33602, Data, Src_Data)};

write (33603,[
    SubType,
    Rare,
    Rewards,
    CostGoodsTypeId,
    CostNum,
    IsEnsure
] = Src_Data) ->
    Bin_Rewards = pt:write_string(Rewards), 

    Data = <<
        SubType:16,
        Rare:8,
        Bin_Rewards/binary,
        CostGoodsTypeId:32,
        CostNum:16,
        IsEnsure:8
    >>,
    {ok, pt:encode(33603, Data, Src_Data)};

write (33604,[
    SubType,
    SelectRewardList
] = Src_Data) ->
    BinList_SelectRewardList = [
        item_to_bin_4(SelectRewardList_Item) || SelectRewardList_Item <- SelectRewardList
    ], 

    SelectRewardList_Len = length(SelectRewardList), 
    Bin_SelectRewardList = list_to_binary(BinList_SelectRewardList),

    Data = <<
        SubType:16,
        SelectRewardList_Len:16, Bin_SelectRewardList/binary
    >>,
    {ok, pt:encode(33604, Data, Src_Data)};

write (33605,[
    SubType,
    Res
] = Src_Data) ->
    Data = <<
        SubType:16,
        Res:32
    >>,
    {ok, pt:encode(33605, Data, Src_Data)};

write (33611,[
    BaseType,
    SubType,
    RewardType,
    Status,
    TabList
] = Src_Data) ->
    BinList_TabList = [
        item_to_bin_5(TabList_Item) || TabList_Item <- TabList
    ], 

    TabList_Len = length(TabList), 
    Bin_TabList = list_to_binary(BinList_TabList),

    Data = <<
        BaseType:16,
        SubType:16,
        RewardType:8,
        Status:8,
        TabList_Len:16, Bin_TabList/binary
    >>,
    {ok, pt:encode(33611, Data, Src_Data)};

write (33612,[
    BaseType,
    SubType,
    Grade,
    Errcode
] = Src_Data) ->
    Data = <<
        BaseType:16,
        SubType:16,
        Grade:16,
        Errcode:32
    >>,
    {ok, pt:encode(33612, Data, Src_Data)};

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

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


item_to_bin_0 ({
    Rare,
    Rewards
}) ->
    Bin_Rewards = pt:write_string(Rewards), 

    Data = <<
        Rare:8,
        Bin_Rewards/binary
    >>,
    Data.
item_to_bin_1 ({
    Rare,
    Rewards
}) ->
    Bin_Rewards = pt:write_string(Rewards), 

    Data = <<
        Rare:8,
        Bin_Rewards/binary
    >>,
    Data.
item_to_bin_2 ({
    CfgRare,
    CfgRewards
}) ->
    Bin_CfgRewards = pt:write_string(CfgRewards), 

    Data = <<
        CfgRare:8,
        Bin_CfgRewards/binary
    >>,
    Data.
item_to_bin_3 ({
    Rare,
    Rewards
}) ->
    Bin_Rewards = pt:write_string(Rewards), 

    Data = <<
        Rare:8,
        Bin_Rewards/binary
    >>,
    Data.
item_to_bin_4 ({
    Rare,
    Rewards
}) ->
    Bin_Rewards = pt:write_string(Rewards), 

    Data = <<
        Rare:8,
        Bin_Rewards/binary
    >>,
    Data.
item_to_bin_5 ({
    Grade,
    Status
}) ->
    Data = <<
        Grade:16,
        Status:8
    >>,
    Data.
