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

read(42401, Bin0) ->
    <<Type:8, _Bin1/binary>> = Bin0, 
    {ok, [Type]};
read(42402, Bin0) ->
    <<Type:8, Bin1/binary>> = Bin0, 
    <<Event:8, _Bin2/binary>> = Bin1, 
    {ok, [Type, Event]};
read(42403, _) ->
    {ok, []};
read(42405, _) ->
    {ok, []};
read(42406, Bin0) ->
    <<Times:16, _Bin1/binary>> = Bin0, 
    {ok, [Times]};
read(42407, _) ->
    {ok, []};
read(42408, Bin0) ->
    <<QuizId:16, Bin1/binary>> = Bin0, 
    <<AnswerId:8, _Bin2/binary>> = Bin1, 
    {ok, [QuizId, AnswerId]};
read(42409, _) ->
    {ok, []};
read(42410, Bin0) ->
    <<RewardId:16, Bin1/binary>> = Bin0, 
    <<Index:8, Bin2/binary>> = Bin1, 
    <<BuyNum:16, _Bin3/binary>> = Bin2, 
    {ok, [RewardId, Index, BuyNum]};
read(42411, _) ->
    {ok, []};
read(42412, _) ->
    {ok, []};
read(42413, Bin0) ->
    <<Num:16, _Bin1/binary>> = Bin0, 
    {ok, [Num]};
read(42414, _) ->
    {ok, []};
read(42415, Bin0) ->
    <<ExpireTime:32, Bin1/binary>> = Bin0, 
    <<RewardId:16, Bin2/binary>> = Bin1, 
    <<Index:8, Bin3/binary>> = Bin2, 
    <<BuyNum:16, _Bin4/binary>> = Bin3, 
    {ok, [ExpireTime, RewardId, Index, BuyNum]};
read(_Cmd, _R) -> {error, no_match}.

write (42401,[
    Errcode,
    Type,
    NeedFindWay,
    Scene,
    PoolId,
    X,
    Y,
    Event
] = Src_Data) ->
    Data = <<
        Errcode:32,
        Type:8,
        NeedFindWay:8,
        Scene:32,
        PoolId:32,
        X:32,
        Y:32,
        Event:8
    >>,
    {ok, pt:encode(42401, Data, Src_Data)};

write (42402,[
    Errcode,
    Type,
    Event,
    RewardId,
    RewardList
] = Src_Data) ->
    BinList_RewardList = [
        item_to_bin_0(RewardList_Item) || RewardList_Item <- RewardList
    ], 

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

    Data = <<
        Errcode:32,
        Type:8,
        Event:8,
        RewardId:16,
        RewardList_Len:16, Bin_RewardList/binary
    >>,
    {ok, pt:encode(42402, Data, Src_Data)};

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

write (42405,[
    Times,
    ReceiveList
] = Src_Data) ->
    BinList_ReceiveList = [
        item_to_bin_1(ReceiveList_Item) || ReceiveList_Item <- ReceiveList
    ], 

    ReceiveList_Len = length(ReceiveList), 
    Bin_ReceiveList = list_to_binary(BinList_ReceiveList),

    Data = <<
        Times:16,
        ReceiveList_Len:16, Bin_ReceiveList/binary
    >>,
    {ok, pt:encode(42405, Data, Src_Data)};

write (42406,[
    Errcode,
    ReceiveList
] = Src_Data) ->
    BinList_ReceiveList = [
        item_to_bin_2(ReceiveList_Item) || ReceiveList_Item <- ReceiveList
    ], 

    ReceiveList_Len = length(ReceiveList), 
    Bin_ReceiveList = list_to_binary(BinList_ReceiveList),

    Data = <<
        Errcode:32,
        ReceiveList_Len:16, Bin_ReceiveList/binary
    >>,
    {ok, pt:encode(42406, Data, Src_Data)};

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

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

write (42409,[
    Errcode,
    FreeReward,
    LimitReward,
    ExpireTime
] = Src_Data) ->
    Data = <<
        Errcode:32,
        FreeReward:16,
        LimitReward:16,
        ExpireTime:32
    >>,
    {ok, pt:encode(42409, Data, Src_Data)};

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

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

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

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

write (42414,[
    Errcode,
    GoodsList
] = Src_Data) ->
    BinList_GoodsList = [
        item_to_bin_3(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(42414, Data, Src_Data)};

write (42415,[
    Errcode,
    ExpireTime,
    RewardId,
    Index,
    RestNum,
    MaxNum
] = Src_Data) ->
    Data = <<
        Errcode:32,
        ExpireTime:32,
        RewardId:16,
        Index:8,
        RestNum:8,
        MaxNum:8
    >>,
    {ok, pt:encode(42415, Data, Src_Data)};

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


item_to_bin_0 (
    RewardId
) ->
    Data = <<
        RewardId:16
    >>,
    Data.
item_to_bin_1 (
    ReTimes
) ->
    Data = <<
        ReTimes:16
    >>,
    Data.
item_to_bin_2 (
    ReTimes
) ->
    Data = <<
        ReTimes:16
    >>,
    Data.
item_to_bin_3 ({
    ExpireTime,
    RewardId,
    Index,
    RestNum,
    MaxNum
}) ->
    Data = <<
        ExpireTime:32,
        RewardId:16,
        Index:8,
        RestNum:16,
        MaxNum:16
    >>,
    Data.
