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

read(22501, Bin0) ->
    <<Type:32, Bin1/binary>> = Bin0, 
    <<SubType:16, _Bin2/binary>> = Bin1, 
    {ok, [Type, SubType]};
read(22502, Bin0) ->
    <<SubType:16, _Bin1/binary>> = Bin0, 
    {ok, [SubType]};
read(22503, Bin0) ->
    <<Type:32, Bin1/binary>> = Bin0, 
    <<SubType:16, Bin2/binary>> = Bin1, 
    <<Goal:8, _Bin3/binary>> = Bin2, 
    {ok, [Type, SubType, Goal]};
read(22510, _) ->
    {ok, []};
read(_Cmd, _R) -> {error, no_match}.

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

write (22501,[
    SubType,
    RankType,
    SelRank,
    SelVal1,
    SelVal2,
    Sum,
    MaxLen,
    RankLimit,
    RankList
] = Src_Data) ->
    BinList_RankList = [
        item_to_bin_0(RankList_Item) || RankList_Item <- RankList
    ], 

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

    Data = <<
        SubType:16,
        RankType:32,
        SelRank:32,
        SelVal1:32,
        SelVal2:32,
        Sum:32,
        MaxLen:16,
        RankLimit:32,
        RankList_Len:16, Bin_RankList/binary
    >>,
    {ok, pt:encode(22501, Data, Src_Data)};

write (22502,[
    SubType,
    GoalList
] = Src_Data) ->
    BinList_GoalList = [
        item_to_bin_1(GoalList_Item) || GoalList_Item <- GoalList
    ], 

    GoalList_Len = length(GoalList), 
    Bin_GoalList = list_to_binary(BinList_GoalList),

    Data = <<
        SubType:16,
        GoalList_Len:16, Bin_GoalList/binary
    >>,
    {ok, pt:encode(22502, Data, Src_Data)};

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

write (22510,[
    RankTypeCfg,
    RankRewardCfg,
    RankGoalCfg
] = Src_Data) ->
    BinList_RankTypeCfg = [
        item_to_bin_2(RankTypeCfg_Item) || RankTypeCfg_Item <- RankTypeCfg
    ], 

    RankTypeCfg_Len = length(RankTypeCfg), 
    Bin_RankTypeCfg = list_to_binary(BinList_RankTypeCfg),

    BinList_RankRewardCfg = [
        item_to_bin_3(RankRewardCfg_Item) || RankRewardCfg_Item <- RankRewardCfg
    ], 

    RankRewardCfg_Len = length(RankRewardCfg), 
    Bin_RankRewardCfg = list_to_binary(BinList_RankRewardCfg),

    BinList_RankGoalCfg = [
        item_to_bin_4(RankGoalCfg_Item) || RankGoalCfg_Item <- RankGoalCfg
    ], 

    RankGoalCfg_Len = length(RankGoalCfg), 
    Bin_RankGoalCfg = list_to_binary(BinList_RankGoalCfg),

    Data = <<
        RankTypeCfg_Len:16, Bin_RankTypeCfg/binary,
        RankRewardCfg_Len:16, Bin_RankRewardCfg/binary,
        RankGoalCfg_Len:16, Bin_RankGoalCfg/binary
    >>,
    {ok, pt:encode(22510, Data, Src_Data)};

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


item_to_bin_0 ({
    PlayerId,
    Name,
    Value1,
    Value2,
    Value3,
    Rank,
    VipFlag,
    SupVipType
}) ->
    Bin_Name = pt:write_string(Name), 

    Data = <<
        PlayerId:64,
        Bin_Name/binary,
        Value1:32,
        Value2:32,
        Value3:32,
        Rank:16,
        VipFlag:8,
        SupVipType:8
    >>,
    Data.
item_to_bin_1 ({
    RankType,
    Goal,
    GoalSum,
    State
}) ->
    Data = <<
        RankType:32,
        Goal:16,
        GoalSum:16,
        State:8
    >>,
    Data.
item_to_bin_2 ({
    Id,
    Name,
    StartDay,
    ClearDay,
    Limit,
    FigureId
}) ->
    Bin_Name = pt:write_string(Name), 

    Bin_Limit = pt:write_string(Limit), 

    Bin_FigureId = pt:write_string(FigureId), 

    Data = <<
        Id:8,
        Bin_Name/binary,
        StartDay:8,
        ClearDay:8,
        Bin_Limit/binary,
        Bin_FigureId/binary
    >>,
    Data.
item_to_bin_3 ({
    Id,
    RewardId,
    RankMin,
    RankMax,
    RankLimit,
    Reward,
    About
}) ->
    Bin_RankLimit = pt:write_string(RankLimit), 

    Bin_Reward = pt:write_string(Reward), 

    Bin_About = pt:write_string(About), 

    Data = <<
        Id:8,
        RewardId:16,
        RankMin:16,
        RankMax:16,
        Bin_RankLimit/binary,
        Bin_Reward/binary,
        Bin_About/binary
    >>,
    Data.
item_to_bin_4 ({
    Id,
    RewardId,
    GoalValue,
    Reward,
    About
}) ->
    Bin_GoalValue = pt:write_string(GoalValue), 

    Bin_Reward = pt:write_string(Reward), 

    Bin_About = pt:write_string(About), 

    Data = <<
        Id:8,
        RewardId:8,
        Bin_GoalValue/binary,
        Bin_Reward/binary,
        Bin_About/binary
    >>,
    Data.
