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

read(11200, Bin0) ->
    <<DressType:32, _Bin1/binary>> = Bin0, 
    {ok, [DressType]};
read(11201, Bin0) ->
    <<DressId:32, _Bin1/binary>> = Bin0, 
    {ok, [DressId]};
read(11202, Bin0) ->
    FunArray0 = fun(<<RestBin0/binary>>) -> 
        <<DressId:32, _Args1/binary>> = RestBin0, 
        {DressId,_Args1}
        end,
    {DressupList, _Bin1} = pt:read_array(FunArray0, Bin0),

    {ok, [DressupList]};
read(11203, Bin0) ->
    <<DressType:8, Bin1/binary>> = Bin0, 
    <<DressId:32, Bin2/binary>> = Bin1, 
    <<GoodsId:64, _Bin3/binary>> = Bin2, 
    {ok, [DressType, DressId, GoodsId]};
read(_Cmd, _R) -> {error, no_match}.

write (11200,[
    Res,
    DressType,
    EnableList,
    UseDressId
] = Src_Data) ->
    BinList_EnableList = [
        item_to_bin_0(EnableList_Item) || EnableList_Item <- EnableList
    ], 

    EnableList_Len = length(EnableList), 
    Bin_EnableList = list_to_binary(BinList_EnableList),

    Data = <<
        Res:32,
        DressType:8,
        EnableList_Len:16, Bin_EnableList/binary,
        UseDressId:32
    >>,
    {ok, pt:encode(11200, Data, Src_Data)};

write (11201,[
    Res,
    DressId,
    DressType
] = Src_Data) ->
    Data = <<
        Res:32,
        DressId:32,
        DressType:8
    >>,
    {ok, pt:encode(11201, Data, Src_Data)};

write (11202,[
    Res,
    DressupList
] = Src_Data) ->
    BinList_DressupList = [
        item_to_bin_1(DressupList_Item) || DressupList_Item <- DressupList
    ], 

    DressupList_Len = length(DressupList), 
    Bin_DressupList = list_to_binary(BinList_DressupList),

    Data = <<
        Res:32,
        DressupList_Len:16, Bin_DressupList/binary
    >>,
    {ok, pt:encode(11202, Data, Src_Data)};

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

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


item_to_bin_0 (
    DressId
) ->
    Data = <<
        DressId:32
    >>,
    Data.
item_to_bin_1 ({
    DressId,
    DressType
}) ->
    Data = <<
        DressId:32,
        DressType:8
    >>,
    Data.
