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

read(33300, _) ->
    {ok, []};
read(33301, Bin0) ->
    <<BaseType:16, _Bin1/binary>> = Bin0, 
    {ok, [BaseType]};
read(33302, Bin0) ->
    <<BaseType:16, Bin1/binary>> = Bin0, 
    <<Times:8, Bin2/binary>> = Bin1, 
    <<IsAuto:8, _Bin3/binary>> = Bin2, 
    {ok, [BaseType, Times, IsAuto]};
read(33303, Bin0) ->
    <<BaseType:16, Bin1/binary>> = Bin0, 
    <<Id:16, _Bin2/binary>> = Bin1, 
    {ok, [BaseType, Id]};
read(33304, Bin0) ->
    <<BaseType:16, _Bin1/binary>> = Bin0, 
    {ok, [BaseType]};
read(33305, Bin0) ->
    <<BaseType:16, _Bin1/binary>> = Bin0, 
    {ok, [BaseType]};
read(33306, Bin0) ->
    <<BaseType:16, Bin1/binary>> = Bin0, 
    <<Opty:8, Bin2/binary>> = Bin1, 
    <<Auto:8, _Bin3/binary>> = Bin2, 
    {ok, [BaseType, Opty, Auto]};
read(_Cmd, _R) -> {error, no_match}.

write (33300,[
    List
] = Src_Data) ->
    BinList_List = [
        item_to_bin_0(List_Item) || List_Item <- List
    ], 

    List_Len = length(List), 
    Bin_List = list_to_binary(BinList_List),

    Data = <<
        List_Len:16, Bin_List/binary
    >>,
    {ok, pt:encode(33300, Data, Src_Data)};

write (33301,[
    BaseType,
    FreeTimes,
    BuyTimes,
    SumScore,
    Times,
    AwardList,
    RankList
] = Src_Data) ->
    BinList_AwardList = [
        item_to_bin_1(AwardList_Item) || AwardList_Item <- AwardList
    ], 

    AwardList_Len = length(AwardList), 
    Bin_AwardList = list_to_binary(BinList_AwardList),

    BinList_RankList = [
        item_to_bin_2(RankList_Item) || RankList_Item <- RankList
    ], 

    RankList_Len = length(RankList), 
    Bin_RankList = list_to_binary(BinList_RankList),

    Data = <<
        BaseType:16,
        FreeTimes:8,
        BuyTimes:16,
        SumScore:32,
        Times:32,
        AwardList_Len:16, Bin_AwardList/binary,
        RankList_Len:16, Bin_RankList/binary
    >>,
    {ok, pt:encode(33301, Data, Src_Data)};

write (33302,[
    Errcode,
    BaseType,
    Times,
    SumScore,
    DrawTimes,
    Stage,
    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,
        BaseType:16,
        Times:8,
        SumScore:32,
        DrawTimes:32,
        Stage:8,
        RewardList_Len:16, Bin_RewardList/binary
    >>,
    {ok, pt:encode(33302, Data, Src_Data)};

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

write (33304,[
    BaseType,
    RankList
] = Src_Data) ->
    BinList_RankList = [
        item_to_bin_4(RankList_Item) || RankList_Item <- RankList
    ], 

    RankList_Len = length(RankList), 
    Bin_RankList = list_to_binary(BinList_RankList),

    Data = <<
        BaseType:16,
        RankList_Len:16, Bin_RankList/binary
    >>,
    {ok, pt:encode(33304, Data, Src_Data)};

write (33305,[
    BaseType,
    GoldCoin,
    Total,
    Score,
    RuneTimes,
    AwardList
] = Src_Data) ->
    BinList_AwardList = [
        item_to_bin_5(AwardList_Item) || AwardList_Item <- AwardList
    ], 

    AwardList_Len = length(AwardList), 
    Bin_AwardList = list_to_binary(BinList_AwardList),

    Data = <<
        BaseType:16,
        GoldCoin:16,
        Total:32,
        Score:32,
        RuneTimes:16,
        AwardList_Len:16, Bin_AwardList/binary
    >>,
    {ok, pt:encode(33305, Data, Src_Data)};

write (33306,[
    Res,
    BaseType,
    Opty,
    Stage,
    RewardList
] = Src_Data) ->
    BinList_RewardList = [
        item_to_bin_6(RewardList_Item) || RewardList_Item <- RewardList
    ], 

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

    Data = <<
        Res:32,
        BaseType:16,
        Opty:8,
        Stage:8,
        RewardList_Len:16, Bin_RewardList/binary
    >>,
    {ok, pt:encode(33306, Data, Src_Data)};

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


item_to_bin_0 ({
    BaseType,
    EndTime,
    EndShowTime,
    WorldStage
}) ->
    Data = <<
        BaseType:16,
        EndTime:32,
        EndShowTime:32,
        WorldStage:16
    >>,
    Data.
item_to_bin_1 ({
    Id,
    State
}) ->
    Data = <<
        Id:16,
        State:8
    >>,
    Data.
item_to_bin_2 ({
    RoleId,
    RoleName,
    ServerNum,
    Rank,
    Score
}) ->
    Bin_RoleName = pt:write_string(RoleName), 

    Data = <<
        RoleId:64,
        Bin_RoleName/binary,
        ServerNum:32,
        Rank:8,
        Score:32
    >>,
    Data.
item_to_bin_3 (
    AwardId
) ->
    Data = <<
        AwardId:16
    >>,
    Data.
item_to_bin_4 ({
    RoleId,
    RoleName,
    ServerNum,
    Rank,
    Score
}) ->
    Bin_RoleName = pt:write_string(RoleName), 

    Data = <<
        RoleId:64,
        Bin_RoleName/binary,
        ServerNum:32,
        Rank:8,
        Score:32
    >>,
    Data.
item_to_bin_5 ({
    Id,
    State
}) ->
    Data = <<
        Id:16,
        State:8
    >>,
    Data.
item_to_bin_6 (
    AwardId
) ->
    Data = <<
        AwardId:16
    >>,
    Data.
