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

read(15400, _) ->
    {ok, []};
read(15401, Bin0) ->
    <<AuctionType:32, Bin1/binary>> = Bin0,
    <<Type:32, Bin2/binary>> = Bin1,
    <<ModuleId:32, _Bin3/binary>> = Bin2,
    {ok, [AuctionType, Type, ModuleId]};
read(15403, Bin0) ->
    <<GoodsId:64, Bin1/binary>> = Bin0,
    <<PriceType:8, Bin2/binary>> = Bin1,
    <<Bgold:32, Bin3/binary>> = Bin2,
    <<Gold:32, _Bin4/binary>> = Bin3,
    {ok, [GoodsId, PriceType, Bgold, Gold]};
read(15405, Bin0) ->
    <<Type:8, _Bin1/binary>> = Bin0,
    {ok, [Type]};
read(15406, Bin0) ->
    <<AuctionType:32, _Bin1/binary>> = Bin0,
    {ok, [AuctionType]};
read(15407, _) ->
    {ok, []};
read(_Cmd, _R) -> {error, no_match}.

write (15400,[
    ModuleList,
    TypeList
]) ->
    BinList_ModuleList = [
        item_to_bin_0(ModuleList_Item) || ModuleList_Item <- ModuleList
    ],

    ModuleList_Len = length(ModuleList),
    Bin_ModuleList = list_to_binary(BinList_ModuleList),

    BinList_TypeList = [
        item_to_bin_1(TypeList_Item) || TypeList_Item <- TypeList
    ],

    TypeList_Len = length(TypeList),
    Bin_TypeList = list_to_binary(BinList_TypeList),

    Data = <<
        ModuleList_Len:16, Bin_ModuleList/binary,
        TypeList_Len:16, Bin_TypeList/binary
    >>,
    {ok, pt:pack(15400, Data)};

write (15401,[
    AuctionType,
    AuctionStatus,
    StartTime,
    GoodsList
]) ->
    BinList_GoodsList = [
        item_to_bin_2(GoodsList_Item) || GoodsList_Item <- GoodsList
    ],

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

    Data = <<
        AuctionType:32,
        AuctionStatus:8,
        StartTime:32,
        GoodsList_Len:16, Bin_GoodsList/binary
    >>,
    {ok, pt:pack(15401, Data)};

write (15402,[
    GoodsId,
    NowPrice,
    NextPrice,
    Time,
    TopPlayerId,
    IsDelay,
    GoodsStatus
]) ->
    Data = <<
        GoodsId:64,
        NowPrice:32,
        NextPrice:32,
        Time:32,
        TopPlayerId:64,
        IsDelay:8,
        GoodsStatus:8
    >>,
    {ok, pt:pack(15402, Data)};

write (15403,[
    Res,
    PriceType
]) ->
    Data = <<
        Res:32,
        PriceType:8
    >>,
    {ok, pt:pack(15403, Data)};

write (15404,[
    AuctionType,
    Type,
    ModuleId,
    GoodsId,
    TypeId,
    GoodsNum,
    Bgold,
    Gold
]) ->
    Data = <<
        AuctionType:32,
        Type:32,
        ModuleId:32,
        GoodsId:64,
        TypeId:32,
        GoodsNum:32,
        Bgold:32,
        Gold:32
    >>,
    {ok, pt:pack(15404, Data)};

write (15405,[
    Type,
    GoodsList
]) ->
    BinList_GoodsList = [
        item_to_bin_3(GoodsList_Item) || GoodsList_Item <- GoodsList
    ],

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

    Data = <<
        Type:8,
        GoodsList_Len:16, Bin_GoodsList/binary
    >>,
    {ok, pt:pack(15405, Data)};

write (15406,[
    AuctionType,
    AuctionList
]) ->
    BinList_AuctionList = [
        item_to_bin_4(AuctionList_Item) || AuctionList_Item <- AuctionList
    ],

    AuctionList_Len = length(AuctionList),
    Bin_AuctionList = list_to_binary(BinList_AuctionList),

    Data = <<
        AuctionType:32,
        AuctionList_Len:16, Bin_AuctionList/binary
    >>,
    {ok, pt:pack(15406, Data)};

write (15407,[
    EstimateBonus
]) ->
    Data = <<
        EstimateBonus:32
    >>,
    {ok, pt:pack(15407, Data)};

write (15408,[
    ModuleId,
    Type
]) ->
    Data = <<
        ModuleId:32,
        Type:8
    >>,
    {ok, pt:pack(15408, Data)};

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


item_to_bin_0 ({
    ModuleId,
    Num
}) ->
    Data = <<
        ModuleId:32,
        Num:32
    >>,
    Data.
item_to_bin_1 ({
    Type,
    Num
}) ->
    Data = <<
        Type:32,
        Num:32
    >>,
    Data.
item_to_bin_2 ({
    GoodsId,
    TypeId,
    GoodsNum,
    MoneyType,
    NowPrice,
    NextPrice,
    Price,
    Time,
    PlayerId,
    TopPlayerId,
    IsDelay,
    TaxRate
}) ->
    Data = <<
        GoodsId:64,
        TypeId:32,
        GoodsNum:32,
        MoneyType:8,
        NowPrice:32,
        NextPrice:32,
        Price:32,
        Time:32,
        PlayerId:64,
        TopPlayerId:64,
        IsDelay:8,
        TaxRate:8
    >>,
    Data.
item_to_bin_3 ({
    GoodsId,
    TypeId,
    GoodsNum,
    MoneyType,
    NowPrice,
    NextPrice,
    Price,
    Time,
    PlayerId,
    TopPlayerId,
    IsDelay,
    TaxRate
}) ->
    Data = <<
        GoodsId:64,
        TypeId:32,
        GoodsNum:32,
        MoneyType:8,
        NowPrice:32,
        NextPrice:32,
        Price:32,
        Time:32,
        PlayerId:64,
        TopPlayerId:64,
        IsDelay:8,
        TaxRate:8
    >>,
    Data.
item_to_bin_4 ({
    ModuleId,
    Time,
    TypeId,
    GoodsNum,
    MoneyType,
    Price,
    ToWorld
}) ->
    Data = <<
        ModuleId:32,
        Time:32,
        TypeId:32,
        GoodsNum:32,
        MoneyType:8,
        Price:32,
        ToWorld:8
    >>,
    Data.
