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

read(33400, Bin0) ->
    <<SubType:16, _Bin1/binary>> = Bin0, 
    {ok, [SubType]};
read(33401, Bin0) ->
    <<SubType:16, Bin1/binary>> = Bin0, 
    <<Type:8, Bin2/binary>> = Bin1, 
    <<IsAuto:8, _Bin3/binary>> = Bin2, 
    {ok, [SubType, Type, IsAuto]};
read(33402, Bin0) ->
    <<SubType:16, Bin1/binary>> = Bin0, 
    <<LogType:8, _Bin2/binary>> = Bin1, 
    {ok, [SubType, LogType]};
read(33403, Bin0) ->
    <<SubType:16, _Bin1/binary>> = Bin0, 
    {ok, [SubType]};
read(33404, Bin0) ->
    <<SubType:16, Bin1/binary>> = Bin0, 
    <<RewardId:32, Bin2/binary>> = Bin1, 
    <<BuyNum:16, _Bin3/binary>> = Bin2, 
    {ok, [SubType, RewardId, BuyNum]};
read(_Cmd, _R) -> {error, no_match}.

write (33400,[
    SubType,
    LuckyValue,
    UseFreeTimes
] = Src_Data) ->
    Data = <<
        SubType:16,
        LuckyValue:32,
        UseFreeTimes:8
    >>,
    {ok, pt:encode(33400, Data, Src_Data)};

write (33401,[
    Errcode,
    SubType,
    LuckyValue,
    UseFreeTimes,
    RewardList,
    Score
] = 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,
        SubType:16,
        LuckyValue:32,
        UseFreeTimes:8,
        RewardList_Len:16, Bin_RewardList/binary,
        Score:32
    >>,
    {ok, pt:encode(33401, Data, Src_Data)};

write (33402,[
    SubType,
    LogType,
    LogList
] = Src_Data) ->
    BinList_LogList = [
        item_to_bin_1(LogList_Item) || LogList_Item <- LogList
    ], 

    LogList_Len = length(LogList), 
    Bin_LogList = list_to_binary(BinList_LogList),

    Data = <<
        SubType:16,
        LogType:8,
        LogList_Len:16, Bin_LogList/binary
    >>,
    {ok, pt:encode(33402, Data, Src_Data)};

write (33403,[
    SubType,
    Score,
    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 = <<
        SubType:16,
        Score:32,
        GoodsList_Len:16, Bin_GoodsList/binary
    >>,
    {ok, pt:encode(33403, Data, Src_Data)};

write (33404,[
    Errcode,
    SubType,
    Score,
    RewardId,
    Times
] = Src_Data) ->
    Data = <<
        Errcode:32,
        SubType:16,
        Score:32,
        RewardId:32,
        Times:16
    >>,
    {ok, pt:encode(33404, Data, Src_Data)};

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


item_to_bin_0 ({
    RewardId,
    Num
}) ->
    Data = <<
        RewardId:32,
        Num:16
    >>,
    Data.
item_to_bin_1 ({
    Name,
    GoodTypeId,
    Num,
    Time
}) ->
    Bin_Name = pt:write_string(Name), 

    Data = <<
        Bin_Name/binary,
        GoodTypeId:32,
        Num:16,
        Time:32
    >>,
    Data.
item_to_bin_2 ({
    RewardId,
    Times
}) ->
    Data = <<
        RewardId:32,
        Times:16
    >>,
    Data.
