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

read(40600, _) ->
    {ok, []};
read(40601, _) ->
    {ok, []};
read(40602, Bin0) ->
    <<Act:8, _Bin1/binary>> = Bin0, 
    {ok, [Act]};
read(40603, Bin0) ->
    <<Act:8, _Bin1/binary>> = Bin0, 
    {ok, [Act]};
read(40604, Bin0) ->
    <<Act:8, Bin1/binary>> = Bin0, 
    <<Opty:8, _Bin2/binary>> = Bin1, 
    {ok, [Act, Opty]};
read(40606, _) ->
    {ok, []};
read(40607, _) ->
    {ok, []};
read(40608, Bin0) ->
    <<Opty:8, _Bin1/binary>> = Bin0, 
    {ok, [Opty]};
read(40610, Bin0) ->
    <<Opty:8, Bin1/binary>> = Bin0, 
    <<DropId:32, _Bin2/binary>> = Bin1, 
    {ok, [Opty, DropId]};
read(40613, _) ->
    {ok, []};
read(40614, Bin0) ->
    <<Option:8, Bin1/binary>> = Bin0, 
    <<OrderId:16, _Bin2/binary>> = Bin1, 
    {ok, [Option, OrderId]};
read(40616, Bin0) ->
    <<OrderId:16, _Bin1/binary>> = Bin0, 
    {ok, [OrderId]};
read(_Cmd, _R) -> {error, no_match}.

write (40600,[
    Act,
    Mon,
    Hp,
    MaxHp,
    State,
    Stime,
    Etime
] = Src_Data) ->
    Data = <<
        Act:8,
        Mon:32,
        Hp:64,
        MaxHp:64,
        State:8,
        Stime:32,
        Etime:32
    >>,
    {ok, pt:encode(40600, Data, Src_Data)};

write (40601,[
    Hp,
    HpLimit
] = Src_Data) ->
    Data = <<
        Hp:64,
        HpLimit:64
    >>,
    {ok, pt:encode(40601, Data, Src_Data)};

write (40602,[
    Act,
    HurtList
] = Src_Data) ->
    BinList_HurtList = [
        item_to_bin_0(HurtList_Item) || HurtList_Item <- HurtList
    ], 

    HurtList_Len = length(HurtList), 
    Bin_HurtList = list_to_binary(BinList_HurtList),

    Data = <<
        Act:8,
        HurtList_Len:16, Bin_HurtList/binary
    >>,
    {ok, pt:encode(40602, Data, Src_Data)};

write (40603,[
    Act,
    HurtList
] = Src_Data) ->
    BinList_HurtList = [
        item_to_bin_1(HurtList_Item) || HurtList_Item <- HurtList
    ], 

    HurtList_Len = length(HurtList), 
    Bin_HurtList = list_to_binary(BinList_HurtList),

    Data = <<
        Act:8,
        HurtList_Len:16, Bin_HurtList/binary
    >>,
    {ok, pt:encode(40603, Data, Src_Data)};

write (40604,[
    Act,
    Opty,
    Id,
    Rank,
    Hp
] = Src_Data) ->
    Data = <<
        Act:8,
        Opty:8,
        Id:64,
        Rank:16,
        Hp:64
    >>,
    {ok, pt:encode(40604, Data, Src_Data)};

write (40605,[
    Mrank,
    Grank,
    RoleAwards,
    GuildAwards
] = Src_Data) ->
    BinList_RoleAwards = [
        item_to_bin_2(RoleAwards_Item) || RoleAwards_Item <- RoleAwards
    ], 

    RoleAwards_Len = length(RoleAwards), 
    Bin_RoleAwards = list_to_binary(BinList_RoleAwards),

    BinList_GuildAwards = [
        item_to_bin_3(GuildAwards_Item) || GuildAwards_Item <- GuildAwards
    ], 

    GuildAwards_Len = length(GuildAwards), 
    Bin_GuildAwards = list_to_binary(BinList_GuildAwards),

    Data = <<
        Mrank:16,
        Grank:16,
        RoleAwards_Len:16, Bin_RoleAwards/binary,
        GuildAwards_Len:16, Bin_GuildAwards/binary
    >>,
    {ok, pt:encode(40605, Data, Src_Data)};

write (40606,[
    MonState,
    Etime
] = Src_Data) ->
    Data = <<
        MonState:8,
        Etime:32
    >>,
    {ok, pt:encode(40606, Data, Src_Data)};

write (40607,[
    FreeTimes,
    CostTimes,
    GuildTimes
] = Src_Data) ->
    Data = <<
        FreeTimes:16,
        CostTimes:16,
        GuildTimes:16
    >>,
    {ok, pt:encode(40607, Data, Src_Data)};

write (40608,[
    Result,
    Opty
] = Src_Data) ->
    Data = <<
        Result:32,
        Opty:8
    >>,
    {ok, pt:encode(40608, Data, Src_Data)};

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

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

write (40611,[
    Boss,
    Click
] = Src_Data) ->
    Data = <<
        Boss:32,
        Click:8
    >>,
    {ok, pt:encode(40611, Data, Src_Data)};

write (40612,[
    Head,
    Times,
    Total
] = Src_Data) ->
    Bin_Head = pt:write_picture(Head), 

    Data = <<
        Bin_Head/binary,
        Times:8,
        Total:16
    >>,
    {ok, pt:encode(40612, Data, Src_Data)};

write (40613,[
    StartTime,
    RollList
] = Src_Data) ->
    BinList_RollList = [
        item_to_bin_4(RollList_Item) || RollList_Item <- RollList
    ], 

    RollList_Len = length(RollList), 
    Bin_RollList = list_to_binary(BinList_RollList),

    Data = <<
        StartTime:32,
        RollList_Len:16, Bin_RollList/binary
    >>,
    {ok, pt:encode(40613, Data, Src_Data)};

write (40614,[
    Result,
    Option,
    OrderId,
    RollNum,
    MaxRoll
] = Src_Data) ->
    Data = <<
        Result:32,
        Option:8,
        OrderId:16,
        RollNum:8,
        MaxRoll:8
    >>,
    {ok, pt:encode(40614, Data, Src_Data)};

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

write (40616,[
    OrderId,
    RoleList
] = Src_Data) ->
    BinList_RoleList = [
        item_to_bin_5(RoleList_Item) || RoleList_Item <- RoleList
    ], 

    RoleList_Len = length(RoleList), 
    Bin_RoleList = list_to_binary(BinList_RoleList),

    Data = <<
        OrderId:16,
        RoleList_Len:16, Bin_RoleList/binary
    >>,
    {ok, pt:encode(40616, Data, Src_Data)};

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


item_to_bin_0 ({
    Rank,
    Name,
    Hurt
}) ->
    Bin_Name = pt:write_string(Name), 

    Data = <<
        Rank:32,
        Bin_Name/binary,
        Hurt:64
    >>,
    Data.
item_to_bin_1 ({
    Rank,
    Gname,
    Hurt
}) ->
    Bin_Gname = pt:write_string(Gname), 

    Data = <<
        Rank:32,
        Bin_Gname/binary,
        Hurt:64
    >>,
    Data.
item_to_bin_2 ({
    Goodsid,
    Num
}) ->
    Data = <<
        Goodsid:32,
        Num:16
    >>,
    Data.
item_to_bin_3 ({
    Goodsid,
    Num
}) ->
    Data = <<
        Goodsid:32,
        Num:16
    >>,
    Data.
item_to_bin_4 ({
    OrderId,
    GoodsTypeId,
    Num,
    RollNum
}) ->
    Data = <<
        OrderId:16,
        GoodsTypeId:32,
        Num:16,
        RollNum:8
    >>,
    Data.
item_to_bin_5 ({
    RoleId,
    RoleName,
    RollNum
}) ->
    Bin_RoleName = pt:write_string(RoleName), 

    Data = <<
        RoleId:64,
        Bin_RoleName/binary,
        RollNum:8
    >>,
    Data.
