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

read(46101, Bin0) ->
    <<Level:8, _Bin1/binary>> = Bin0, 
    {ok, [Level]};
read(46102, _) ->
    {ok, []};
read(46103, Bin0) ->
    <<BossId:32, _Bin1/binary>> = Bin0, 
    {ok, [BossId]};
read(46104, _) ->
    {ok, []};
read(46105, _) ->
    {ok, []};
read(46106, Bin0) ->
    <<BossId:32, _Bin1/binary>> = Bin0, 
    {ok, [BossId]};
read(46107, Bin0) ->
    <<Type:8, Bin1/binary>> = Bin0, 
    <<BossId:32, _Bin2/binary>> = Bin1, 
    {ok, [Type, BossId]};
read(46108, _) ->
    {ok, []};
read(46109, Bin0) ->
    <<Level:8, _Bin1/binary>> = Bin0, 
    {ok, [Level]};
read(46111, _) ->
    {ok, []};
read(46112, _) ->
    {ok, []};
read(_Cmd, _R) -> {error, no_match}.

write (46101,[
    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 = <<
        BossInfo_Len:16, Bin_BossInfo/binary
    >>,
    {ok, pt:encode(46101, Data, Src_Data)};

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

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

write (46104,[
    BossId,
    RoleId,
    Name,
    Hurt
] = Src_Data) ->
    Bin_Name = pt:write_string(Name), 

    Data = <<
        BossId:32,
        RoleId:64,
        Bin_Name/binary,
        Hurt:64
    >>,
    {ok, pt:encode(46104, Data, Src_Data)};

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

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

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

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

write (46109,[
    BossInfo
] = Src_Data) ->
    BinList_BossInfo = [
        item_to_bin_2(BossInfo_Item) || BossInfo_Item <- BossInfo
    ], 

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

    Data = <<
        BossInfo_Len:16, Bin_BossInfo/binary
    >>,
    {ok, pt:encode(46109, Data, Src_Data)};

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

write (46111,[
    UseTimes,
    ProtectTime
] = Src_Data) ->
    Data = <<
        UseTimes:8,
        ProtectTime:32
    >>,
    {ok, pt:encode(46111, Data, Src_Data)};

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

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

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

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


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

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

    Data = <<
        RoleId:64,
        Type:8,
        ServerNum:16,
        Bin_Name/binary,
        Hurt:64
    >>,
    Data.
item_to_bin_2 ({
    Index,
    Alive
}) ->
    Data = <<
        Index:8,
        Alive:8
    >>,
    Data.
