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

read(10201, _) ->
    {ok, []};
read(10202, Bin0) ->
    <<Type:8, _Bin1/binary>> = Bin0, 
    {ok, [Type]};
read(10203, Bin0) ->
    <<Type:8, Bin1/binary>> = Bin0, 
    <<Subtype:8, Bin2/binary>> = Bin1, 
    <<IsOpen:8, _Bin3/binary>> = Bin2, 
    {ok, [Type, Subtype, IsOpen]};
read(10204, Bin0) ->
    <<ClientVer:64, _Bin1/binary>> = Bin0, 
    {ok, [ClientVer]};
read(10206, Bin0) ->
    <<Opty:8, _Bin1/binary>> = Bin0, 
    {ok, [Opty]};
read(10210, _) ->
    {ok, []};
read(10211, Bin0) ->
    <<Id:16, Bin1/binary>> = Bin0, 
    <<State:8, _Bin2/binary>> = Bin1, 
    {ok, [Id, State]};
read(_Cmd, _R) -> {error, no_match}.

write (10201,[
    OpenTime,
    MergeTime,
    MergeStartTime,
    MergeCount,
    Unixdate,
    WorldLv
] = Src_Data) ->
    Data = <<
        OpenTime:32,
        MergeTime:32,
        MergeStartTime:32,
        MergeCount:32,
        Unixdate:32,
        WorldLv:32
    >>,
    {ok, pt:encode(10201, Data, Src_Data)};

write (10202,[
    Type,
    SettingList
] = Src_Data) ->
    BinList_SettingList = [
        item_to_bin_0(SettingList_Item) || SettingList_Item <- SettingList
    ], 

    SettingList_Len = length(SettingList), 
    Bin_SettingList = list_to_binary(BinList_SettingList),

    Data = <<
        Type:8,
        SettingList_Len:16, Bin_SettingList/binary
    >>,
    {ok, pt:encode(10202, Data, Src_Data)};

write (10203,[
    ErrorCode,
    Type,
    Subtype,
    IsOpen
] = Src_Data) ->
    Data = <<
        ErrorCode:32,
        Type:8,
        Subtype:8,
        IsOpen:8
    >>,
    {ok, pt:encode(10203, Data, Src_Data)};

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

write (10205,[
    ErrorCode,
    Args
] = Src_Data) ->
    Bin_Args = pt:write_string(Args), 

    Data = <<
        ErrorCode:32,
        Bin_Args/binary
    >>,
    {ok, pt:encode(10205, Data, Src_Data)};

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

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

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

write (10209,[
    Killer,
    Deader,
    Type
] = Src_Data) ->
    Bin_Killer = pt:write_picture(Killer), 

    Bin_Deader = pt:write_picture(Deader), 

    Data = <<
        Bin_Killer/binary,
        Bin_Deader/binary,
        Type:8
    >>,
    {ok, pt:encode(10209, Data, Src_Data)};

write (10210,[
    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(10210, Data, Src_Data)};

write (10211,[
    Id,
    State
] = Src_Data) ->
    Data = <<
        Id:16,
        State:8
    >>,
    {ok, pt:encode(10211, Data, Src_Data)};

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


item_to_bin_0 ({
    Subtype,
    IsOpen
}) ->
    Data = <<
        Subtype:8,
        IsOpen:8
    >>,
    Data.
item_to_bin_1 ({
    Id,
    State
}) ->
    Data = <<
        Id:16,
        State:8
    >>,
    Data.
