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

read(40500, _) ->
    {ok, []};
read(40501, Bin0) ->
    <<PoolId:8, _Bin1/binary>> = Bin0, 
    {ok, [PoolId]};
read(40502, _) ->
    {ok, []};
read(40504, _) ->
    {ok, []};
read(40505, _) ->
    {ok, []};
read(40507, Bin0) ->
    <<NpcId:32, _Bin1/binary>> = Bin0, 
    {ok, [NpcId]};
read(40509, _) ->
    {ok, []};
read(40513, _) ->
    {ok, []};
read(40514, _) ->
    {ok, []};
read(_Cmd, _R) -> {error, no_match}.

write (40500,[
    BeginTime,
    EndTime,
    Score,
    PoolInfo
] = Src_Data) ->
    BinList_PoolInfo = [
        item_to_bin_0(PoolInfo_Item) || PoolInfo_Item <- PoolInfo
    ], 

    PoolInfo_Len = length(PoolInfo), 
    Bin_PoolInfo = list_to_binary(BinList_PoolInfo),

    Data = <<
        BeginTime:32,
        EndTime:32,
        Score:32,
        PoolInfo_Len:16, Bin_PoolInfo/binary
    >>,
    {ok, pt:encode(40500, Data, Src_Data)};

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



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

write (40504,[
    Rank
] = Src_Data) ->
    BinList_Rank = [
        item_to_bin_1(Rank_Item) || Rank_Item <- Rank
    ], 

    Rank_Len = length(Rank), 
    Bin_Rank = list_to_binary(BinList_Rank),

    Data = <<
        Rank_Len:16, Bin_Rank/binary
    >>,
    {ok, pt:encode(40504, Data, Src_Data)};

write (40505,[
    Rank
] = Src_Data) ->
    BinList_Rank = [
        item_to_bin_2(Rank_Item) || Rank_Item <- Rank
    ], 

    Rank_Len = length(Rank), 
    Bin_Rank = list_to_binary(BinList_Rank),

    Data = <<
        Rank_Len:16, Bin_Rank/binary
    >>,
    {ok, pt:encode(40505, Data, Src_Data)};

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

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

write (40508,[
    TopGuild,
    TopGuildChief,
    MyGuildRank,
    MyPersonalRank,
    Score,
    KillRank
] = Src_Data) ->
    Bin_TopGuild = pt:write_string(TopGuild), 

    Bin_TopGuildChief = pt:write_string(TopGuildChief), 

    Data = <<
        Bin_TopGuild/binary,
        Bin_TopGuildChief/binary,
        MyGuildRank:16,
        MyPersonalRank:16,
        Score:32,
        KillRank:16
    >>,
    {ok, pt:encode(40508, Data, Src_Data)};

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

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

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

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

write (40512,[
    Picture,
    Op,
    Score,
    Value
] = Src_Data) ->
    Bin_Picture = pt:write_picture(Picture), 

    Bin_Value = pt:write_string(Value), 

    Data = <<
        Bin_Picture/binary,
        Op:8,
        Score:16,
        Bin_Value/binary
    >>,
    {ok, pt:encode(40512, Data, Src_Data)};

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

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

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


item_to_bin_0 ({
    PoolId,
    Num
}) ->
    Data = <<
        PoolId:8,
        Num:8
    >>,
    Data.
item_to_bin_1 ({
    Name,
    SupVipType,
    VipFlag,
    Sex,
    Power,
    GuildName,
    Score,
    Time,
    MaxKill,
    ContinueKill
}) ->
    Bin_Name = pt:write_string(Name), 

    Bin_GuildName = pt:write_string(GuildName), 

    Data = <<
        Bin_Name/binary,
        SupVipType:8,
        VipFlag:8,
        Sex:8,
        Power:64,
        Bin_GuildName/binary,
        Score:32,
        Time:32,
        MaxKill:16,
        ContinueKill:16
    >>,
    Data.
item_to_bin_2 ({
    GuildId,
    GuildName,
    Score,
    Time
}) ->
    Bin_GuildName = pt:write_string(GuildName), 

    Data = <<
        GuildId:64,
        Bin_GuildName/binary,
        Score:32,
        Time:32
    >>,
    Data.
