%% -----------------------------------------
%% desc		:废都boss协助
%% module	:guild_support_deserted_boss.erl
%% author	:hh
%% time		:20/09/16
%% -----------------------------------------

-module(guild_support_deserted_boss).

-include("guild.hrl").
-include("local_boss.hrl").
-include("server.hrl").
-include("predefine.hrl").
-include("common.hrl").
-include("def_fun.hrl").
-include("guild_support.hrl").

%% - callback_func
-export([
	get_handler/0
	,get_support_init_status/0
	,make_content/1
	,get_support_scene/1
	,player_enter_game_player/3
	,after_support_end/3

	,supporter_enter_cls_scene/2
	,role_supporter_enter_cls_scene/3
]).

-export([
	guild_proc_support_cancel_check/4,
	guild_proc_finish_support/4
	]).


get_handler() ->
	?MODULE.


get_support_init_status() ->
	1.

%% GameInfo :: {BossType, BossId}
make_content(GameInfo) ->
	{deserted_boss, GameInfo}.


get_support_scene(SupportInfo) ->
	#guild_support{content = Content} = SupportInfo,
	case Content of
		{deserted_boss, {_BossType, BossId}} ->     %% 赏金boss
			case data_deserted_boss:get_boss_cfg(BossId) of
				#base_deserted_boss_info{level = Level} ->
					case data_deserted_boss:get_level(Level) of
						#base_deserted_level{scene = SceneId, cls_scene = ClsScene} ->
						 	{Ret, _, _} = deserted_boss_mgr:check_cls_deserted_boss(),
							TarScene = ?IF(Ret == true, ClsScene, SceneId),
							TarScene;
						_ ->
							0
					end;
				_ ->
					0
			end;
		_ ->
			0
	end.

%% boss玩法
player_enter_game_player(Player, SupportInfo, _ArgsList) ->
	#player_status{id = RoleId, guild = StatusGuild, scene = PlayerScene} = Player,
	#status_guild{id = GuildId} = StatusGuild,
	#guild_support{
		support_cfg_id = SupportCfgId,
		support_sid  = SupportId,
		content      = Content,
		ask_id       = AskerId
	} = SupportInfo,
	{deserted_boss, {_BossType, BossId}} = Content,
	% 服务端进入boss场景
	case data_deserted_boss:get_boss_cfg(BossId) of
		#base_deserted_boss_info{level = Level} ->
			case data_deserted_boss:get_level(Level) of
				#base_deserted_level{scene = SceneId, cls_scene = ClsScene} ->
					{Ret, _, _} = deserted_boss_mgr:check_cls_deserted_boss(),
					TarScene = ?IF(Ret == true, ClsScene, SceneId),
					[X, Y] = lib_scene:get_born_xy(TarScene),
					case PlayerScene == TarScene of
						true ->
							deserted_boss_mgr:role_go_supporting(RoleId, BossId),
							%% 直接修改协助状态,不进行场景切换
							SceneInfo = {TarScene, 0, 0, X, Y},
							NewPlayer = lib_guild_support_mod:go_to_support_in_scene(Player, SceneInfo, SupportId, AskerId, SupportCfgId);
						_ ->
							%% 必须传入 support_scene_info 作为是否成功切换场景判断
							NewStatusGuild = StatusGuild#status_guild{
								support_scene_info = {TarScene, 0, 0, X, Y}, support_leader_id = AskerId},
							Player1 = Player#player_status{guild = NewStatusGuild},
							case local_boss_api:deserted_boss_enter(Player1, BossId, [support_enter, [SupportId, AskerId, SupportCfgId]]) of
								{true, NewPlayer0} ->
									deserted_boss_mgr:role_go_supporting(RoleId, BossId),
									NewStatusGuild2 = NewPlayer0#player_status.guild,
									NewPlayer = NewPlayer0#player_status{
										guild = NewStatusGuild2#status_guild{support_cfg_id = SupportCfgId, support_id = SupportId}
									};
								clusters -> %% 跨服玩法,异步判断
									NewPlayer = Player;
								{false, Err} ->
									lib_server_send:send_to_uid(RoleId, pt_461, 46106, [Err, BossId]),
									mod_guild_support:apply_cast(lib_guild_support_mod, delete_from_support_role_list, [GuildId, SupportId, RoleId]),
									NewPlayer = Player;
								_ ->
									mod_guild_support:apply_cast(lib_guild_support_mod, delete_from_support_role_list, [GuildId, SupportId, RoleId]),
									NewPlayer = Player
							end
					end;
				_ ->
					mod_guild_support:apply_cast(lib_guild_support_mod, delete_from_support_role_list, [GuildId, SupportId, RoleId]),
					NewPlayer = Player
			end;
		_ ->
			%% 查找错误将玩家踢出列表
			mod_guild_support:apply_cast(lib_guild_support_mod, delete_from_support_role_list, [GuildId, SupportId, RoleId]),
			NewPlayer = Player
	end,
	{ok, NewPlayer}.


%% 取消协助|退出场景
after_support_end(RoleId, FinishType, SupportInfo) ->
	#guild_support{ask_id = Asker, support_role_list = SupportList, content = Content} = SupportInfo,
	{deserted_boss, {_BossType, BossId}} = Content,
	if
		RoleId == Asker -> %% 被协助者取消协助
			deserted_boss_mgr:asker_cancel_support(RoleId, BossId),
			[deserted_boss_mgr:supported_cancel_support(Supporter, BossId) || Supporter <- SupportList];
		true -> %% 协助者取消协助
			FinishType =:= ?SUPPORT_SUCCESS andalso
				lib_player:apply_cast(RoleId, ?APPLY_CAST_STATUS, equipment_logic, help_kill_deserted_boss, []),
			deserted_boss_mgr:supported_cancel_support(RoleId, BossId)
	end,
	ok.

%% 公会进程调用
guild_proc_support_cancel_check(RoleId, GuildId, BossId, SupportUniqueId) ->
	GuildMaps = lib_guild_support_mod:get_guild_support_map(GuildId),
	GuildL = maps:values(GuildMaps),
	case lists:keyfind(SupportUniqueId, #guild_support.support_sid, GuildL) of
		#guild_support{ask_id = RoleId, content = {deserted_boss, {_, BossId}}} ->
			lib_player:apply_cast(RoleId, ?APPLY_CAST_STATUS, lib_guild_support_mod, cancel_support, []);
		_A ->
			skip
	end.

guild_proc_finish_support(RoleId, GuildId, BossId, Sid) ->
	GuildMaps = lib_guild_support_mod:get_guild_support_map(GuildId),
	GuildL = maps:values(GuildMaps),
	case lists:keyfind(Sid, #guild_support.support_sid, GuildL) of
		#guild_support{ask_id = RoleId, content = {deserted_boss, {_, BossId}}, support_role_list = SupportList} ->
			lib_guild_support_mod:asker_finish_support(RoleId, GuildId, Sid, []),
			[begin
				lib_guild_support_mod:supporter_finish_support(Supporter, GuildId, Sid, []),
				lib_achievement_api:async_event(Supporter, lib_achievement_api, support_event, 1)
			end
				|| Supporter <- SupportList];
		_ ->
			skip
	end.

%% 进入跨服玩法协助后返回更新人物身上数据
supporter_enter_cls_scene(SceneArgs, EnterArgs) ->
	[RoleId|_] = SceneArgs,
	lib_player:apply_cast(RoleId, ?APPLY_CAST_STATUS, ?MODULE, role_supporter_enter_cls_scene, [SceneArgs, EnterArgs]).

role_supporter_enter_cls_scene(Player, SceneArgs, EnterArgs) ->
	#player_status{guild = StatusGuild} = Player,
	[_, [SupportId, AskerId, SupportCfgId| _] | _ ] = EnterArgs,
	[_, SceneId, ScenePool, CopyId, X, Y, Args|_] = SceneArgs,
	NewStatusGuild = StatusGuild#status_guild{
		support_scene_info = {SceneId, ScenePool, 0, X, Y}, support_cfg_id = SupportCfgId,
		support_id = SupportId, support_leader_id = AskerId
	},
	NewPlayer = Player#player_status{guild = NewStatusGuild},
	NewPlayer1 = lib_scene:change_scene(NewPlayer, SceneId, ScenePool, CopyId, X, Y, Args),
	{ok, NewPlayer1}.