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

read(21500, _) ->
    {ok, []};
read(21501, Bin0) ->
    <<DunId:32, _Bin1/binary>> = Bin0, 
    {ok, [DunId]};
read(21502, Bin0) ->
    <<DunId:32, Bin1/binary>> = Bin0, 
    <<MinLv:32, Bin2/binary>> = Bin1, 
    <<Power:32, Bin3/binary>> = Bin2, 
    <<Password:32, Bin4/binary>> = Bin3, 
    <<TaType:8, Bin5/binary>> = Bin4, 
    <<IsNeedApprove:8, Bin6/binary>> = Bin5, 
    <<MergeType:8, _Bin7/binary>> = Bin6, 
    {ok, [DunId, MinLv, Power, Password, TaType, IsNeedApprove, MergeType]};
read(21503, Bin0) ->
    <<TeamId:32, Bin1/binary>> = Bin0, 
    <<DunId:32, Bin2/binary>> = Bin1, 
    <<Password:32, Bin3/binary>> = Bin2, 
    <<IsReady:8, _Bin4/binary>> = Bin3, 
    {ok, [TeamId, DunId, Password, IsReady]};
read(21504, Bin0) ->
    <<TeamId:32, _Bin1/binary>> = Bin0, 
    {ok, [TeamId]};
read(21505, Bin0) ->
    <<RoleId:64, _Bin1/binary>> = Bin0, 
    {ok, [RoleId]};
read(21506, _) ->
    {ok, []};
read(21507, Bin0) ->
    <<IsReady:8, _Bin1/binary>> = Bin0, 
    {ok, [IsReady]};
read(21508, _) ->
    {ok, []};
read(21510, Bin0) ->
    <<TeamId:32, Bin1/binary>> = Bin0, 
    <<DunId:32, Bin2/binary>> = Bin1, 
    FunArray0 = fun(<<RestBin0/binary>>) -> 
        <<RoleId:64, _Args01/binary>> = RestBin0, 
        {RoleId,_Args01}
        end,
    {RoleList, Bin3} = pt:read_array(FunArray0, Bin2),

    <<InviteType:8, _Bin4/binary>> = Bin3, 
    {ok, [TeamId, DunId, RoleList, InviteType]};
read(21511, _) ->
    {ok, []};
read(21512, Bin0) ->
    <<Type:8, Bin1/binary>> = Bin0, 
    <<DunId:32, _Bin2/binary>> = Bin1, 
    {ok, [Type, DunId]};
read(21514, Bin0) ->
    <<Type:8, _Bin1/binary>> = Bin0, 
    {ok, [Type]};
read(21515, Bin0) ->
    <<MinLv:32, Bin1/binary>> = Bin0, 
    <<Power:32, Bin2/binary>> = Bin1, 
    <<Password:32, _Bin3/binary>> = Bin2, 
    {ok, [MinLv, Power, Password]};
read(21516, Bin0) ->
    <<RoleId:64, _Bin1/binary>> = Bin0, 
    {ok, [RoleId]};
read(21517, _) ->
    {ok, []};
read(21519, _) ->
    {ok, []};
read(21520, Bin0) ->
    <<Type:8, Bin1/binary>> = Bin0, 
    FunArray1 = fun(<<RestBin1/binary>>) -> 
        <<TeamId:32, _Args101/binary>> = RestBin1, 
        {TeamId,_Args101}
        end,
    {TeamList, _Bin2} = pt:read_array(FunArray1, Bin1),

    {ok, [Type, TeamList]};
read(21522, _) ->
    {ok, []};
read(21523, Bin0) ->
    <<CountPoint:16, _Bin1/binary>> = Bin0, 
    {ok, [CountPoint]};
read(21524, Bin0) ->
    <<DunType:8, Bin1/binary>> = Bin0, 
    <<ActSubtype:16, Bin2/binary>> = Bin1, 
    <<IsMatch:8, _Bin3/binary>> = Bin2, 
    {ok, [DunType, ActSubtype, IsMatch]};
read(21526, Bin0) ->
    <<DunId:32, Bin1/binary>> = Bin0, 
    <<Type:8, _Bin2/binary>> = Bin1, 
    {ok, [DunId, Type]};
read(21527, Bin0) ->
    <<DunId:32, Bin1/binary>> = Bin0, 
    <<TeamId:32, _Bin2/binary>> = Bin1, 
    {ok, [DunId, TeamId]};
read(21528, Bin0) ->
    <<DunType:8, _Bin1/binary>> = Bin0, 
    {ok, [DunType]};
read(21530, Bin0) ->
    <<TeamId:32, _Bin1/binary>> = Bin0, 
    {ok, [TeamId]};
read(21531, Bin0) ->
    <<Res:8, _Bin1/binary>> = Bin0, 
    {ok, [Res]};
read(21532, _) ->
    {ok, []};
read(21534, Bin0) ->
    <<DunId:32, Bin1/binary>> = Bin0, 
    <<IsNeedApprove:8, _Bin2/binary>> = Bin1, 
    {ok, [DunId, IsNeedApprove]};
read(21537, Bin0) ->
    FunArray1 = fun(<<RestBin1/binary>>) -> 
        <<IsAgree:8, _Args101/binary>> = RestBin1, 
        <<RoleId:64, _Args102/binary>> = _Args101, 
        {{IsAgree, RoleId},_Args102}
        end,
    {ResRequest, _Bin1} = pt:read_array(FunArray1, Bin0),

    {ok, [ResRequest]};
read(21538, Bin0) ->
    <<IsNeedApprove:8, _Bin1/binary>> = Bin0, 
    {ok, [IsNeedApprove]};
read(21539, _) ->
    {ok, []};
read(21540, Bin0) ->
    <<DunType:8, Bin1/binary>> = Bin0, 
    <<HelpType:8, _Bin2/binary>> = Bin1, 
    {ok, [DunType, HelpType]};
read(21541, Bin0) ->
    <<DunType:8, Bin1/binary>> = Bin0, 
    <<MergeType:8, _Bin2/binary>> = Bin1, 
    {ok, [DunType, MergeType]};
read(_Cmd, _R) -> {error, no_match}.

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

write (21501,[
    DunId,
    TeamList
] = Src_Data) ->
    BinList_TeamList = [
        item_to_bin_0(TeamList_Item) || TeamList_Item <- TeamList
    ], 

    TeamList_Len = length(TeamList), 
    Bin_TeamList = list_to_binary(BinList_TeamList),

    Data = <<
        DunId:32,
        TeamList_Len:16, Bin_TeamList/binary
    >>,
    {ok, pt:encode(21501, Data, Src_Data)};

write (21502,[
    Errcode,
    DunId,
    TeamId,
    MinLv,
    Power,
    IsNeedApprove
] = Src_Data) ->
    Data = <<
        Errcode:32,
        DunId:32,
        TeamId:32,
        MinLv:32,
        Power:32,
        IsNeedApprove:8
    >>,
    {ok, pt:encode(21502, Data, Src_Data)};

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

write (21504,[
    TeamId,
    DunId,
    Auto,
    MinLv,
    Power,
    IsMatch,
    NeedPassword,
    Password,
    MemberList
] = Src_Data) ->
    BinList_MemberList = [
        item_to_bin_2(MemberList_Item) || MemberList_Item <- MemberList
    ], 

    MemberList_Len = length(MemberList), 
    Bin_MemberList = list_to_binary(BinList_MemberList),

    Data = <<
        TeamId:32,
        DunId:32,
        Auto:8,
        MinLv:16,
        Power:32,
        IsMatch:8,
        NeedPassword:8,
        Password:32,
        MemberList_Len:16, Bin_MemberList/binary
    >>,
    {ok, pt:encode(21504, Data, Src_Data)};

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

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

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

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

write (21509,[
    RoleId,
    Type,
    Val
] = Src_Data) ->
    Data = <<
        RoleId:64,
        Type:16,
        Val:8
    >>,
    {ok, pt:encode(21509, Data, Src_Data)};

write (21510,[
    Errcode,
    TeamId,
    DunId,
    RoleList
] = Src_Data) ->
    BinList_RoleList = [
        item_to_bin_3(RoleList_Item) || RoleList_Item <- RoleList
    ], 

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

    Data = <<
        Errcode:32,
        TeamId:32,
        DunId:32,
        RoleList_Len:16, Bin_RoleList/binary
    >>,
    {ok, pt:encode(21510, Data, Src_Data)};

write (21511,[
    DunType,
    ShowList
] = Src_Data) ->
    BinList_ShowList = [
        item_to_bin_4(ShowList_Item) || ShowList_Item <- ShowList
    ], 

    ShowList_Len = length(ShowList), 
    Bin_ShowList = list_to_binary(BinList_ShowList),

    Data = <<
        DunType:8,
        ShowList_Len:16, Bin_ShowList/binary
    >>,
    {ok, pt:encode(21511, Data, Src_Data)};

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

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

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

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

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

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

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

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

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

write (21521,[
    IsMatch
] = Src_Data) ->
    Data = <<
        IsMatch:8
    >>,
    {ok, pt:encode(21521, Data, Src_Data)};

write (21522,[
    Errcode,
    Count,
    RestCount,
    CountPoint
] = Src_Data) ->
    Data = <<
        Errcode:32,
        Count:16,
        RestCount:16,
        CountPoint:16
    >>,
    {ok, pt:encode(21522, Data, Src_Data)};

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

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

write (21526,[
    Errcode,
    DunId,
    Type,
    RoleList
] = Src_Data) ->
    BinList_RoleList = [
        item_to_bin_5(RoleList_Item) || RoleList_Item <- RoleList
    ], 

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

    Data = <<
        Errcode:32,
        DunId:32,
        Type:8,
        RoleList_Len:16, Bin_RoleList/binary
    >>,
    {ok, pt:encode(21526, Data, Src_Data)};

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

write (21528,[
    DunType,
    RecoverTime,
    Count
] = Src_Data) ->
    Data = <<
        DunType:8,
        RecoverTime:32,
        Count:16
    >>,
    {ok, pt:encode(21528, Data, Src_Data)};

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

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

write (21531,[
    ErrorCode,
    Res
] = Src_Data) ->
    Data = <<
        ErrorCode:32,
        Res:8
    >>,
    {ok, pt:encode(21531, Data, Src_Data)};

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

write (21533,[
    RoleId,
    Res
] = Src_Data) ->
    Data = <<
        RoleId:64,
        Res:8
    >>,
    {ok, pt:encode(21533, Data, Src_Data)};

write (21534,[
    ErrorCode,
    DunId,
    IsNeedApprove
] = Src_Data) ->
    Data = <<
        ErrorCode:32,
        DunId:32,
        IsNeedApprove:8
    >>,
    {ok, pt:encode(21534, Data, Src_Data)};

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

write (21536,[
    RoleInfo,
    ServerNum,
    SentTime
] = Src_Data) ->
    Bin_RoleInfo = pt:write_picture(RoleInfo), 

    Data = <<
        Bin_RoleInfo/binary,
        ServerNum:32,
        SentTime:32
    >>,
    {ok, pt:encode(21536, Data, Src_Data)};

write (21537,[
    Res,
    AgreeList
] = Src_Data) ->
    BinList_AgreeList = [
        item_to_bin_6(AgreeList_Item) || AgreeList_Item <- AgreeList
    ], 

    AgreeList_Len = length(AgreeList), 
    Bin_AgreeList = list_to_binary(BinList_AgreeList),

    Data = <<
        Res:32,
        AgreeList_Len:16, Bin_AgreeList/binary
    >>,
    {ok, pt:encode(21537, Data, Src_Data)};

write (21538,[
    Res,
    IsNeedApprove
] = Src_Data) ->
    Data = <<
        Res:32,
        IsNeedApprove:8
    >>,
    {ok, pt:encode(21538, Data, Src_Data)};

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

write (21540,[
    Res,
    DunType,
    HelpType
] = Src_Data) ->
    Data = <<
        Res:32,
        DunType:8,
        HelpType:8
    >>,
    {ok, pt:encode(21540, Data, Src_Data)};

write (21541,[
    Res,
    DunType,
    MergeType
] = Src_Data) ->
    Data = <<
        Res:32,
        DunType:8,
        MergeType:8
    >>,
    {ok, pt:encode(21541, Data, Src_Data)};

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


item_to_bin_0 ({
    TeamId,
    RealDun,
    LeaderId,
    Name,
    ServerNum,
    LeftNum,
    Auto,
    MinLv,
    Power,
    IsStart,
    NeedPassword,
    MemberList
}) ->
    Bin_Name = pt:write_string(Name), 

    BinList_MemberList = [
        item_to_bin_1(MemberList_Item) || MemberList_Item <- MemberList
    ], 

    MemberList_Len = length(MemberList), 
    Bin_MemberList = list_to_binary(BinList_MemberList),

    Data = <<
        TeamId:32,
        RealDun:32,
        LeaderId:64,
        Bin_Name/binary,
        ServerNum:16,
        LeftNum:16,
        Auto:8,
        MinLv:16,
        Power:32,
        IsStart:8,
        NeedPassword:8,
        MemberList_Len:16, Bin_MemberList/binary
    >>,
    Data.
item_to_bin_1 ({
    RoleId,
    Lv,
    Career,
    Sex,
    Turn,
    PictureVer,
    Picture,
    ProfilePhotoId,
    DressBoard
}) ->
    Bin_Picture = pt:write_string(Picture), 

    Data = <<
        RoleId:64,
        Lv:16,
        Career:8,
        Sex:8,
        Turn:8,
        PictureVer:32,
        Bin_Picture/binary,
        ProfilePhotoId:32,
        DressBoard:32
    >>,
    Data.
item_to_bin_2 ({
    RoleId,
    ServerId,
    ServerNum,
    IsLeader,
    HelpType,
    Member,
    LeftNum,
    RolePower,
    IsReady,
    DummyType
}) ->
    Bin_Member = pt:write_figure(Member), 

    Data = <<
        RoleId:64,
        ServerId:16,
        ServerNum:16,
        IsLeader:8,
        HelpType:8,
        Bin_Member/binary,
        LeftNum:16,
        RolePower:32,
        IsReady:8,
        DummyType:8
    >>,
    Data.
item_to_bin_3 (
    RoleId
) ->
    Data = <<
        RoleId:64
    >>,
    Data.
item_to_bin_4 ({
    DunId,
    TeamId,
    InviterId,
    InviterFigure,
    EndTime
}) ->
    Bin_InviterFigure = pt:write_figure(InviterFigure), 

    Data = <<
        DunId:32,
        TeamId:32,
        InviterId:64,
        Bin_InviterFigure/binary,
        EndTime:8
    >>,
    Data.
item_to_bin_5 ({
    RoleId,
    Figure,
    Rank,
    SupportPoint
}) ->
    Bin_Figure = pt:write_figure(Figure), 

    Data = <<
        RoleId:64,
        Bin_Figure/binary,
        Rank:16,
        SupportPoint:32
    >>,
    Data.
item_to_bin_6 ({
    IsAgree,
    RoleId
}) ->
    Data = <<
        IsAgree:8,
        RoleId:64
    >>,
    Data.
