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

read(41101, _) ->
    {ok, []};
read(41102, Bin0) ->
    <<Id:32, _Bin1/binary>> = Bin0, 
    {ok, [Id]};
read(41103, Bin0) ->
    <<Id:32, _Bin1/binary>> = Bin0, 
    {ok, [Id]};
read(41104, _) ->
    {ok, []};
read(41105, _) ->
    {ok, []};
read(41106, Bin0) ->
    <<GoodsId:32, Bin1/binary>> = Bin0, 
    <<Id:32, Bin2/binary>> = Bin1, 
    <<GoodsNum:16, _Bin3/binary>> = Bin2, 
    {ok, [GoodsId, Id, GoodsNum]};
read(41107, _) ->
    {ok, []};
read(_Cmd, _R) -> {error, no_match}.

write (41101,[
    Currentused,
    DsgtList
] = Src_Data) ->
    BinList_DsgtList = [
        item_to_bin_0(DsgtList_Item) || DsgtList_Item <- DsgtList
    ], 

    DsgtList_Len = length(DsgtList), 
    Bin_DsgtList = list_to_binary(BinList_DsgtList),

    Data = <<
        Currentused:32,
        DsgtList_Len:16, Bin_DsgtList/binary
    >>,
    {ok, pt:encode(41101, Data, Src_Data)};

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

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

write (41104,[
    Code,
    Id,
    EndTime
] = Src_Data) ->
    Data = <<
        Code:32,
        Id:32,
        EndTime:32
    >>,
    {ok, pt:encode(41104, Data, Src_Data)};

write (41105,[
    PlayerId,
    Id
] = Src_Data) ->
    Data = <<
        PlayerId:64,
        Id:32
    >>,
    {ok, pt:encode(41105, Data, Src_Data)};

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

write (41107,[
    GoodsId,
    PriceType,
    Price
] = Src_Data) ->
    Data = <<
        GoodsId:32,
        PriceType:8,
        Price:16
    >>,
    {ok, pt:encode(41107, Data, Src_Data)};

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


item_to_bin_0 ({
    Id,
    EndTime
}) ->
    Data = <<
        Id:32,
        EndTime:32
    >>,
    Data.
