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

read(14501, _) ->
    {ok, []};
read(14502, Bin0) ->
    <<BossId:32, Bin1/binary>> = Bin0, 
    <<Type:8, _Bin2/binary>> = Bin1, 
    {ok, [BossId, Type]};
read(14510, _) ->
    {ok, []};
read(14512, Bin0) ->
    <<TaskId:32, _Bin1/binary>> = Bin0, 
    {ok, [TaskId]};
read(_Cmd, _R) -> {error, no_match}.

write (14501,[
    List
] = Src_Data) ->
    BinList_List = [
        item_to_bin_0(List_Item) || List_Item <- List
    ], 

    List_Len = length(List), 
    Bin_List = list_to_binary(BinList_List),

    Data = <<
        List_Len:16, Bin_List/binary
    >>,
    {ok, pt:encode(14501, Data, Src_Data)};

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

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

write (14510,[
    List
] = Src_Data) ->
    BinList_List = [
        item_to_bin_1(List_Item) || List_Item <- List
    ], 

    List_Len = length(List), 
    Bin_List = list_to_binary(BinList_List),

    Data = <<
        List_Len:16, Bin_List/binary
    >>,
    {ok, pt:encode(14510, Data, Src_Data)};

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

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

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


item_to_bin_0 ({
    BossId,
    Killer,
    KillId,
    Time,
    SelfKillStatus,
    ServerAwardStatus
}) ->
    Bin_Killer = pt:write_string(Killer), 

    Data = <<
        BossId:32,
        Bin_Killer/binary,
        KillId:64,
        Time:32,
        SelfKillStatus:8,
        ServerAwardStatus:8
    >>,
    Data.
item_to_bin_1 ({
    TaskId,
    Process,
    Status
}) ->
    Data = <<
        TaskId:32,
        Process:32,
        Status:8
    >>,
    Data.
