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

read(40700, _) ->
    {ok, []};
read(40701, _) ->
    {ok, []};
read(40702, _) ->
    {ok, []};
read(40703, _) ->
    {ok, []};
read(40704, Bin0) ->
    <<Type:8, _Bin1/binary>> = Bin0, 
    {ok, [Type]};
read(40705, Bin0) ->
    <<Type:8, _Bin1/binary>> = Bin0, 
    {ok, [Type]};
read(40706, _) ->
    {ok, []};
read(40707, Bin0) ->
    <<TaskId:16, _Bin1/binary>> = Bin0, 
    {ok, [TaskId]};
read(_Cmd, _R) -> {error, no_match}.

write (40700,[
    GuildList,
    MyGuildRank
] = Src_Data) ->
    BinList_GuildList = [
        item_to_bin_0(GuildList_Item) || GuildList_Item <- GuildList
    ], 

    GuildList_Len = length(GuildList), 
    Bin_GuildList = list_to_binary(BinList_GuildList),

    Data = <<
        GuildList_Len:16, Bin_GuildList/binary,
        MyGuildRank:8
    >>,
    {ok, pt:encode(40700, Data, Src_Data)};

write (40701,[
    RankList,
    MyRank
] = Src_Data) ->
    BinList_RankList = [
        item_to_bin_1(RankList_Item) || RankList_Item <- RankList
    ], 

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

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

write (40702,[
    Rank,
    GoodsId
] = Src_Data) ->
    Data = <<
        Rank:8,
        GoodsId:32
    >>,
    {ok, pt:encode(40702, Data, Src_Data)};

write (40703,[
    GuildList,
    MyLive
] = Src_Data) ->
    BinList_GuildList = [
        item_to_bin_2(GuildList_Item) || GuildList_Item <- GuildList
    ], 

    GuildList_Len = length(GuildList), 
    Bin_GuildList = list_to_binary(BinList_GuildList),

    Data = <<
        GuildList_Len:16, Bin_GuildList/binary,
        MyLive:32
    >>,
    {ok, pt:encode(40703, Data, Src_Data)};

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

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

write (40706,[
    TaskList
] = Src_Data) ->
    BinList_TaskList = [
        item_to_bin_3(TaskList_Item) || TaskList_Item <- TaskList
    ], 

    TaskList_Len = length(TaskList), 
    Bin_TaskList = list_to_binary(BinList_TaskList),

    Data = <<
        TaskList_Len:16, Bin_TaskList/binary
    >>,
    {ok, pt:encode(40706, Data, Src_Data)};

write (40707,[
    Res,
    TaskId,
    Progress,
    Status
] = Src_Data) ->
    Data = <<
        Res:32,
        TaskId:16,
        Progress:64,
        Status:8
    >>,
    {ok, pt:encode(40707, Data, Src_Data)};

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


item_to_bin_0 ({
    Rank,
    GuildId,
    GuildName,
    CaptainName,
    MemNumber,
    MemLimit,
    SumPower,
    OldRank
}) ->
    Bin_GuildName = pt:write_string(GuildName), 

    Bin_CaptainName = pt:write_string(CaptainName), 

    Data = <<
        Rank:8,
        GuildId:64,
        Bin_GuildName/binary,
        Bin_CaptainName/binary,
        MemNumber:8,
        MemLimit:8,
        SumPower:64,
        OldRank:8
    >>,
    Data.
item_to_bin_1 ({
    Rank,
    RoleId,
    RoleName,
    Power,
    Position,
    VipFlag,
    SupVipType
}) ->
    Bin_RoleName = pt:write_string(RoleName), 

    Data = <<
        Rank:8,
        RoleId:64,
        Bin_RoleName/binary,
        Power:64,
        Position:8,
        VipFlag:8,
        SupVipType:8
    >>,
    Data.
item_to_bin_2 ({
    Rank,
    GuildId,
    GuildName,
    CaptainName,
    MemNumber,
    MemLimit,
    SumLive
}) ->
    Bin_GuildName = pt:write_string(GuildName), 

    Bin_CaptainName = pt:write_string(CaptainName), 

    Data = <<
        Rank:8,
        GuildId:64,
        Bin_GuildName/binary,
        Bin_CaptainName/binary,
        MemNumber:8,
        MemLimit:8,
        SumLive:32
    >>,
    Data.
item_to_bin_3 ({
    TaskId,
    Progress,
    Status
}) ->
    Data = <<
        TaskId:16,
        Progress:64,
        Status:8
    >>,
    Data.
