%%%-------------------------------------------------------------------
%%% @doc     温泉托管
%%% Module  : offline_hosting_charm_beach.erl
%%% Created : 2020-08-04
%%% @Author : tyl
%%%-------------------------------------------------------------------
-module(offline_hosting_charm_beach).

%% API
-export([
	charm_beach_hosting/0
	,act_end/0
]).

%% -callback
-export([
	host_behavior/3
]).

-compile(export_all).

-include("server.hrl").
-include("charm_beach.hrl").
-include("predefine.hrl").
-include("def_module.hrl").
-include("def_goods.hrl").
-include("common.hrl").
-include("offline_hosting.hrl").
-include("attr.hrl").
-include("errcode.hrl").
-include("figure.hrl").
-include_lib("stdlib/include/ms_transform.hrl").

%% 交给take_part_in
%% @param: return - Pid  返回托管管理进程Id
charm_beach_hosting() ->
	Pid = offline_hosting:take_all_part_in(?MODULE, ?MOD_BEACH, [], 30000),
	Pid.

%% 温泉活动结束,触发所有托管玩家退出
act_end() ->
	offline_hosting:notice_logout(?MOD_BEACH).

leave_charm_beach_scene(Player) ->
	#player_status{offline_hosting = OffLineHosting} = Player,
	#offline_hosting{handler = Handler, behavior_ref = Ref} = OffLineHosting,
	case Handler == offline_hosting_charm_beach of
		true ->
			util:cancel_timer(Ref),
			pp_charm_beach:handle(41803, Player, []);
		_ ->
			ignore
	end,
	{ok, Player}.

%% 活动结束排行奖励加入log
add_log(RoleId, Reward) ->
	%AllHostId = ets:match(?ETS_OFFLINE_HOSTING, #ets_offline_hosting{role_id = '$1', _ = '_'}),
	Fun = ets:fun2ms(fun(EtsHosting) when EtsHosting#ets_offline_hosting.value> 0 -> EtsHosting#ets_offline_hosting.role_id end),
	AllHostId = ets:select(?ETS_OFFLINE_HOSTING, Fun),
	F = fun(HostId) when HostId == RoleId ->
		lib_player:apply_cast(HostId, ?APPLY_CAST_STATUS, ?MODULE, do_add_reward_log, [Reward]);
		(_) -> ignore
	    end,
	lists:foreach(F, AllHostId).

do_add_reward_log(Player, Reward) ->
	offline_hosting:host_behavior_reward(Player, Reward).

%% ------------------------------- callback function ------------------------------------------------
%% 托管行为 - 上线进入场景
host_behavior(login, Player, _) ->
	%% 进入温泉场景,会根据本服的情况决定进入本服温泉还是跨服温泉
	pp_charm_beach:handle(41802, Player, []),
	{ok, Player1} = offline_hosting:host_behavior_load_scene(Player, [], 5000), %% 此处需等待 change_scene 返回
	{ok, Player1};

%% 托管行为 - 加载场景(托管玩家加载到场景)
%% 如果玩家不在玩法场景(活动已经结束,就要停止后面的行为)
host_behavior(load_scene, Player, _) ->
	#player_status{scene = SceneId} = Player,
	case lists:member(SceneId, [6110, 6100]) of
		true ->
			%% 开始跳舞
			{ok, Player2} = offline_hosting:host_behavior_delay(Player, start_dance, [], 5000);
		_ ->
			Player2 = Player
	end,
	{ok, Player2};

%% 托管行为 - 开始跳舞
host_behavior(start_dance, Player, _) ->
	%% 开始跳舞
	DanceType = urand:rand(1, 2),
	{ok, Player1} = pp_scene:handle(12025, Player, [DanceType]),
	{ok, Player1};

%% 托管行为 - 结束处理
host_behavior(host_end_pre, Player, _Arg) ->
	#player_status{figure = Figure, id = RoleId} = Player,
	%% 此处不管玩家上线/活动结束都走结束托管,托管玩家执行退出逻辑,直接发放经验值
	case offline_hosting_data:hosting_role_lookup(RoleId) of
		[#ets_hosting_role{time = Begin}] ->
			Now = utime:unixtime(),
			Times = (Now - Begin) div 5,
			{BaseExp, VipRatio} = lib_charm_beach:get_add_exp(?MOD_BEACH, 1, Figure#figure.lv, Figure#figure.vip_type, Figure#figure.vip),
			AddExp = round(BaseExp * (1+VipRatio/100)),
			Player1 = lib_player:add_exp(Player, AddExp * Times, charm_beach),
			{ok, NewPlayer} = offline_hosting:host_behavior_reward(Player1, [{?TYPE_EXP, ?GOODS_ID_EXP, AddExp * Times }]);
		_ ->
			NewPlayer = Player
	end,
	{ok, NewPlayer};

host_behavior(_Behavior, Player, _Any) ->
	{ok, Player}.