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

read(42101, _) ->
    {ok, []};
read(42102, Bin0) ->
    <<Type:8, Bin1/binary>> = Bin0, 
    FunArray0 = fun(<<RestBin0/binary>>) -> 
        <<GoodsId:32, _Args1/binary>> = RestBin0, 
        {GoodsId,_Args1}
        end,
    {GoodsList, _Bin2} = pt:read_array(FunArray0, Bin1),

    {ok, [Type, GoodsList]};
read(42103, Bin0) ->
    <<GoodsId:32, _Bin1/binary>> = Bin0, 
    {ok, [GoodsId]};
read(42104, _) ->
    {ok, []};
read(42105, Bin0) ->
    <<SuitId:16, _Bin1/binary>> = Bin0, 
    {ok, [SuitId]};
read(42106, _) ->
    {ok, []};
read(42107, Bin0) ->
    <<Pos:8, _Bin1/binary>> = Bin0, 
    {ok, [Pos]};
read(42108, _) ->
    {ok, []};
read(_Cmd, _R) -> {error, no_match}.

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

write (42101,[
    TargetList
] = Src_Data) ->
    BinList_TargetList = [
        item_to_bin_0(TargetList_Item) || TargetList_Item <- TargetList
    ], 

    TargetList_Len = length(TargetList), 
    Bin_TargetList = list_to_binary(BinList_TargetList),

    Data = <<
        TargetList_Len:16, Bin_TargetList/binary
    >>,
    {ok, pt:encode(42101, Data, Src_Data)};

write (42102,[
    Type,
    TargetList
] = Src_Data) ->
    BinList_TargetList = [
        item_to_bin_1(TargetList_Item) || TargetList_Item <- TargetList
    ], 

    TargetList_Len = length(TargetList), 
    Bin_TargetList = list_to_binary(BinList_TargetList),

    Data = <<
        Type:8,
        TargetList_Len:16, Bin_TargetList/binary
    >>,
    {ok, pt:encode(42102, Data, Src_Data)};

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

write (42104,[
    SuitList
] = Src_Data) ->
    BinList_SuitList = [
        item_to_bin_2(SuitList_Item) || SuitList_Item <- SuitList
    ], 

    SuitList_Len = length(SuitList), 
    Bin_SuitList = list_to_binary(BinList_SuitList),

    Data = <<
        SuitList_Len:16, Bin_SuitList/binary
    >>,
    {ok, pt:encode(42104, Data, Src_Data)};

write (42105,[
    SuitId,
    SumStar,
    OwnNum,
    ActiveNum
] = Src_Data) ->
    Data = <<
        SuitId:16,
        SumStar:16,
        OwnNum:8,
        ActiveNum:8
    >>,
    {ok, pt:encode(42105, Data, Src_Data)};

write (42106,[
    GoodsList,
    RefreshCount,
    LastRefreshTime,
    NeedCountDown
] = Src_Data) ->
    BinList_GoodsList = [
        item_to_bin_3(GoodsList_Item) || GoodsList_Item <- GoodsList
    ], 

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

    Data = <<
        GoodsList_Len:16, Bin_GoodsList/binary,
        RefreshCount:8,
        LastRefreshTime:32,
        NeedCountDown:8
    >>,
    {ok, pt:encode(42106, Data, Src_Data)};

write (42107,[
    GoodsList
] = Src_Data) ->
    BinList_GoodsList = [
        item_to_bin_4(GoodsList_Item) || GoodsList_Item <- GoodsList
    ], 

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

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

write (42108,[
    GoodsList,
    RefreshCount,
    LastRefreshTime,
    NeedCountDown
] = Src_Data) ->
    BinList_GoodsList = [
        item_to_bin_5(GoodsList_Item) || GoodsList_Item <- GoodsList
    ], 

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

    Data = <<
        GoodsList_Len:16, Bin_GoodsList/binary,
        RefreshCount:8,
        LastRefreshTime:32,
        NeedCountDown:8
    >>,
    {ok, pt:encode(42108, Data, Src_Data)};

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


item_to_bin_0 ({
    Pos,
    GoodsId
}) ->
    Data = <<
        Pos:8,
        GoodsId:32
    >>,
    Data.
item_to_bin_1 ({
    Pos,
    GoodsId
}) ->
    Data = <<
        Pos:8,
        GoodsId:32
    >>,
    Data.
item_to_bin_2 ({
    SuitId,
    SumStar,
    OwnNum,
    ActiveNum
}) ->
    Data = <<
        SuitId:16,
        SumStar:16,
        OwnNum:8,
        ActiveNum:8
    >>,
    Data.
item_to_bin_3 ({
    Pos,
    GoodsId,
    HasBuy
}) ->
    Data = <<
        Pos:8,
        GoodsId:32,
        HasBuy:8
    >>,
    Data.
item_to_bin_4 ({
    Pos,
    GoodsId,
    HasBuy
}) ->
    Data = <<
        Pos:8,
        GoodsId:32,
        HasBuy:8
    >>,
    Data.
item_to_bin_5 ({
    Pos,
    GoodsId,
    HasBuy
}) ->
    Data = <<
        Pos:8,
        GoodsId:32,
        HasBuy:8
    >>,
    Data.
