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

read(46201, Bin0) ->
    <<BossType:8, Bin1/binary>> = Bin0, 
    <<Level:8, Bin2/binary>> = Bin1, 
    <<Guild:64, _Bin3/binary>> = Bin2, 
    {ok, [BossType, Level, Guild]};
read(46202, Bin0) ->
    <<BossType:8, Bin1/binary>> = Bin0, 
    <<BossId:32, _Bin2/binary>> = Bin1, 
    {ok, [BossType, BossId]};
read(46203, _) ->
    {ok, []};
read(46204, Bin0) ->
    <<BossType:8, Bin1/binary>> = Bin0, 
    <<BossId:32, _Bin2/binary>> = Bin1, 
    {ok, [BossType, BossId]};
read(46205, _) ->
    {ok, []};
read(_Cmd, _R) -> {error, no_match}.

write (46201,[
    BossType,
    Level,
    Guild,
    BossInfo
] = Src_Data) ->
    BinList_BossInfo = [
        item_to_bin_0(BossInfo_Item) || BossInfo_Item <- BossInfo
    ], 

    BossInfo_Len = length(BossInfo), 
    Bin_BossInfo = list_to_binary(BinList_BossInfo),

    Data = <<
        BossType:8,
        Level:8,
        Guild:64,
        BossInfo_Len:16, Bin_BossInfo/binary
    >>,
    {ok, pt:encode(46201, Data, Src_Data)};

write (46202,[
    BossType,
    BossId,
    BossHp,
    RankList
] = 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 = <<
        BossType:8,
        BossId:32,
        BossHp:64,
        RankList_Len:16, Bin_RankList/binary
    >>,
    {ok, pt:encode(46202, Data, Src_Data)};

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

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

write (46205,[
    GuildList
] = 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
    >>,
    {ok, pt:encode(46205, Data, Src_Data)};

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

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


item_to_bin_0 ({
    BossId,
    RebornTime,
    Killer
}) ->
    Bin_Killer = pt:write_string(Killer), 

    Data = <<
        BossId:32,
        RebornTime:32,
        Bin_Killer/binary
    >>,
    Data.
item_to_bin_1 ({
    RoleId,
    Type,
    Name,
    Hurt
}) ->
    Bin_Name = pt:write_string(Name), 

    Data = <<
        RoleId:64,
        Type:8,
        Bin_Name/binary,
        Hurt:64
    >>,
    Data.
item_to_bin_2 ({
    GuildId,
    GuildName
}) ->
    Bin_GuildName = pt:write_string(GuildName), 

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