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

read(41400, _) ->
    {ok, []};
read(41401, Bin0) ->
    <<Type:8, Bin1/binary>> = Bin0, 
    <<CostOpt:8, Bin2/binary>> = Bin1, 
    <<Time:32, _Bin3/binary>> = Bin2, 
    {ok, [Type, CostOpt, Time]};
read(41402, Bin0) ->
    <<Result:8, Bin1/binary>> = Bin0, 
    <<QualId:16, Bin2/binary>> = Bin1, 
    {Tokens, _Bin3} = pt:read_string(Bin2), 
    {ok, [Result, QualId, Tokens]};
read(41403, _) ->
    {ok, []};
read(41404, _) ->
    {ok, []};
read(41405, _) ->
    {ok, []};
read(41406, Bin0) ->
    <<Index:16, _Bin1/binary>> = Bin0, 
    {ok, [Index]};
read(41407, _) ->
    {ok, []};
read(41408, Bin0) ->
    <<Index:32, Bin1/binary>> = Bin0, 
    <<Num:16, _Bin2/binary>> = Bin1, 
    {ok, [Index, Num]};
read(41409, _) ->
    {ok, []};
read(41410, Bin0) ->
    <<Type:8, _Bin1/binary>> = Bin0, 
    {ok, [Type]};
read(41411, _) ->
    {ok, []};
read(_Cmd, _R) -> {error, no_match}.

write (41400,[
    Progress,
    Count,
    ResetCount,
    Reward,
    BoxIndex
] = Src_Data) ->
    BinList_Reward = [
        item_to_bin_0(Reward_Item) || Reward_Item <- Reward
    ], 

    Reward_Len = length(Reward), 
    Bin_Reward = list_to_binary(BinList_Reward),

    BinList_BoxIndex = [
        item_to_bin_2(BoxIndex_Item) || BoxIndex_Item <- BoxIndex
    ], 

    BoxIndex_Len = length(BoxIndex), 
    Bin_BoxIndex = list_to_binary(BinList_BoxIndex),

    Data = <<
        Progress:16,
        Count:16,
        ResetCount:16,
        Reward_Len:16, Bin_Reward/binary,
        BoxIndex_Len:16, Bin_BoxIndex/binary
    >>,
    {ok, pt:encode(41400, Data, Src_Data)};

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

write (41402,[
    Errcode,
    RewardList
] = Src_Data) ->
    BinList_RewardList = [
        item_to_bin_3(RewardList_Item) || RewardList_Item <- RewardList
    ], 

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

    Data = <<
        Errcode:32,
        RewardList_Len:16, Bin_RewardList/binary
    >>,
    {ok, pt:encode(41402, Data, Src_Data)};

write (41403,[
    Errcode,
    GoodsList
] = Src_Data) ->
    BinList_GoodsList = [
        item_to_bin_4(GoodsList_Item) || GoodsList_Item <- GoodsList
    ], 

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

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

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

write (41405,[
    ResetCount,
    ShopList,
    Time
] = Src_Data) ->
    BinList_ShopList = [
        item_to_bin_5(ShopList_Item) || ShopList_Item <- ShopList
    ], 

    ShopList_Len = length(ShopList), 
    Bin_ShopList = list_to_binary(BinList_ShopList),

    Data = <<
        ResetCount:16,
        ShopList_Len:16, Bin_ShopList/binary,
        Time:32
    >>,
    {ok, pt:encode(41405, Data, Src_Data)};

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

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

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

write (41409,[
    Errcode,
    Turn,
    Step,
    ResetCount,
    FreeCount,
    ShowReward,
    Times
] = Src_Data) ->
    BinList_ShowReward = [
        item_to_bin_6(ShowReward_Item) || ShowReward_Item <- ShowReward
    ], 

    ShowReward_Len = length(ShowReward), 
    Bin_ShowReward = list_to_binary(BinList_ShowReward),

    Data = <<
        Errcode:32,
        Turn:8,
        Step:16,
        ResetCount:8,
        FreeCount:8,
        ShowReward_Len:16, Bin_ShowReward/binary,
        Times:8
    >>,
    {ok, pt:encode(41409, Data, Src_Data)};

write (41410,[
    Res,
    Walk,
    NewStep
] = Src_Data) ->
    Data = <<
        Res:32,
        Walk:8,
        NewStep:16
    >>,
    {ok, pt:encode(41410, Data, Src_Data)};

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

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


item_to_bin_0 ({
    Id,
    FixedReward,
    State
}) ->
    BinList_FixedReward = [
        item_to_bin_1(FixedReward_Item) || FixedReward_Item <- FixedReward
    ], 

    FixedReward_Len = length(FixedReward), 
    Bin_FixedReward = list_to_binary(BinList_FixedReward),

    Data = <<
        Id:16,
        FixedReward_Len:16, Bin_FixedReward/binary,
        State:8
    >>,
    Data.
item_to_bin_1 ({
    Goodstype,
    Goodsid,
    Num
}) ->
    Data = <<
        Goodstype:16,
        Goodsid:32,
        Num:16
    >>,
    Data.
item_to_bin_2 ({
    Index,
    Qua
}) ->
    Data = <<
        Index:8,
        Qua:8
    >>,
    Data.
item_to_bin_3 ({
    Goodstype,
    Goodsid,
    Num
}) ->
    Data = <<
        Goodstype:32,
        Goodsid:32,
        Num:16
    >>,
    Data.
item_to_bin_4 ({
    GoodsType,
    GoodsId,
    Num
}) ->
    Data = <<
        GoodsType:8,
        GoodsId:32,
        Num:16
    >>,
    Data.
item_to_bin_5 ({
    Index,
    State
}) ->
    Data = <<
        Index:16,
        State:8
    >>,
    Data.
item_to_bin_6 ({
    StepId,
    IsDouble,
    StepReward
}) ->
    BinList_StepReward = [
        item_to_bin_7(StepReward_Item) || StepReward_Item <- StepReward
    ], 

    StepReward_Len = length(StepReward), 
    Bin_StepReward = list_to_binary(BinList_StepReward),

    Data = <<
        StepId:16,
        IsDouble:8,
        StepReward_Len:16, Bin_StepReward/binary
    >>,
    Data.
item_to_bin_7 ({
    GoodId,
    GoodNum
}) ->
    Data = <<
        GoodId:32,
        GoodNum:32
    >>,
    Data.
