%% ---------------------------------------------------------------------------
%% @doc 交易市场
%% @author zhaoyu
%% @since  2019-12-26
%% ---------------------------------------------------------------------------
-module(pp_market).

-include("server.hrl").
-include("errcode.hrl").
-include("common.hrl").
-include("predefine.hrl").
-include("figure.hrl").
-include("market.hrl").

-export([
        handle/3
]).

%% 协议处理
handle(Cmd, Role, ProtoData) ->
    #player_status{figure = #figure{lv = RoleLev}} = Role,
    OpenLev = case data_market:get_config(open_lv) of
        OpenLv when is_integer(OpenLv) -> OpenLv;
        _ -> 90
    end,
    case RoleLev >= OpenLev of
        true ->
            handle_do(Cmd, Role, ProtoData);
        false ->
            {ok, Role}
    end.


%% 市场商品一级列表
handle_do(15501, Role, [
        SellType, Type, SubType, Series, Color, Career, PageSize, PageNo, SortId, SortOrder]
) ->
    market:send_market_goods(
        Role, SellType, Type, SubType, Series, Color, Career, PageSize, PageNo, SortId, SortOrder
    );

%% 发送玩家正在竞价的商品列表
handle_do(15502, Role, _) ->
    market:send_role_bid_goods_info(Role);

%% 发送玩家已上架物品列表
handle_do(15503, Role, _) ->
    market:send_role_market_goods(Role);

%% 成交记录
handle_do(15504, Role, [LogType]) ->
    market:send_deal_log(Role, LogType);

%% 商品上架 pt_15506_[554050792087,24101013,1,1,0]
handle_do(15506, Role, [GoodsId, GoodsTypeId, Num, SellType, IsToWorld]) ->
    #player_status{sid = Sid} = Role,
    {
        Reason, NewRole
    } = case market:put_on_goods(Role, GoodsId, GoodsTypeId, Num, SellType, IsToWorld) of
        {false, Rea} -> {Rea, Role};
        {true, _MarketGoods, Role2} ->
            lib_task_api:put_on_goods(Role),
            {?SUCCESS, Role2}
    end,
    {ok, BinData} = pt_155:write(15506, [GoodsId, Reason]),
    lib_server_send:send_to_sid(Sid, BinData),
    {ok, NewRole};

%% 竞拍物品
handle_do(15507, Role, [MarketId, Price]) ->
    #player_status{sid = Sid} = Role,
    case market:bid_for_goods(Role, MarketId, Price) of
        {false, Rea} ->
            {ok, BinData} = pt_155:write(15507, [Rea, MarketId, Price]),
            lib_server_send:send_to_sid(Sid, BinData);
        ok ->
            lib_task_api:bid_for_goods(Role),
            ok
    end,
    {ok, Role};

%% 商品下架
handle_do(15508, Role, [MarketId]) ->
    #player_status{sid = Sid} = Role,
    case market:put_goods_down(Role, MarketId) of
        {false, Rea} ->
            {ok, BinData} = pt_155:write(15508, [Rea]),
            lib_server_send:send_to_sid(Sid, BinData),
            {ok, Role};
        {true, Role2} ->
            {ok, Role2}
    end;

%% 商品再上架
handle_do(15509, Role, [MarketId, SellType, IsToWorld]) ->
    #player_status{sid = Sid} = Role,
    {
        Reason, NewRole
    } = case market:put_on_goods_again(Role, MarketId, SellType, IsToWorld) of
        {false, Rea} -> {Rea, Role};
        {true, _MarketGoods, Role2} -> {?SUCCESS, Role2}
    end,
    {ok, BinData} = pt_155:write(15509, [Reason]),
    lib_server_send:send_to_sid(Sid, BinData),
    {ok, NewRole};

%% 关注商品
handle_do(15510, Role, [MarketId]) ->
    #player_status{sid = Sid} = Role,
    {Reason, NewRole} = case market:like_market_goods(Role, MarketId) of
        {false, Rea} -> {Rea, Role};
        {ok, Role2} -> {?SUCCESS, Role2}
    end,
    {ok, BinData} = pt_155:write(15510, [MarketId, Reason]),
    lib_server_send:send_to_sid(Sid, BinData),
    {ok, NewRole};

%% 取消关注商品
handle_do(15511, Role, [MarketId]) ->
    #player_status{sid = Sid} = Role,
    {Reason, NewRole} = case market:del_like_market_goods(Role, MarketId) of
        {false, Rea} -> {Rea, Role};
        {ok, Role2} -> {?SUCCESS, Role2}
    end,
    {ok, BinData} = pt_155:write(15511, [MarketId, Reason]),
    lib_server_send:send_to_sid(Sid, BinData),
    {ok, NewRole};

%% 玩家关注的商品列表
handle_do(15512, Role, _) ->
    market:send_role_like_market_goods(Role);

%% 已添加的入库提醒
handle_do(15505, Role, _) ->
    #player_status{sid = Sid, market_data = MarketData} = Role,
    #market_data{reminder_list = ReminderList} = MarketData,
    {ok, BinData} = pt_155:write(15505, [ReminderList]),
    lib_server_send:send_to_sid(Sid, BinData),
    {ok, Role};

%% 添加上架提醒
handle_do(15513, Role, [GoodsTypeId]) ->
    #player_status{sid = Sid} = Role,
    {Reason, NewRole} = case market:add_reminder(Role, GoodsTypeId) of
        {false, Rea} -> {Rea, Role};
        {ok, Role2} -> {?SUCCESS, Role2}
    end,
    {ok, BinData} = pt_155:write(15513, [GoodsTypeId, Reason]),
    lib_server_send:send_to_sid(Sid, BinData),
    {ok, NewRole};

%% 取消入库提醒
handle_do(15514, Role, [GoodsTypeId]) ->
    #player_status{sid = Sid} = Role,
    {Reason, NewRole} = case market:remove_reminder(Role, GoodsTypeId) of
        {false, Rea} -> {Rea, Role};
        {ok, Role2} -> {?SUCCESS, Role2}
    end,
    {ok, BinData} = pt_155:write(15514, [GoodsTypeId, Reason]),
    lib_server_send:send_to_sid(Sid, BinData),
    {ok, NewRole};

%% 一键取消提醒
handle_do(15516, Role, [Type, SubType]) ->
    #player_status{sid = Sid} = Role,
    {Reason, NewRole} = case market:remove_reminder_by_type(Role, Type, SubType) of
        {false, Rea} -> {Rea, Role};
        {ok, Role2} -> {?SUCCESS, Role2}
    end,
    {ok, BinData} = pt_155:write(15516, [Reason]),
    lib_server_send:send_to_sid(Sid, BinData),
    {ok, NewRole};


%% 商品再上架
handle_do(15519, Role, []) ->
    #player_status{sid = Sid} = Role,
    {
        Reason, NewRole
    } = case market:batch_put_goods_on_again(Role) of
            {false, Rea} ->
                {Rea, Role};
            {true, Role2} ->
                market:send_role_market_goods(Role2),
                {?SUCCESS, Role2}
        end,
    {ok, BinData} = pt_155:write(15519, [Reason]),
    lib_server_send:send_to_sid(Sid, BinData),
    {ok, NewRole};

handle_do(_Cmd, _Status, _Data) ->
    {error, io_lib:format("~w no match", [?MODULE])}.


