%%%-------------------------------------------------------------------
%%% @author zmh
%%% @doc
%%%     离线托管-社团篝火
%%% @end
%%%-------------------------------------------------------------------
-module(offline_hosting_guild_tribe).

-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").
-include("errcode.hrl").
-include("dance.hrl").

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

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

%% 公会进程判断情况
check_hosting_in_guild(_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) end
    ),
    Pid.

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

notice_logout()->
    offline_hosting:notice_logout(?MOD_GUILD).

%% 状态：
%%                        进入场景 前往  发呆

%% 托管上线事件 2s后加载一下场景
host_behavior(login, Player, _) ->
    #player_status{id = RoleID, figure = #figure{lv = Lv}, guild = GuildStatus} = Player,
    #status_guild{id = GuildId, name = GName} = GuildStatus,
    mod_guild_tribe_mgr:enter_scene(RoleID, GuildId, GName ,Lv),
    {ok, Player1} = offline_hosting:host_behavior_load_scene(Player, [], 2000),
    {ok, Player1};

%%前往挂机点 跳舞
host_behavior(load_scene,Player,_)->
    {TargetX,TargetY} = data_guild_boss:get(mon_pos),
    {RandX, RandY} = umath:rand_point_in_circle(TargetX,TargetY, 800, 1),
    DestinationBehavior = dance,
    {ok, Player2} = offline_hosting:host_behavior_move(Player, RandX, RandY, DestinationBehavior, [], 3000),
    {ok, Player2};

host_behavior(dance,Player,_)->
    #player_status{id = RoleId,scene = SceneId, scene_pool_id = ScenePoolId, copy_id = CopyId, dance = Dance} = Player,
    #dance{is_rand = IsRand,dance_id = DanceId, dance_list = DanceList} = Dance,
    Type =
    if
        IsRand == 0 ->
            DanceId;
        DanceList =/= [] ->
            urand:list_rand(DanceList);
        true ->
            0
    end,
    mod_scene_agent:change_dance(SceneId, ScenePoolId, CopyId, RoleId, Type),
    {ok, Player#player_status{role_dance = Type}};

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