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

read(16001, _) ->
    {ok, []};
read(16002, _) ->
    {ok, []};
read(16003, _) ->
    {ok, []};
read(16004, Bin0) ->
    <<Id:8, _Bin1/binary>> = Bin0, 
    {ok, [Id]};
read(16005, _) ->
    {ok, []};
read(16006, Bin0) ->
    <<No:8, _Bin1/binary>> = Bin0, 
    {ok, [No]};
read(16007, _) ->
    {ok, []};
read(16008, _) ->
    {ok, []};
read(16009, Bin0) ->
    {Code, Bin1} = pt:read_string(Bin0), 
    <<GoodsId:64, _Bin2/binary>> = Bin1, 
    {ok, [Code, GoodsId]};
read(16010, Bin0) ->
    <<No:8, _Bin1/binary>> = Bin0, 
    {ok, [No]};
read(16011, _) ->
    {ok, []};
read(_Cmd, _R) -> {error, no_match}.

write (16001,[
    Round,
    Days,
    SignInStatus,
    CumulativeInfo
] = Src_Data) ->
    Data = <<
        Round:8,
        Days:8,
        SignInStatus:8,
        CumulativeInfo:16
    >>,
    {ok, pt:encode(16001, Data, Src_Data)};

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

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

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

write (16005,[
    ReceiveState,
    LimitState
] = Src_Data) ->
    BinList_LimitState = [
        item_to_bin_0(LimitState_Item) || LimitState_Item <- LimitState
    ], 

    LimitState_Len = length(LimitState), 
    Bin_LimitState = list_to_binary(BinList_LimitState),

    Data = <<
        ReceiveState:64,
        LimitState_Len:16, Bin_LimitState/binary
    >>,
    {ok, pt:encode(16005, Data, Src_Data)};

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

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

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

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

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

write (16011,[
    Number,
    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 = <<
        Number:16,
        List_Len:16, Bin_List/binary
    >>,
    {ok, pt:encode(16011, Data, Src_Data)};

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


item_to_bin_0 ({
    Id,
    Number
}) ->
    Data = <<
        Id:8,
        Number:16
    >>,
    Data.
item_to_bin_1 (
    Picture
) ->
    Bin_Picture = pt:write_picture(Picture), 

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