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

read(11100, _) ->
    {ok, []};
read(11101, Bin0) ->
    {Command, _Bin1} = pt:read_string(Bin0),
    {ok, [Command]};
read(_Cmd, _R) -> {error, no_match}.

write (11100,[
    CommandList
]) ->
    BinList_CommandList = [
        item_to_bin_0(CommandList_Item) || CommandList_Item <- CommandList
    ],

    CommandList_Len = length(CommandList),
    Bin_CommandList = list_to_binary(BinList_CommandList),

    Data = <<
        CommandList_Len:16, Bin_CommandList/binary
    >>,
    {ok, pt:pack(11100, Data)};



write (11105,[
    CtrlId
]) ->
    Data = <<
        CtrlId:8
    >>,
    {ok, pt:pack(11105, Data)};

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


item_to_bin_0 ({
    Command,
    CommandName,
    ArgsList
}) ->
    Bin_Command = pt:write_string(Command),

    Bin_CommandName = pt:write_string(CommandName),

    BinList_ArgsList = [
        item_to_bin_1(ArgsList_Item) || ArgsList_Item <- ArgsList
    ],

    ArgsList_Len = length(ArgsList),
    Bin_ArgsList = list_to_binary(BinList_ArgsList),

    Data = <<
        Bin_Command/binary,
        Bin_CommandName/binary,
        ArgsList_Len:16, Bin_ArgsList/binary
    >>,
    Data.
item_to_bin_1 (
    Args
) ->
    Bin_Args = pt:write_string(Args),

    Data = <<
        Bin_Args/binary
    >>,
    Data.
