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

read(45101, _) ->
    {ok, []};
read(45102, Bin0) ->
    <<Type:8, _Bin1/binary>> = Bin0, 
    {ok, [Type]};
read(45103, Bin0) ->
    <<Type:8, Bin1/binary>> = Bin0, 
    <<GoodsId:32, Bin2/binary>> = Bin1, 
    <<Count:8, _Bin3/binary>> = Bin2, 
    {ok, [Type, GoodsId, Count]};
read(45104, _) ->
    {ok, []};
read(45105, Bin0) ->
    <<GoodsId:32, Bin1/binary>> = Bin0, 
    <<Num:16, _Bin2/binary>> = Bin1, 
    {ok, [GoodsId, Num]};
read(45106, _) ->
    {ok, []};
read(45107, Bin0) ->
    <<Type:8, _Bin1/binary>> = Bin0, 
    {ok, [Type]};
read(45109, _) ->
    {ok, []};
read(45110, Bin0) ->
    <<DayId:8, Bin1/binary>> = Bin0, 
    <<Type:8, _Bin2/binary>> = Bin1, 
    {ok, [DayId, Type]};
read(_Cmd, _R) -> {error, no_match}.

write (45101,[
    SupVipType,
    EndTime,
    TodayCharge,
    TotalCharge,
    ContinueDay,
    Day,
    GiftCount
] = Src_Data) ->
    Data = <<
        SupVipType:8,
        EndTime:32,
        TodayCharge:32,
        TotalCharge:32,
        ContinueDay:8,
        Day:8,
        GiftCount:8
    >>,
    {ok, pt:encode(45101, Data, Src_Data)};

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

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

write (45104,[
    Res,
    Type,
    GoodsList,
    UnlockId
] = Src_Data) ->
    BinList_GoodsList = [
        item_to_bin_0(GoodsList_Item) || GoodsList_Item <- GoodsList
    ], 

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

    Data = <<
        Res:32,
        Type:8,
        GoodsList_Len:16, Bin_GoodsList/binary,
        UnlockId:8
    >>,
    {ok, pt:encode(45104, Data, Src_Data)};

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

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

write (45107,[
    Type,
    RestCount
] = Src_Data) ->
    Data = <<
        Type:8,
        RestCount:16
    >>,
    {ok, pt:encode(45107, Data, Src_Data)};

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

write (45109,[
    DayState,
    LoginDays
] = Src_Data) ->
    BinList_DayState = [
        item_to_bin_1(DayState_Item) || DayState_Item <- DayState
    ], 

    DayState_Len = length(DayState), 
    Bin_DayState = list_to_binary(BinList_DayState),

    Data = <<
        DayState_Len:16, Bin_DayState/binary,
        LoginDays:32
    >>,
    {ok, pt:encode(45109, Data, Src_Data)};

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

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


item_to_bin_0 ({
    CountType,
    GoodsId,
    Count
}) ->
    Data = <<
        CountType:8,
        GoodsId:32,
        Count:8
    >>,
    Data.
item_to_bin_1 ({
    DayId,
    RewardState
}) ->
    Data = <<
        DayId:8,
        RewardState:8
    >>,
    Data.
