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

read(14300, _) ->
    {ok, []};
read(14301, Bin0) ->
    <<GoodsId:64, Bin1/binary>> = Bin0, 
    <<Pos:8, _Bin2/binary>> = Bin1, 
    {ok, [GoodsId, Pos]};
read(14302, Bin0) ->
    <<GoodsId:64, _Bin1/binary>> = Bin0, 
    {ok, [GoodsId]};
read(14303, Bin0) ->
    <<GoodsId:64, Bin1/binary>> = Bin0, 
    <<Location:8, Bin2/binary>> = Bin1, 
    <<Count:8, _Bin3/binary>> = Bin2, 
    {ok, [GoodsId, Location, Count]};
read(14304, Bin0) ->
    <<GoodsId:64, Bin1/binary>> = Bin0, 
    <<Location:8, Bin2/binary>> = Bin1, 
    FunArray0 = fun(<<RestBin0/binary>>) -> 
        <<CostGoodsId:64, _Args1/binary>> = RestBin0, 
        {CostGoodsId,_Args1}
        end,
    {ExtraCost, _Bin3} = pt:read_array(FunArray0, Bin2),

    {ok, [GoodsId, Location, ExtraCost]};
read(14305, _) ->
    {ok, []};
read(14306, _) ->
    {ok, []};
read(14307, _) ->
    {ok, []};
read(14308, Bin0) ->
    <<GoodsId:64, Bin1/binary>> = Bin0, 
    <<Location:8, _Bin2/binary>> = Bin1, 
    {ok, [GoodsId, Location]};
read(14309, _) ->
    {ok, []};
read(14310, Bin0) ->
    <<Pos:8, _Bin1/binary>> = Bin0, 
    {ok, [Pos]};
read(_Cmd, _R) -> {error, no_match}.

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

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

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

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

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

write (14305,[
    Num,
    GoodsList
] = Src_Data) ->
    BinList_GoodsList = [
        item_to_bin_0(GoodsList_Item) || GoodsList_Item <- GoodsList
    ], 

    GoodsList_Len = length(GoodsList), 
    Bin_GoodsList = list_to_binary(BinList_GoodsList),

    Data = <<
        Num:16,
        GoodsList_Len:16, Bin_GoodsList/binary
    >>,
    {ok, pt:encode(14305, Data, Src_Data)};

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

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

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

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

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

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


item_to_bin_0 (
    GoodsId
) ->
    Data = <<
        GoodsId:32
    >>,
    Data.
item_to_bin_1 (
    Pos
) ->
    Data = <<
        Pos:8
    >>,
    Data.
