%%%-------------------------------------------------------------------
%%% @doc     精英考核(任务考核)
%%% Module  : lib_elite_assess_api.erl
%%% Created : 2020-07-13
%%% @Author : tyl
%%%-------------------------------------------------------------------
-module(lib_elite_assess_api).

-include("server.hrl").
-include("def_event.hrl").
-include("def_module.hrl").
-include("def_goods.hrl").
-include("predefine.hrl").
-include("common.hrl").
-include("role.hrl").
-include("figure.hrl").
-include("elite_assessment.hrl").
-include("rec_event.hrl").
-include("dungeon.hrl").

-compile(export_all).


%% 异步触发
async_event(PlayerId, M, F, A) when is_integer(PlayerId) andalso PlayerId > 16#FFFF -> %% 怪物击杀要过滤怪物击杀者
	lib_player:apply_cast(PlayerId, ?APPLY_CAST_SAVE, M, F, [A]);
async_event(_PlayerId, _M, _F, _A) -> skip.


%% 等级提升功能开放(已废弃)
handle_event(PS, #event_callback{type_id = ?EVENT_LV_UP}) when is_record(PS, player_status)  ->
	#player_status{figure = Figure} = PS,
	OpenModuleL = get_open_modules(Figure#figure.lv),
	OpenSubModL = get_open_sub_modules(Figure#figure.lv),
	AllOpenModL = OpenModuleL ++ OpenSubModL,
	case AllOpenModL =/= [] of
		true ->
			%% 功能开放任务
			F = fun({TModule, TSubModule}) ->
				FTaskId =
				case TSubModule == 0 of
					true ->
						case data_module:get_pre_task_id(TModule) of
							TaskId when TaskId > 0 -> TaskId;
							_ -> 0
						end;
					_ ->
						case data_module:get_sub_pre_task_id(TModule, TSubModule) of
							TaskId1 when TaskId1 > 0 -> TaskId1;
							_ -> 0
						end
				end,
				case FTaskId > 0 of
					true ->
						case lib_role_task:is_finish_task_id(FTaskId) of
							true ->
								open_func_event(PS, [{TModule, TSubModule}]);
							_ ->
								skip
						end;
					_ ->
						open_func_event(PS, [{TModule, TSubModule}])
				end
			end,
			lists:map(F, AllOpenModL);
		_ ->
			skip
	end,
	%% 等级任务
	lv_up_event(PS, 0),
	{ok, PS};

%% 完成任务触发(已废弃)
handle_event(PS, #event_callback{type_id = ?EVENT_FINISH_TASK, data = FinTaskId}) when is_record(PS, player_status) ->
	% #player_status{login_reward = Figure} = PS,
	% ModuleList = get_has_open_modules(Figure#figure.lv),
	%% 功能开放
%%	F = fun({Module, SubModule}) ->
%%			ActiveTask =
%%			case SubModule == 0 of
%%				true ->
%%					case data_module:get_pre_task_id(Module) of
%%						TaskId when TaskId > 0 -> TaskId;
%%						_ -> 0
%%					end;
%%				_ ->
%%					case data_module:get_sub_pre_task_id(Module, SubModule) of
%%						TaskId1 when TaskId1 > 0 -> TaskId1;
%%						_ -> 0
%%					end
%%			end,
%%			case ActiveTask == FinTaskId of
%%				true ->
%%					open_func_event(PS, [{Module, SubModule}]);
%%				_ -> skip
%%			end
%%	end,
%%	lists:map(F, ModuleList),
	%% 完成任务
	task_event(PS, FinTaskId),
	{ok, PS};

%% 登录刷新累计登录任务进度(已废弃)
handle_event(Player, #event_callback{type_id = ?EVENT_LOGIN_CAST}) when is_record(Player, player_status) ->
	% lv_up_event(Player, 0),
	#player_status{id = RoleId, figure = Figure} = Player,
	case lib_elite_assessment:get_elite_assess_dict(RoleId, Figure#figure.lv) of
		{ok, StatusEliteAssess} ->
			#status_elite_assess{
				login_day = LoginDays
				,last_refresh_time = ReFreshTime
			} = StatusEliteAssess,
			MidNight = utime:unixdate(),
			case utime:is_same_day(ReFreshTime, MidNight) of
				true -> ignore;
				_ ->
					NewLoginDays = LoginDays + 1,
					NewRefTime = MidNight,
					NewStatusEliteAssess = StatusEliteAssess#status_elite_assess{
						last_refresh_time = NewRefTime, login_day = NewLoginDays
					},
					lib_elite_assessment:db_save_login_days(RoleId, NewLoginDays, NewRefTime),
					lib_elite_assessment:save_elite_assess_dict(NewStatusEliteAssess),
					login_day_event(Player, NewLoginDays)
			end;
		_ ->
			ignore
	end,
	{ok, Player};

%% 0点刷新累计登录任务进度(已废弃)
handle_event(Player, #event_callback{type_id = ?EVENT_ZERO_CLOCK}) when is_record(Player, player_status)  ->
	#player_status{id = RoleId, figure = Figure} = Player,
	case lib_elite_assessment:get_elite_assess_dict(RoleId, Figure#figure.lv) of
		{ok, StatusEliteAssess} ->
			#status_elite_assess{
				login_day = LoginDays
				,last_refresh_time = ReFreshTime
			} = StatusEliteAssess,
			MidNight = utime:unixdate(),
			case utime:is_same_day(ReFreshTime, MidNight) of
				true -> ignore;
				_ ->
					NewLoginDays = LoginDays + 1,
					NewRefTime = MidNight,
					NewStatusEliteAssess = StatusEliteAssess#status_elite_assess{
						last_refresh_time = NewRefTime, login_day = NewLoginDays
					},
					lib_elite_assessment:db_save_login_days(RoleId, NewLoginDays, NewRefTime),
					lib_elite_assessment:save_elite_assess_dict(NewStatusEliteAssess),
					%% 事件触发
					login_day_event(Player, NewLoginDays)
			end;
		_ ->
			ignore
	end,
	{ok, Player};

%% 战力变更(已废弃)
handle_event(Player, #event_callback{type_id = ?EVENT_COMBAT_POWER}) when is_record(Player, player_status) ->
	#player_status{combat_power = Power} = Player,
	power_event(Player, Power),
	{ok, Player};

handle_event(Player, _) ->
	{ok, Player}.

get_open_modules(Lv) ->
	List = data_module:get_all_module(),
	[{Module, 0} || {Module, L} <- List, Lv =:= L].

get_open_sub_modules(Lv) ->
	List = data_module:get_all_sub_module(),
	[{Module, SubModule} || {Module, SubModule, L} <- List, Lv =:= L].

get_has_open_modules(Lv) ->
	List = data_module:get_all_sub_module(),
	ModuleL = [{Module, SubModule} || {Module, SubModule, L} <- List, Lv >= L],
	List1 = data_module:get_all_module(),
	ModuleL1 = [{TModule, 0} || {TModule, L} <- List1, Lv >= L],
	ModuleL ++ ModuleL1.

%%--------------------------------------------------
%% @doc 最新版任务 @end
%% date: 2021-07-21
%%--------------------------------------------------
%% 放置冒险
fast_dun_event(Player, Data) ->
	#player_status{id = RoleId, figure = Figure} = Player,
	#figure{lv = RoleLv} = Figure,
	lib_elite_assessment:trigger(RoleId, RoleLv, fast_dun, [{fast_dun, Data}]),
	{ok, Player}.

%% 无尽回廊（爬塔副本）普通
tower_dun_event(Player, DunId) ->
	#player_status{id = RoleId, figure = Figure} = Player,
	#figure{lv = RoleLv} = Figure,
%%	case data_dungeon:get_dun(DunId) of
%%		#dun_cfg{condition = Condition} ->
%%			case lists:keyfind(floor, 1, Condition) of
%%				{_, 1, _} ->
%%					lib_elite_assessment:trigger(RoleId, RoleLv, diff_tower_dun, [{diff_tower_dun, DunId}]);
%%				_ ->
%%					lib_elite_assessment:trigger(RoleId, RoleLv, normal_tower_dun, [{normal_tower_dun, DunId}])
%%			end;
%%		_ ->
%%			ignore
%%	end,
	lib_elite_assessment:trigger(RoleId, RoleLv, normal_tower_dun, [{normal_tower_dun, DunId}]),
	{ok, Player}.

%% 困难本，必须达到3星
diff_tower_dun_event(Player, DunId) ->
	#player_status{id = RoleId, figure = Figure} = Player,
	#figure{lv = RoleLv} = Figure,
	lib_elite_assessment:trigger(RoleId, RoleLv, diff_tower_dun, [{diff_tower_dun, DunId}]),
	{ok, Player}.

%% 幻光副本
visus_dun_event(Player, Data) ->
	#player_status{id = RoleId, figure = Figure} = Player,
	#figure{lv = RoleLv} = Figure,
	lib_elite_assessment:trigger(RoleId, RoleLv, visus_dun, [{visus_dun, Data}]),
	{ok, Player}.

%% 宠物副本
pet_dun_event(Player, Data) ->
	#player_status{id = RoleId, figure = Figure} = Player,
	#figure{lv = RoleLv} = Figure,
	lib_elite_assessment:trigger(RoleId, RoleLv, pet_dun, [{pet_dun, Data}]),
	{ok, Player}.

%% 赏金幻魔
reward_boss_event(Player, Data) ->
	#player_status{id = RoleId, figure = Figure} = Player,
	#figure{lv = RoleLv} = Figure,
	lib_elite_assessment:trigger(RoleId, RoleLv, reward_boss, [{reward_boss, Data}]),
	{ok, Player}.

%% 星域幻魔
deserted_boss_event(Player, Data) ->
	#player_status{id = RoleId, figure = Figure} = Player,
	#figure{lv = RoleLv} = Figure,
	lib_elite_assessment:trigger(RoleId, RoleLv, deserted_boss, [{deserted_boss, Data}]),
	{ok, Player}.

%% 击杀个人boss
person_boss_event(PS, Data) ->
	#player_status{id = RoleId, figure = Figure} = PS,
	#figure{lv = RoleLv} = Figure,
	lib_elite_assessment:trigger(RoleId, RoleLv, person_boss, [{person_boss, Data}]),
	{ok, PS}.

%% 试炼副本
assess_dun_event(Player, DunId) ->
	#player_status{id = RoleId, figure = Figure} = Player,
	#figure{lv = RoleLv} = Figure,
	lib_elite_assessment:trigger(RoleId, RoleLv, enter_dungeon, [{enter_dungeon, DunId}]),
	{ok, Player}.



%%--------------------------------------------------
%% 完成任务功能
%% @param  PS   description
%% @param  Data
%% @return      description
%%--------------------------------------------------
task_event(PS, Data) ->
	#player_status{id = RoleId, figure = Figure} = PS,
	#figure{lv = RoleLv} = Figure,
	lib_elite_assessment:trigger(RoleId, RoleLv, task, [{task, Data}]),
	{ok, PS}.

%%--------------------------------------------------
%% 累计登录
%% @param  PS   description
%% @param  Data
%% @return      description
%%--------------------------------------------------
login_day_event(PS, Data) ->
	#player_status{id = RoleId, figure = Figure} = PS,
	#figure{lv = RoleLv} = Figure,
	lib_elite_assessment:trigger(RoleId, RoleLv, login_day, [{login_day, Data}]),
	{ok, PS}.

%%--------------------------------------------------
%% 战力变更
%% @param  PS   description
%% @param  Data
%% @return      description
%%--------------------------------------------------
power_event(PS, Data) ->
	#player_status{id = RoleId, figure = Figure} = PS,
	#figure{lv = RoleLv} = Figure,
	lib_elite_assessment:trigger(RoleId, RoleLv, power, [{power, Data}]),
	{ok, PS}.

%%--------------------------------------------------
%% @doc 功能任务
%%--------------------------------------------------
%%--------------------------------------------------
%% 玩家升级
%% @param  PS   description
%% @param  Data
%% @return      description
%%--------------------------------------------------
lv_up_event(PS, _Data) ->
	#player_status{id = RoleId, figure = Figure} = PS,
	#figure{lv = RoleLv} = Figure,
	lib_elite_assessment:trigger(RoleId, RoleLv, lv, [{lv, RoleLv}]),
	{ok, PS}.

%%--------------------------------------------------
%% 击杀xx怪物xx只
%% @param  PS   description
%% @param  Data
%% @return      description
%%--------------------------------------------------
kill_mon_event(PS, Data) ->
	#player_status{id = RoleId, figure = Figure} = PS,
	#figure{lv = RoleLv} = Figure,
	lib_elite_assessment:trigger(RoleId, RoleLv, kill_mon, [{kill_mon, Data}]),
	{ok, PS}.

%%--------------------------------------------------
%% 击杀特定副本
%% @param  PS   description
%% @param  Data
%% @return      description
%%--------------------------------------------------
pass_dun_event(PS, Data) ->
	#player_status{id = RoleId, figure = Figure} = PS,
	#figure{lv = RoleLv} = Figure,
	lib_elite_assessment:trigger(RoleId, RoleLv, pass_dun, [{pass_dun, Data}]),
	{ok, PS}.

%%--------------------------------------------------
%% 击杀xx经验副本xx次
%% @param  PS   description
%% @param  Data
%% @return      description
%%--------------------------------------------------
pass_dun_wave_event(PS, Data) ->
	#player_status{id = RoleId, figure = Figure} = PS,
	#figure{lv = RoleLv} = Figure,
	lib_elite_assessment:trigger(RoleId, RoleLv, dun_wave, [{pass_dun_wave, Data}]),
	{ok, PS}.

%%--------------------------------------------------
%% 开放xx功能
%% @param  PS   description
%% @param  Data
%% @return      description
%%--------------------------------------------------
open_func_event(PS, Data) ->
	#player_status{id = RoleId, figure = Figure} = PS,
	#figure{lv = RoleLv} = Figure,
	lib_elite_assessment:trigger(RoleId, RoleLv, open_func, [{open_func, Data}]),
	{ok, PS}.

%%--------------------------------------------------
%% 升级进阶系统功能
%% @param  PS   description
%% @param  Data
%% @return      description
%%--------------------------------------------------
upgrade_foster_event(PS, Data) ->
	#player_status{id = RoleId, figure = Figure} = PS,
	#figure{lv = RoleLv} = Figure,
	lib_elite_assessment:trigger(RoleId, RoleLv, foster, [{foster, Data}]),
	{ok, PS}.

%%--------------------------------------------------
%% 活跃等级功能
%% @param  PS   description
%% @param  Data
%% @return      description
%%--------------------------------------------------
activity_event(PS, Data) ->
	#player_status{id = RoleId, figure = Figure} = PS,
	#figure{lv = RoleLv} = Figure,
	lib_elite_assessment:trigger(RoleId, RoleLv, activity_lv, [{activity_lv, Data}]),
	{ok, PS}.

%%--------------------------------------------------
%% 装备评分功能
%% @param  PS   description
%% @param  Data
%% @return      description
%%--------------------------------------------------
equip_score_event(PS, Data) ->
	#player_status{id = RoleId, figure = Figure} = PS,
	#figure{lv = RoleLv} = Figure,
	lib_elite_assessment:trigger(RoleId, RoleLv, equip_score, [{equip_score, Data}]),
	{ok, PS}.

%%--------------------------------------------------
%% 装备穿戴功能
%% @param  PS   description
%% @param  Data
%% @return      description
%%--------------------------------------------------
equip_wear_event(PS, Data) ->
	#player_status{id = RoleId, figure = Figure} = PS,
	#figure{lv = RoleLv} = Figure,
	lib_elite_assessment:trigger(RoleId, RoleLv, equip_num, [{equip_wear, Data}]),
	{ok, PS}.

%%--------------------------------------------------
%% 击杀世界boss
%% @param  PS   description
%% @param  Data
%% @return      description
%%--------------------------------------------------
world_boss_event(PS, Data) ->
	#player_status{id = RoleId, figure = Figure} = PS,
	#figure{lv = RoleLv} = Figure,
	lib_elite_assessment:trigger(RoleId, RoleLv, world_boss, [{world_boss, Data}]),
	{ok, PS}.

%%--------------------------------------------------
%% 市场购买
%% @param  PS   description
%% @param  Data
%% @return      description
%%--------------------------------------------------
market_buy_event(PS, Data) ->
	#player_status{id = RoleId, figure = Figure} = PS,
	#figure{lv = RoleLv} = Figure,
	lib_elite_assessment:trigger(RoleId, RoleLv, market_buy, [{market_buy, Data}]),
	{ok, PS}.

%%--------------------------------------------------
%% 市场上架
%% @param  PS   description
%% @param  Data
%% @return      description
%%--------------------------------------------------
market_show_event(PS, Data) ->
	#player_status{id = RoleId, figure = Figure} = PS,
	#figure{lv = RoleLv} = Figure,
	lib_elite_assessment:trigger(RoleId, RoleLv, market_show, [{market_show, Data}]),
	{ok, PS}.

%%--------------------------------------------------
%% 小队讨伐
%% @param  PS   description
%% @param  Data
%% @return      description
%%--------------------------------------------------
multi_dun_event(PS, Data) ->
	#player_status{id = RoleId, figure = Figure} = PS,
	#figure{lv = RoleLv} = Figure,
	lib_elite_assessment:trigger(RoleId, RoleLv, multi_dun, [{multi_dun, Data}]),
	{ok, PS}.

%%--------------------------------------------------
%% 装备强化
%% @param  PS   description
%% @param  Data
%% @return      description
%%--------------------------------------------------
equip_event(PS, Data) ->
	#player_status{id = RoleId, figure = Figure} = PS,
	#figure{lv = RoleLv} = Figure,
	lib_elite_assessment:trigger(RoleId, RoleLv, equip, [{equip, Data}]),
	{ok, PS}.

%%--------------------------------------------------
%% 装备进化
%% @param  PS   description
%% @param  Data
%% @return      description
%%--------------------------------------------------
equip_star_event(PS, Data) ->
	#player_status{id = RoleId, figure = Figure} = PS,
	#figure{lv = RoleLv} = Figure,
	lib_elite_assessment:trigger(RoleId, RoleLv, equip_sum_star, [{equip_sum_star, Data}]),
	{ok, PS}.

%%--------------------------------------------------
%% 器灵激活
%% @param  PS   description
%% @param  Data
%% @return      description
%%--------------------------------------------------
red_equip_event(PS, Data) ->
	#player_status{id = RoleId, figure = Figure} = PS,
	#figure{lv = RoleLv} = Figure,
	lib_elite_assessment:trigger(RoleId, RoleLv, red_equip, [{red_equip, Data}]),
	{ok, PS}.

%%--------------------------------------------------
%% 战魂上阵
%% @param  PS   description
%% @param  Data
%% @return      description
%%--------------------------------------------------
war_soul_event(PS, Data) ->
	#player_status{id = RoleId, figure = Figure} = PS,
	#figure{lv = RoleLv} = Figure,
	lib_elite_assessment:trigger(RoleId, RoleLv, war_soul, [{war_soul, Data}]),
	{ok, PS}.

%%--------------------------------------------------
%% 完成X转转职
%% @param  PS   description
%% @param  Data
%% @return      description
%%--------------------------------------------------
turn_event(PS, Data) ->
	% ?INFO_TTY("turn_event Data is~w~n",[Data]),
	#player_status{id = RoleId, figure = Figure} = PS,
	#figure{lv = RoleLv} = Figure,
	lib_elite_assessment:trigger(RoleId, RoleLv, turn, [{turn, Data}]),
	{ok, PS}.

%%--------------------------------------------------
%% 魂力达到X阶
%% @param  PS   description
%% @param  Data
%% @return      description
%%--------------------------------------------------
uranium_event(PS, Data) ->
	#player_status{id = RoleId, figure = Figure} = PS,
	#figure{lv = RoleLv} = Figure,
	lib_elite_assessment:trigger(RoleId, RoleLv, uranium, [{uranium, Data}]),
	{ok, PS}.