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

read(22101, Bin0) ->
    <<Type:32, Bin1/binary>> = Bin0, 
    <<Start:32, Bin2/binary>> = Bin1, 
    <<Len:32, _Bin3/binary>> = Bin2, 
    {ok, [Type, Start, Len]};
read(22102, Bin0) ->
    <<Type:32, Bin1/binary>> = Bin0, 
    <<Start:32, Bin2/binary>> = Bin1, 
    <<Len:32, _Bin3/binary>> = Bin2, 
    {ok, [Type, Start, Len]};
read(22103, Bin0) ->
    <<RankType:32, _Bin1/binary>> = Bin0, 
    {ok, [RankType]};
read(22104, Bin0) ->
    <<RoleId:64, Bin1/binary>> = Bin0, 
    <<RankType:32, _Bin2/binary>> = Bin1, 
    {ok, [RoleId, RankType]};
read(22105, Bin0) ->
    <<Type:32, Bin1/binary>> = Bin0, 
    <<Start:32, Bin2/binary>> = Bin1, 
    <<Len:32, _Bin3/binary>> = Bin2, 
    {ok, [Type, Start, Len]};
read(22106, Bin0) ->
    <<RankType:32, _Bin1/binary>> = Bin0, 
    {ok, [RankType]};
read(22107, Bin0) ->
    <<RankType:32, Bin1/binary>> = Bin0, 
    <<Target:64, _Bin2/binary>> = Bin1, 
    {ok, [RankType, Target]};
read(22108, Bin0) ->
    <<Type:32, Bin1/binary>> = Bin0, 
    <<Start:32, Bin2/binary>> = Bin1, 
    <<Len:32, _Bin3/binary>> = Bin2, 
    {ok, [Type, Start, Len]};
read(22109, Bin0) ->
    <<RoleId:64, _Bin1/binary>> = Bin0, 
    {ok, [RoleId]};
read(22111, Bin0) ->
    <<Type:32, _Bin1/binary>> = Bin0, 
    {ok, [Type]};
read(22112, Bin0) ->
    <<PlayerId:64, _Bin1/binary>> = Bin0, 
    {ok, [PlayerId]};
read(_Cmd, _R) -> {error, no_match}.

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

write (22101,[
    RankType,
    RoleRank,
    SelVal,
    SelSecVal,
    Sum,
    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 = <<
        RankType:32,
        RoleRank:32,
        SelVal:64,
        SelSecVal:32,
        Sum:32,
        RankList_Len:16, Bin_RankList/binary
    >>,
    {ok, pt:encode(22101, Data, Src_Data)};

write (22102,[
    RankType,
    RoleRank,
    GuildId,
    GuildName,
    GuildLv,
    MembersNum,
    FirstValue,
    Sum,
    RankList
] = Src_Data) ->
    Bin_GuildName = pt:write_string(GuildName), 

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

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

    Data = <<
        RankType:32,
        RoleRank:32,
        GuildId:64,
        Bin_GuildName/binary,
        GuildLv:32,
        MembersNum:32,
        FirstValue:32,
        Sum:32,
        RankList_Len:16, Bin_RankList/binary
    >>,
    {ok, pt:encode(22102, Data, Src_Data)};

write (22103,[
    Errcode,
    RankType,
    RoleId,
    State
] = Src_Data) ->
    Data = <<
        Errcode:32,
        RankType:32,
        RoleId:64,
        State:8
    >>,
    {ok, pt:encode(22103, Data, Src_Data)};

write (22104,[
    Errcode,
    RankType,
    RoleId,
    PraiseNum
] = Src_Data) ->
    Data = <<
        Errcode:32,
        RankType:32,
        RoleId:64,
        PraiseNum:32
    >>,
    {ok, pt:encode(22104, Data, Src_Data)};

write (22105,[
    RankType,
    RankList
] = Src_Data) ->
    BinList_RankList = [
        item_to_bin_2(RankList_Item) || RankList_Item <- RankList
    ], 

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

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

write (22106,[
    RankType,
    TopList
] = Src_Data) ->
    BinList_TopList = [
        item_to_bin_3(TopList_Item) || TopList_Item <- TopList
    ], 

    TopList_Len = length(TopList), 
    Bin_TopList = list_to_binary(BinList_TopList),

    Data = <<
        RankType:32,
        TopList_Len:16, Bin_TopList/binary
    >>,
    {ok, pt:encode(22106, Data, Src_Data)};

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

write (22108,[
    Errcode,
    RankType,
    RoleRank,
    SelVal,
    SelSecVal,
    Sum,
    RankList,
    TopThree
] = Src_Data) ->
    BinList_RankList = [
        item_to_bin_4(RankList_Item) || RankList_Item <- RankList
    ], 

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

    BinList_TopThree = [
        item_to_bin_5(TopThree_Item) || TopThree_Item <- TopThree
    ], 

    TopThree_Len = length(TopThree), 
    Bin_TopThree = list_to_binary(BinList_TopThree),

    Data = <<
        Errcode:32,
        RankType:32,
        RoleRank:32,
        SelVal:64,
        SelSecVal:32,
        Sum:32,
        RankList_Len:16, Bin_RankList/binary,
        TopThree_Len:16, Bin_TopThree/binary
    >>,
    {ok, pt:encode(22108, Data, Src_Data)};

write (22109,[
    Figure
] = Src_Data) ->
    Bin_Figure = pt:write_figure(Figure), 

    Data = <<
        Bin_Figure/binary
    >>,
    {ok, pt:encode(22109, Data, Src_Data)};

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

write (22111,[
    RankType,
    RankList
] = Src_Data) ->
    BinList_RankList = [
        item_to_bin_6(RankList_Item) || RankList_Item <- RankList
    ], 

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

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

write (22112,[
    PlayerId,
    EquipList
] = Src_Data) ->
    BinList_EquipList = [
        item_to_bin_7(EquipList_Item) || EquipList_Item <- EquipList
    ], 

    EquipList_Len = length(EquipList), 
    Bin_EquipList = list_to_binary(BinList_EquipList),

    Data = <<
        PlayerId:64,
        EquipList_Len:16, Bin_EquipList/binary
    >>,
    {ok, pt:encode(22112, Data, Src_Data)};

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


item_to_bin_0 ({
    PlayerId,
    PraiseNum,
    Figure,
    SelCombat,
    FirstValue,
    SecondValue,
    ThirdValue,
    ExtraValues,
    Rank
}) ->
    Bin_Figure = pt:write_figure(Figure), 

    Bin_ExtraValues = pt:write_string(ExtraValues), 

    Data = <<
        PlayerId:64,
        PraiseNum:32,
        Bin_Figure/binary,
        SelCombat:64,
        FirstValue:64,
        SecondValue:32,
        ThirdValue:32,
        Bin_ExtraValues/binary,
        Rank:32
    >>,
    Data.
item_to_bin_1 ({
    GuildId,
    GuildName,
    GuildFlag,
    ChairmanId,
    ChairmanName,
    PraiseNum,
    GuildLv,
    MembersNum,
    Figure,
    SelCombat,
    FirstValue,
    SecValue,
    Rank,
    AddMemNum,
    Division
}) ->
    Bin_GuildName = pt:write_string(GuildName), 

    Bin_ChairmanName = pt:write_string(ChairmanName), 

    Bin_Figure = pt:write_figure(Figure), 

    Data = <<
        GuildId:64,
        Bin_GuildName/binary,
        GuildFlag:32,
        ChairmanId:64,
        Bin_ChairmanName/binary,
        PraiseNum:32,
        GuildLv:32,
        MembersNum:32,
        Bin_Figure/binary,
        SelCombat:64,
        FirstValue:64,
        SecValue:64,
        Rank:32,
        AddMemNum:8,
        Division:16
    >>,
    Data.
item_to_bin_2 ({
    PlayerId,
    Rank
}) ->
    Data = <<
        PlayerId:64,
        Rank:32
    >>,
    Data.
item_to_bin_3 ({
    Target,
    AwardState,
    Score,
    Figure
}) ->
    Bin_Figure = pt:write_picture(Figure), 

    Data = <<
        Target:64,
        AwardState:8,
        Score:64,
        Bin_Figure/binary
    >>,
    Data.
item_to_bin_4 ({
    PlayerId,
    FirstValue,
    SecondValue,
    Rank,
    Name,
    Vip,
    SupVip,
    Sex,
    ServerNum,
    ServerId
}) ->
    Bin_Name = pt:write_string(Name), 

    Data = <<
        PlayerId:64,
        FirstValue:64,
        SecondValue:32,
        Rank:32,
        Bin_Name/binary,
        Vip:16,
        SupVip:16,
        Sex:16,
        ServerNum:32,
        ServerId:32
    >>,
    Data.
item_to_bin_5 ({
    Rank,
    RoleFigure,
    ServerNum,
    ServerId
}) ->
    Bin_RoleFigure = pt:write_picture(RoleFigure), 

    Data = <<
        Rank:32,
        Bin_RoleFigure/binary,
        ServerNum:32,
        ServerId:32
    >>,
    Data.
item_to_bin_6 ({
    PlayerId,
    Figure,
    Score,
    Rank
}) ->
    Bin_Figure = pt:write_picture(Figure), 

    Data = <<
        PlayerId:64,
        Bin_Figure/binary,
        Score:64,
        Rank:32
    >>,
    Data.
item_to_bin_7 ({
    TypeId,
    Cell,
    WarSoulLv
}) ->
    Data = <<
        TypeId:32,
        Cell:32,
        WarSoulLv:16
    >>,
    Data.
