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

read(14100, Bin0) ->
    <<SubType:16, _Bin1/binary>> = Bin0, 
    {ok, [SubType]};
read(14101, Bin0) ->
    <<SubType:16, Bin1/binary>> = Bin0, 
    <<Opty:8, Bin2/binary>> = Bin1, 
    <<Num:8, _Bin3/binary>> = Bin2, 
    {ok, [SubType, Opty, Num]};
read(14102, Bin0) ->
    <<SubType:16, Bin1/binary>> = Bin0, 
    <<Opty:8, Bin2/binary>> = Bin1, 
    <<Dot:8, _Bin3/binary>> = Bin2, 
    {ok, [SubType, Opty, Dot]};
read(_Cmd, _R) -> {error, no_match}.

write (14100,[
    SubType,
    ServerPro,
    RolePro,
    ServerAwards,
    Buffs,
    RoleAwards
] = Src_Data) ->
    BinList_ServerAwards = [
        item_to_bin_0(ServerAwards_Item) || ServerAwards_Item <- ServerAwards
    ], 

    ServerAwards_Len = length(ServerAwards), 
    Bin_ServerAwards = list_to_binary(BinList_ServerAwards),

    BinList_Buffs = [
        item_to_bin_1(Buffs_Item) || Buffs_Item <- Buffs
    ], 

    Buffs_Len = length(Buffs), 
    Bin_Buffs = list_to_binary(BinList_Buffs),

    BinList_RoleAwards = [
        item_to_bin_3(RoleAwards_Item) || RoleAwards_Item <- RoleAwards
    ], 

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

    Data = <<
        SubType:16,
        ServerPro:16,
        RolePro:16,
        ServerAwards_Len:16, Bin_ServerAwards/binary,
        Buffs_Len:16, Bin_Buffs/binary,
        RoleAwards_Len:16, Bin_RoleAwards/binary
    >>,
    {ok, pt:encode(14100, Data, Src_Data)};

write (14101,[
    Errcode,
    SubType,
    ServerPro,
    RolePro
] = Src_Data) ->
    Data = <<
        Errcode:32,
        SubType:16,
        ServerPro:16,
        RolePro:16
    >>,
    {ok, pt:encode(14101, Data, Src_Data)};

write (14102,[
    Errcode,
    SubType,
    Opty,
    Dot
] = Src_Data) ->
    Data = <<
        Errcode:32,
        SubType:16,
        Opty:8,
        Dot:8
    >>,
    {ok, pt:encode(14102, Data, Src_Data)};

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

write (14104,[
    SubType,
    Dot,
    Bid,
    Stime,
    Etime
] = Src_Data) ->
    Data = <<
        SubType:16,
        Dot:8,
        Bid:16,
        Stime:32,
        Etime:32
    >>,
    {ok, pt:encode(14104, Data, Src_Data)};

write (14105,[
    SubType,
    Dot,
    Bid,
    Stime,
    Etime,
    Used
] = Src_Data) ->
    BinList_Used = [
        item_to_bin_4(Used_Item) || Used_Item <- Used
    ], 

    Used_Len = length(Used), 
    Bin_Used = list_to_binary(BinList_Used),

    Data = <<
        SubType:16,
        Dot:8,
        Bid:16,
        Stime:32,
        Etime:32,
        Used_Len:16, Bin_Used/binary
    >>,
    {ok, pt:encode(14105, Data, Src_Data)};

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


item_to_bin_0 (
    Dot
) ->
    Data = <<
        Dot:8
    >>,
    Data.
item_to_bin_1 ({
    Dot,
    Bid,
    Stime,
    Etime,
    Used
}) ->
    BinList_Used = [
        item_to_bin_2(Used_Item) || Used_Item <- Used
    ], 

    Used_Len = length(Used), 
    Bin_Used = list_to_binary(BinList_Used),

    Data = <<
        Dot:8,
        Bid:16,
        Stime:32,
        Etime:32,
        Used_Len:16, Bin_Used/binary
    >>,
    Data.
item_to_bin_2 ({
    Kdata,
    Vdata
}) ->
    Data = <<
        Kdata:8,
        Vdata:8
    >>,
    Data.
item_to_bin_3 (
    Dot
) ->
    Data = <<
        Dot:8
    >>,
    Data.
item_to_bin_4 ({
    Kdata,
    Vdata
}) ->
    Data = <<
        Kdata:8,
        Vdata:8
    >>,
    Data.
