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

read(28101, _) ->
    {ok, []};
read(28103, Bin0) ->
    <<Count:8, _Bin1/binary>> = Bin0,
    {ok, [Count]};
read(28105, _) ->
    {ok, []};
read(28106, Bin0) ->
    <<Grade:8, _Bin1/binary>> = Bin0,
    {ok, [Grade]};
read(28110, _) ->
    {ok, []};
read(28114, _) ->
    {ok, []};
read(28115, Bin0) ->
    <<PageNo:8, Bin1/binary>> = Bin0,
    <<PageSize:8, _Bin2/binary>> = Bin1,
    {ok, [PageNo, PageSize]};
read(28116, Bin0) ->
    <<QuitType:8, _Bin1/binary>> = Bin0,
    {ok, [QuitType]};
read(_Cmd, _R) -> {error, no_match}.

write (28100,[
    Code,
    Args
]) ->
    Bin_Args = pt:write_string(Args),

    Data = <<
        Code:32,
        Bin_Args/binary
    >>,
    {ok, pt:pack(28100, Data)};

write (28101,[
    SeasonNum,
    SeasonEndTime,
    Score,
    SeasonWorldLv,
    MaxCount,
    DailyCount,
    DailyRewardCounts
]) ->
    BinList_DailyRewardCounts = [
        item_to_bin_0(DailyRewardCounts_Item) || DailyRewardCounts_Item <- DailyRewardCounts
    ],

    DailyRewardCounts_Len = length(DailyRewardCounts),
    Bin_DailyRewardCounts = list_to_binary(BinList_DailyRewardCounts),

    Data = <<
        SeasonNum:16,
        SeasonEndTime:32,
        Score:32,
        SeasonWorldLv:32,
        MaxCount:16,
        DailyCount:16,
        DailyRewardCounts_Len:16, Bin_DailyRewardCounts/binary
    >>,
    {ok, pt:pack(28101, Data)};

write (28103,[
    Count,
    RewardObjList
]) ->
    Bin_RewardObjList = pt:write_object_list(RewardObjList),

    Data = <<
        Count:8,
        Bin_RewardObjList/binary
    >>,
    {ok, pt:pack(28103, Data)};

write (28105,[
    GotList
]) ->
    BinList_GotList = [
        item_to_bin_1(GotList_Item) || GotList_Item <- GotList
    ],

    GotList_Len = length(GotList),
    Bin_GotList = list_to_binary(BinList_GotList),

    Data = <<
        GotList_Len:16, Bin_GotList/binary
    >>,
    {ok, pt:pack(28105, Data)};

write (28106,[
    Grade,
    RewardObjList
]) ->
    Bin_RewardObjList = pt:write_object_list(RewardObjList),

    Data = <<
        Grade:8,
        Bin_RewardObjList/binary
    >>,
    {ok, pt:pack(28106, Data)};

write (28110,[
    Res
]) ->
    Data = <<
        Res:8
    >>,
    {ok, pt:pack(28110, Data)};

write (28111,[
    Res
]) ->
    Data = <<
        Res:8
    >>,
    {ok, pt:pack(28111, Data)};

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

write (28113,[
    Res,
    Score
]) ->
    Data = <<
        Res:8,
        Score:16
    >>,
    {ok, pt:pack(28113, Data)};

write (28114,[
    Res
]) ->
    Data = <<
        Res:8
    >>,
    {ok, pt:pack(28114, Data)};

write (28115,[
    PageNo,
    PageSize,
    TotalListLen,
    Type,
    RankList,
    MyRankId
]) ->
    BinList_RankList = [
        item_to_bin_2(RankList_Item) || RankList_Item <- RankList
    ],

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

    Data = <<
        PageNo:8,
        PageSize:8,
        TotalListLen:8,
        Type:8,
        RankList_Len:16, Bin_RankList/binary,
        MyRankId:16
    >>,
    {ok, pt:pack(28115, Data)};



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


item_to_bin_0 ({
    Count,
    State
}) ->
    Data = <<
        Count:8,
        State:8
    >>,
    Data.
item_to_bin_1 ({
    Grade,
    State
}) ->
    Data = <<
        Grade:8,
        State:8
    >>,
    Data.
item_to_bin_2 ({
    RankId,
    RoleId,
    RoleName,
    Career,
    Power,
    Platform,
    Server,
    Score
}) ->
    Bin_RoleName = pt:write_string(RoleName),

    Bin_Platform = pt:write_string(Platform),

    Data = <<
        RankId:16,
        RoleId:64,
        Bin_RoleName/binary,
        Career:8,
        Power:32,
        Bin_Platform/binary,
        Server:16,
        Score:32
    >>,
    Data.
