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

read(28001, _) ->
    {ok, []};
read(28002, _) ->
    {ok, []};
read(28003, Bin0) ->
    <<SelfRank:32, Bin1/binary>> = Bin0, 
    <<RivalId:64, Bin2/binary>> = Bin1, 
    <<RivalRank:32, Bin3/binary>> = Bin2, 
    <<Quick:8, Bin4/binary>> = Bin3, 
    <<Kind:8, _Bin5/binary>> = Bin4, 
    {ok, [SelfRank, RivalId, RivalRank, Quick, Kind]};
read(28004, _) ->
    {ok, []};
read(28005, Bin0) ->
    <<Num:16, _Bin1/binary>> = Bin0, 
    {ok, [Num]};
read(28006, Bin0) ->
    <<PageNum:8, Bin1/binary>> = Bin0, 
    <<Num:8, _Bin2/binary>> = Bin1, 
    {ok, [PageNum, Num]};
read(28008, Bin0) ->
    <<Type:8, _Bin1/binary>> = Bin0, 
    {ok, [Type]};
read(28009, _) ->
    {ok, []};
read(28012, _) ->
    {ok, []};
read(28013, _) ->
    {ok, []};
read(28014, _) ->
    {ok, []};
read(28015, _) ->
    {ok, []};
read(_Cmd, _R) -> {error, no_match}.

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

write (28001,[
    Rank,
    HistoryRank,
    RewardRank,
    Combat,
    IsReward,
    FirstWin,
    FiveBattle,
    LeftNum,
    SpPkNum,
    SpAward,
    Cd,
    IsLimit
] = Src_Data) ->
    Data = <<
        Rank:16,
        HistoryRank:16,
        RewardRank:16,
        Combat:64,
        IsReward:8,
        FirstWin:8,
        FiveBattle:8,
        LeftNum:8,
        SpPkNum:8,
        SpAward:8,
        Cd:16,
        IsLimit:8
    >>,
    {ok, pt:encode(28001, Data, Src_Data)};

write (28002,[
    RoleList
] = Src_Data) ->
    BinList_RoleList = [
        item_to_bin_0(RoleList_Item) || RoleList_Item <- RoleList
    ], 

    RoleList_Len = length(RoleList), 
    Bin_RoleList = list_to_binary(BinList_RoleList),

    Data = <<
        RoleList_Len:16, Bin_RoleList/binary
    >>,
    {ok, pt:encode(28002, Data, Src_Data)};

write (28003,[
    Quick,
    RoleList,
    Result,
    RankOld,
    RankCur,
    RewardList,
    Kind,
    Cd,
    IsLimit
] = Src_Data) ->
    BinList_RoleList = [
        item_to_bin_1(RoleList_Item) || RoleList_Item <- RoleList
    ], 

    RoleList_Len = length(RoleList), 
    Bin_RoleList = list_to_binary(BinList_RoleList),

    BinList_RewardList = [
        item_to_bin_2(RewardList_Item) || RewardList_Item <- RewardList
    ], 

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

    Data = <<
        Quick:8,
        RoleList_Len:16, Bin_RoleList/binary,
        Result:8,
        RankOld:16,
        RankCur:16,
        RewardList_Len:16, Bin_RewardList/binary,
        Kind:8,
        Cd:16,
        IsLimit:8
    >>,
    {ok, pt:encode(28003, Data, Src_Data)};

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

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

write (28006,[
    PageNum,
    Num,
    RankList
] = Src_Data) ->
    BinList_RankList = [
        item_to_bin_3(RankList_Item) || RankList_Item <- RankList
    ], 

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

    Data = <<
        PageNum:8,
        Num:8,
        RankList_Len:16, Bin_RankList/binary
    >>,
    {ok, pt:encode(28006, Data, Src_Data)};

write (28007,[
    RivalId,
    RivalCombat
] = Src_Data) ->
    Data = <<
        RivalId:64,
        RivalCombat:32
    >>,
    {ok, pt:encode(28007, Data, Src_Data)};

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

write (28009,[
    RoleList
] = Src_Data) ->
    BinList_RoleList = [
        item_to_bin_4(RoleList_Item) || RoleList_Item <- RoleList
    ], 

    RoleList_Len = length(RoleList), 
    Bin_RoleList = list_to_binary(BinList_RoleList),

    Data = <<
        RoleList_Len:16, Bin_RoleList/binary
    >>,
    {ok, pt:encode(28009, Data, Src_Data)};

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

write (28013,[
    SelfRobotId,
    SelfRoleId,
    RivalRobotId,
    RivalRoleId
] = Src_Data) ->
    Data = <<
        SelfRobotId:64,
        SelfRoleId:64,
        RivalRobotId:64,
        RivalRoleId:64
    >>,
    {ok, pt:encode(28013, Data, Src_Data)};

write (28014,[
    Stage,
    Time
] = Src_Data) ->
    Data = <<
        Stage:8,
        Time:32
    >>,
    {ok, pt:encode(28014, Data, Src_Data)};

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

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


item_to_bin_0 ({
    Rank,
    RoleId,
    Figure
}) ->
    Bin_Figure = pt:write_figure(Figure), 

    Data = <<
        Rank:32,
        RoleId:64,
        Bin_Figure/binary
    >>,
    Data.
item_to_bin_1 (
    Picture
) ->
    Bin_Picture = pt:write_picture(Picture), 

    Data = <<
        Bin_Picture/binary
    >>,
    Data.
item_to_bin_2 ({
    Type,
    TypeId,
    Num
}) ->
    Data = <<
        Type:8,
        TypeId:32,
        Num:32
    >>,
    Data.
item_to_bin_3 ({
    Rank,
    Name,
    Figure
}) ->
    Bin_Name = pt:write_string(Name), 

    Bin_Figure = pt:write_figure(Figure), 

    Data = <<
        Rank:16,
        Bin_Name/binary,
        Bin_Figure/binary
    >>,
    Data.
item_to_bin_4 ({
    Rank,
    RoleId,
    Figure
}) ->
    Bin_Figure = pt:write_figure(Figure), 

    Data = <<
        Rank:32,
        RoleId:64,
        Bin_Figure/binary
    >>,
    Data.
