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

read(41500, _) ->
    {ok, []};
read(41501, _) ->
    {ok, []};
read(41502, _) ->
    {ok, []};
read(41506, Bin0) ->
    <<Type:8, _Bin1/binary>> = Bin0, 
    {ok, [Type]};
read(41507, Bin0) ->
    <<Type:8, _Bin1/binary>> = Bin0, 
    {ok, [Type]};
read(41510, _) ->
    {ok, []};
read(41511, _) ->
    {ok, []};
read(41512, _) ->
    {ok, []};
read(_Cmd, _R) -> {error, no_match}.

write (41500,[
    Round,
    StatusEtime
] = Src_Data) ->
    Data = <<
        Round:8,
        StatusEtime:32
    >>,
    {ok, pt:encode(41500, Data, Src_Data)};

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

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

write (41503,[
    ChangeTime,
    EndTime,
    InvincibleTime,
    RelieveInvincibleTime
] = Src_Data) ->
    Data = <<
        ChangeTime:32,
        EndTime:32,
        InvincibleTime:32,
        RelieveInvincibleTime:32
    >>,
    {ok, pt:encode(41503, Data, Src_Data)};

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

write (41505,[
    Winner,
    Loser
] = Src_Data) ->
    Bin_Winner = pt:write_picture(Winner), 

    Bin_Loser = pt:write_picture(Loser), 

    Data = <<
        Bin_Winner/binary,
        Bin_Loser/binary
    >>,
    {ok, pt:encode(41505, Data, Src_Data)};

write (41506,[
    HurtRank,
    MyScore,
    Type
] = Src_Data) ->
    BinList_HurtRank = [
        item_to_bin_0(HurtRank_Item) || HurtRank_Item <- HurtRank
    ], 

    HurtRank_Len = length(HurtRank), 
    Bin_HurtRank = list_to_binary(BinList_HurtRank),

    Data = <<
        HurtRank_Len:16, Bin_HurtRank/binary,
        MyScore:32,
        Type:8
    >>,
    {ok, pt:encode(41506, Data, Src_Data)};

write (41507,[
    HurtRank,
    MyScore,
    Type
] = Src_Data) ->
    BinList_HurtRank = [
        item_to_bin_1(HurtRank_Item) || HurtRank_Item <- HurtRank
    ], 

    HurtRank_Len = length(HurtRank), 
    Bin_HurtRank = list_to_binary(BinList_HurtRank),

    Data = <<
        HurtRank_Len:16, Bin_HurtRank/binary,
        MyScore:32,
        Type:8
    >>,
    {ok, pt:encode(41507, Data, Src_Data)};

write (41508,[
    Round1Rank,
    Round1Score,
    Round1GuildRank,
    Round1IsFirstAttack,
    Round2Rank,
    Round2Score,
    Round2GuildRank,
    Round2IsFirstAttack,
    Round2GuildScore,
    Round2TopServer,
    Round2Node
] = Src_Data) ->
    Data = <<
        Round1Rank:16,
        Round1Score:32,
        Round1GuildRank:16,
        Round1IsFirstAttack:8,
        Round2Rank:16,
        Round2Score:32,
        Round2GuildRank:16,
        Round2IsFirstAttack:8,
        Round2GuildScore:32,
        Round2TopServer:16,
        Round2Node:8
    >>,
    {ok, pt:encode(41508, Data, Src_Data)};

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

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

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

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

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


item_to_bin_0 ({
    ServerNum,
    Score,
    Rank,
    RoleInfo
}) ->
    Bin_RoleInfo = pt:write_picture(RoleInfo), 

    Data = <<
        ServerNum:16,
        Score:32,
        Rank:16,
        Bin_RoleInfo/binary
    >>,
    Data.
item_to_bin_1 ({
    ServerNum,
    Name,
    Score,
    Rank,
    GuildFlag
}) ->
    Bin_Name = pt:write_string(Name), 

    Data = <<
        ServerNum:16,
        Bin_Name/binary,
        Score:32,
        Rank:16,
        GuildFlag:32
    >>,
    Data.
