%%%-------------------------------------------------------------------
%%% @doc    社团支援 - boss玩法
%%% Module  : guild_support_boss.erl
%%% Created : 2020-08-20
%%% @Author : tyl
%%%-------------------------------------------------------------------
-module(guild_support_boss).


-include("guild.hrl").
-include("server.hrl").
-include("sql_guild.hrl").
-include("errcode.hrl").
-include("common.hrl").
-include("figure.hrl").
-include("def_fun.hrl").
-include("def_module.hrl").
-include("local_boss.hrl").
-include("def_act_label.hrl").
-include("guild_support.hrl").
-include("predefine.hrl").

%% boss类型(对应功能Id)
-define(BOSS_TYPE_WORLD_BOSS,  2).     %% 世界boss
-define(BOSS_TYPE_REWARD_BOSS, 3).     %% 赏金boss

%% - 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
]).

%% 处理模块
get_handler() ->
	Handler = ?MODULE,
	Handler.

%% 协助初始状态
get_support_init_status() ->
	InitStatus = 1,
	InitStatus.

%% 记录协助玩法内容
make_content(GameInfo) ->
	Content = {srv_boss, GameInfo},
	Content.

%% 根据玩法记录获取场景信息
get_support_scene(SupportInfo) ->
	#guild_support{content = Content} = SupportInfo,
	case Content of
		{srv_boss, {_BossType, BossId}} ->     %% 赏金boss
			case data_local_boss:get_reward_boss_info(BossId) of
				#base_reward_boss_info{boss_scene = SceneId} ->
					SceneId;
				_ ->
					0
			end;
		_ ->
			0
	end.

%% boss玩法
player_enter_game_player(Player, SupportInfo, _ArgsList) ->
	#player_status{id = RoleId, guild = StatusGuild, scene = PlayerScene, scene_pool_id = PoolId} = Player,
	#status_guild{id = GuildId} = StatusGuild,
	#guild_support{
		support_cfg_id = SupportCfgId,
		support_sid  = SupportId,
		content      = Content,
		ask_id       = AskerId
	} = SupportInfo,
	{srv_boss, {_BossType, BossId}} = Content,
	% 服务端进入boss场景
	case data_local_boss:get_reward_boss_info(BossId) of
		#base_reward_boss_info{boss_scene = SceneId, boss_position = {X, Y}} ->
			case PlayerScene == SceneId andalso PoolId =:= BossId of
				true ->
					reward_boss_mgr:role_go_supporting(RoleId, BossId),
					%% 直接修改协助状态,不进行场景切换
					SceneInfo = {SceneId, BossId, 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_cfg_id = SupportCfgId, support_id = SupportId,
						support_scene_info = {SceneId, BossId, 0, X, Y},support_leader_id = AskerId
					},
					Player1 = Player#player_status{guild = NewStatusGuild},
					case local_boss_api:ask_enter_boss_scene(Player, BossId, true) of
						ok ->
							reward_boss_mgr:role_go_supporting(RoleId, BossId),
							NewPlayer = Player1;
						_ ->
							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,
	{ok, NewPlayer}.

%% 完成协助
after_support_end(RoleId, FinishType, SupportInfo) ->
	RoleId =/= SupportInfo#guild_support.ask_id andalso
		FinishType =:= ?SUPPORT_SUCCESS andalso
			lib_player:apply_cast(RoleId, ?APPLY_CAST_STATUS, equipment_logic, help_kill_reward_boss, []),
	ok.