%% ---------------------------------------------------------------------------
%% @doc 拍卖行
%% @author hek
%% @since  2016-11-14
%% @deprecated
%% ---------------------------------------------------------------------------

-module(lib_auction_api).
-include("common.hrl").
-include("rec_auction.hrl").
-include("server.hrl").
-include("def_timer_quest.hrl").

-export([
		start_guild_auction/3,  		%% 开启公会拍卖
		add_join/4,  					%% 添加参与记录(可以多次调用)
		quit_guild/1,  					%% 退出公会，清除活动参与记录
		start_guild_auction_gm/4 		%% 开启公会拍卖（GM使用）
]).

%%% Usage:
%%% 1. 玩家参与活动：调用参与活动记录接口，用于获得参与分红
%%% lib_auction_api:add_join(PlayerId, GuildId, ModuleId) (可以多次调用)
%%% 2. 活动结束开启公会拍卖：
%%% lib_auction_api:start_guild_auction(ModuleId, GuildGoodsList) (一个活动只可以调用一次)
%%% End.

%% ---------------------------------------------------------------------------
%% @doc 开启公会拍卖(一个活动只可以调用一次)
-spec start_guild_auction(ModuleId, SubModuleId, GuildGoodsList) -> Res when
	ModuleId  		:: integer(), 				%% 功能id
	SubModuleId 	:: integer(), 				%% 子功能id
	GuildGoodsList	:: [{GuildId, ObjectList}],	%% 公会物品列表
	GuildId 		:: integer(), 				%% 公会id
	ObjectList 		:: [{Type::integer(), GoodsId::integer(), Num::integer()}],
	Res 			:: ok.
%% ---------------------------------------------------------------------------
start_guild_auction(ModuleId, SubModuleId, GuildGoodsList) ->
	NewModuleId = lib_auction_data:make_module_id(ModuleId, SubModuleId),
	mod_guild:apply_cast(mod_auction, start_guild_auction, [NewModuleId, GuildGoodsList]),
	ok.

%% ---------------------------------------------------------------------------
%% @doc 添加参与记录(可以多次调用)
%% ---------------------------------------------------------------------------
add_join(PlayerId, GuildId, ModuleId, SubModuleId) ->
	NewModuleId = lib_auction_data:make_module_id(ModuleId, SubModuleId),
	lib_act_join_api:add_join(PlayerId, GuildId, NewModuleId),
	ok.

%% ---------------------------------------------------------------------------
%% @doc 退出公会，清除活动参与记录
%% ---------------------------------------------------------------------------
quit_guild(PlayerId) ->
	mod_auction:quit_guild({PlayerId}),
	lib_act_join_api:quit_guild(PlayerId),
	ok.

%% ---------------------------------------------------------------------------
%% @doc 开启公会拍卖（GM使用）
%% ---------------------------------------------------------------------------
start_guild_auction_gm(Time, ModuleId, SubModuleId, GuildGoodsList) ->
	Duration = Time - utime:unixtime(),
	do_start_guild_auction_gm(Duration, ModuleId, SubModuleId, GuildGoodsList).

do_start_guild_auction_gm(Duration, ModuleId, SubModuleId, GuildGoodsList) when Duration> 0 ->
	NewModuleId = lib_auction_data:make_module_id(ModuleId, SubModuleId),
	QuestArgs = {lib_auction_api, start_guild_auction, [ModuleId, SubModuleId, GuildGoodsList]},
	timer_quest:delete(?UTIMER_ID(?TIMER_AUCTION_GM, NewModuleId)),
	timer_quest:add(?UTIMER_ID(?TIMER_AUCTION_GM, NewModuleId), Duration, 0, QuestArgs),
	ok;
do_start_guild_auction_gm(Duration, ModuleId, SubModuleId, GuildGoodsList) ->
	?ERR("start_guild_auction_gm error duration:~p~n", [{Duration, ModuleId, SubModuleId, GuildGoodsList}]),
	error.

