%%%-------------------------------------------------------------------
%%% @author zmh
%%% @doc
%%%     离线托管-社团BOSS
%%% @end
%%%-------------------------------------------------------------------
-module(offline_hosting_guild_boss).

-include("server.hrl").
-include("common.hrl").
-include("attr.hrl").
-include("scene.hrl").
-include("def_module.hrl").
-include("role.hrl").
-include("guild.hrl").
-include("figure.hrl").

%% API
-export([
    check_hosting/1,
    check_hosting_in_guild/2,
    host_behavior/3,
    notice_logout/1
]).

%% 活动开始30秒后，每3秒进10个人
%% 踢人在玩家结算后再踢人
check_hosting(Act) ->
    mod_guild:apply_cast(?MODULE, check_hosting_in_guild, [Act,self()]).

%% 公会进程判断情况
check_hosting_in_guild(Act,_From) ->
    AllGuild = lib_guild_data:get_guild_map(),
    Guild_Members = maps:fold(
        fun(GuildId, _, Temp) ->
            GuildMember = lib_guild_data:get_guild_member_map(GuildId),
            [{GuildId, maps:keys(GuildMember)}|Temp]
        end, [], AllGuild
    ),
    Now = utime:unixtime(),
    Pid = spawn(
        fun() -> check_hosting_by_role(Guild_Members, Now, 0, Act) end
    ),
    Pid.

check_hosting_by_role([], _, _,_) -> ok;
check_hosting_by_role([{_, []}|T], Now, Count,Act) ->
    check_hosting_by_role(T, Now, Count,Act);
check_hosting_by_role([{GuildId, [RoleId|TRole]}|TGuild], Now, Count,Act) ->
    Res = offline_hosting:take_part_in(?MODULE, ?MOD_GUILD_BOSS, Act, RoleId, []),
    case Res =:= 1 of
        true  ->
            timer:sleep(150),
            check_hosting_by_role(TGuild++[{GuildId, TRole}], Now, Count,Act);
        false ->
            check_hosting_by_role(TGuild++[{GuildId, TRole}], Now, Count,Act)
    end.

notice_logout(Act)->
    offline_hosting:notice_logout(?MOD_GUILD_BOSS,Act).

%% 状态：
%%                        进入场景 前往  打怪

%% 托管上线事件 2s后加载一下场景
host_behavior(login, Player, _) ->
    #player_status{id = RoleID, scene = Scene, figure = #figure{lv = Lv}, guild = GuildStatus} = Player,
    #status_guild{id = GuildId, name = GName} = GuildStatus,
    InGuild = lib_guild_api:check_in_guild_scene(Scene),
    case InGuild of
        true->
            {TargetX,TargetY} = data_guild_boss:get(mon_pos),
            DestinationBehavior = battle,
            {ok, Player1} = offline_hosting:host_behavior_move(Player, TargetX, TargetY, DestinationBehavior, [], 1000),
            {ok, Player1};
        _->
            mod_guild_tribe_mgr:enter_scene(RoleID, GuildId, GName ,Lv),
            {ok, Player1} = offline_hosting:host_behavior_load_scene(Player, [], 2000),
            {ok, Player1}
    end;

%%前往挂机点 开打
host_behavior(load_scene,Player,_)->
    {TargetX,TargetY} = data_guild_boss:get(mon_pos),
    DestinationBehavior = battle,
    {ok, Player2} = offline_hosting:host_behavior_move(Player, TargetX, TargetY, DestinationBehavior, [], 1000),
    {ok, Player2};

host_behavior(battle, Player, _) ->
    {ok, Player1} = offline_hosting:host_behavior_battle(Player, 3),
    {ok, Player1};

%% 死亡后安全区复活-10秒
host_behavior(die, Player, _) ->
    {ok, Player1} = offline_hosting:host_behavior_delay(Player, revive, [], 10000),
    {ok, Player1};

%% 复活->前往目标挂机
host_behavior(revive, Player, _) ->
    #player_status{battle_attr = #battle_attr{hp_lim = Hp} = BA} = Player,
    %% 通知离开场景
    lib_scene:leave_scene(Player),
    [X, Y] = lib_scene:get_born_xy(Player#player_status.scene),
    Player1 = Player#player_status{x = X, y = Y, battle_attr = BA#battle_attr{hp = Hp}},
    %% 加载场景
    {ok, Player2} = pp_scene:handle(12002, Player1, []),
    {TargetX,TargetY} = data_guild_boss:get(mon_pos),
    DestinationBehavior = battle,
    {ok, Player3} = offline_hosting:host_behavior_move(Player2, TargetX, TargetY, DestinationBehavior, [], 1000),
    {ok, Player3};

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