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

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

%% API
-export([
	finish_support/3            %% 击杀宝箱怪,完成协助
	,collect_act_end/1          %% 采集活动结束
	,clean_collect_support/1    %% 清理社团采集协助数据
	,update_support/3           %% 新增/删除协助
]).

%% -callback_func
-export([
	get_handler/0
	,get_support_init_status/0
	,make_content/1
	,get_support_scene/1
	,notice_before_change_scene/2
	,player_enter_game_player/3
	,after_support_end/3
	,check_success_finish/2
]).

%% 裂缝封印(社团采集)场景
-define(COLLECT_SCENE,  4063).

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

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

%% 记录协助玩法内容
make_content(GameInfo) ->
	%% {MonId, BossCfgId} = GameInfo -- {唯一Id,配置Id}
	Content = {guild_collect, GameInfo},
	Content.

%% 根据玩法记录获取场景信息
get_support_scene(SupportInfo) ->
	#guild_support{content = Content} = SupportInfo,
	case Content of
		{guild_collect, _GameInfo} ->
			?COLLECT_SCENE;
		_ ->
			0
	end.

%% 进入驻地前推送当前协助Id
notice_before_change_scene(RoleId, SupportInfo) ->
	#guild_support{support_sid = SupportSid} = SupportInfo,
	lib_server_send:send_to_uid(RoleId, pt_408, 40812, [SupportSid]),
	ok.

%% 裂缝封印(社团采集)玩法
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,
	SceneId = lib_guild_api:get_guild_scene(),
	case urand:list_rand(data_guild:get(born_coordinate)) of
		{X, Y}  -> ok;
		{X, Y, _} -> ok;
		_ -> X = 0, Y = 0
	end,
	% 服务端进入社团驻地场景
	case PlayerScene == SceneId andalso PoolId =:= ?GUILD_REM_POOL(GuildId) of
		true ->
			%% 直接修改协助状态,不进行场景切换
			SceneInfo = {SceneId, ?GUILD_REM_POOL(GuildId), GuildId, X, Y},
			NewPlayer = lib_guild_support_mod:go_to_support_in_scene(Player, SceneInfo, SupportId, AskerId, SupportCfgId);
		_ ->
			%% 切换场景(需要最后更新SupportCfgId 和 SupportId，避免切场景过程中取消了协助单)
			NewStatusGuild = StatusGuild#status_guild{
				support_scene_info = {SceneId, ?GUILD_REM_POOL(GuildId), GuildId, X, Y}, support_leader_id = AskerId},
			Player1 = Player#player_status{guild = NewStatusGuild},
			Player2 = lib_scene:change_scene(Player1, SceneId, ?GUILD_REM_POOL(GuildId), GuildId, X, Y, false, []),
			NewStatusGuild2 = Player2#player_status.guild,
			NewPlayer = Player2#player_status{
				guild = NewStatusGuild2#status_guild{support_cfg_id = SupportCfgId, support_id = SupportId}
			}
	end,
	{ok, NewPlayer}.

%% 取消协助 | 退出场景之后的处理
after_support_end(RoleId, _FinishType, SupportInfo) ->
	#guild_support{ask_id = AskId} = SupportInfo,
	case RoleId =/= AskId of
		true ->
			lib_server_send:send_to_uid(RoleId, pt_408, 40812, [0]);
		_ ->
			ignore
	end,
	ok.

%% 成功完成协助
finish_support(GuildId, AskId, BossId) ->
	GuildSupportMap = lib_guild_support_mod:get_guild_support_map(GuildId),
	F = fun(SupportId, GuildSupport) ->
		case GuildSupport of
			#guild_support{module = ?MODULE, ask_id = AskId} ->
				% ?INFO_TTY("Finish support AskId is ~w~n",[AskId]),
				lib_guild_support_mod:asker_finish_support(AskId, GuildId, SupportId, [AskId, BossId]);
			_ ->
				ignore
		end end,
	maps:map(F, GuildSupportMap),
	ok.

%% 采集活动结束
collect_act_end(GuildList) ->
	mod_guild_support:clean_collect_support(GuildList),
	ok.

clean_collect_support(GuildId) ->
	GuildSupportMap = lib_guild_support_mod:get_guild_support_map(GuildId),
	F = fun(SupportId, GuildSupport, TMap) ->
		case GuildSupport of
			#guild_support{support_cfg_id = CfgId, support_role_list = RoleList} ->
				case data_guild:get_guild_support_cfg(CfgId) of
					#base_guild_support{module_id = ?MOD_GUILD_COLLECT} ->
						F1 = fun(TSupporterId) ->
							lib_player:apply_cast(
								TSupporterId, ?APPLY_CAST_SAVE,
								lib_guild_support_mod, reset_player_support_status, []
							)
						end,
						lists:map(F1, RoleList),
						TMap1 = maps:remove(SupportId, TMap),
						TMap1;
					_ ->
						TMap
				end;
			_ ->
				TMap
		end end,
	NewSupportMap = maps:fold(F, GuildSupportMap, GuildSupportMap),
	lib_guild_support_mod:save_guild_support_map(GuildId, NewSupportMap),
	%% 通知玩家当前的协助列表长度(GuildSupport直接用空数据处理)
	mod_guild:apply_cast_guild_member(GuildId, 1, lib_guild_support_mod, send_to_guild_member,
		[#guild_support{}, maps:size(NewSupportMap)]
	),
	ok.

%% 检查是否击杀求助者召唤的boss
check_success_finish(SupportInfo, Args) ->
	#guild_support{content = Content, ask_id = AskerId} = SupportInfo,
	case Content of
		{guild_collect, {BossId, _}} when [AskerId, BossId] =/= Args ->
			false;
		_ -> true
	end.

%% 协助更新/删除后给社团驻地玩家推送
%% Type 1-更新 2-删除
update_support(Type, GuildSupport, RoleId) ->
	#guild_support{support_cfg_id = SupportCfgId, support_sid = SupportId,
		ask_id = AskId, status = Status, content = Content, create_time = CTime
	} = GuildSupport,
	case lib_role:get_role_show(AskId) of
		#ets_role_show{figure = AskFigure} -> ok;
		_ -> AskFigure = #figure{}
	end,
	case Content of
		{_, {_, BossId}} -> ok;
		{_, BossId} -> ok;
		_ -> BossId = 0
	end,
	lib_server_send:send_to_uid(RoleId, pt_408, 40811,
		[Type, SupportId, SupportCfgId, AskFigure, Status, BossId, CTime]
	),
	ok.

