%%%-------------------------------------------------------------------
%%% @author zmh
%%% @doc
%%%
%%% @end
%%%-------------------------------------------------------------------
-module(pp_guild_boss).
-include("server.hrl").
-include("figure.hrl").
-include("common.hrl").
-include("guild.hrl").
-include("errcode.hrl").
-include("def_module.hrl").
-include("def_goods.hrl").
-include("goods.hrl").
-include("scene.hrl").
-include("predefine.hrl").
-include("guild_tribe.hrl").

%% API
-export([handle/3]).

handle(Cmd, Status, ProData) ->
    do_handle(Cmd, Status, ProData).

%% 获取活动状态
do_handle(40600, Status, []) ->
    #player_status{id=RoleID}=Status,
    mod_guild_boss_mgr:get_act_state(RoleID),
    {ok,Status};

%% 主动拿血量
do_handle(40601,Status,[])->
    #player_status{id=RoleID}=Status,
    mod_guild_boss_mgr:get_hp_state(RoleID),
    {ok,Status};

do_handle(40602,Status,[Act])->
    #player_status{id=RoleID,guild = GuildStatus}=Status,
    #status_guild{id = GuildID} = GuildStatus,
    GuildID>0 andalso mod_guild_boss_mgr:apply_guild_role_rank(RoleID,Act,GuildID),
    {ok,Status};

%%公会榜单
do_handle(40603,Status,[Act])->
    #player_status{id=RoleID,guild=GuildStatus}=Status,
    #status_guild{id=GuildID} = GuildStatus,
    GuildID>0 andalso mod_guild_boss_mgr:apply_guild_rank(RoleID,Act,GuildID),
    {ok,Status};

do_handle(40604,Status,[Act,OpTy])->
    #player_status{id=RoleID,guild=GuildStatus}=Status,
    #status_guild{id=GuildID} = GuildStatus,
    GuildID>0 andalso mod_guild_boss_mgr:apply_self_data(Act,OpTy,RoleID,GuildID),
    {ok,Status};

do_handle(40606,Status,[])->
    #player_status{id=RoleID,guild=GuildStatus}=Status,
    #status_guild{id=GuildID} = GuildStatus,
    GuildID>0 andalso mod_guild_boss_mgr:get_mon_state(RoleID,GuildID),
    {ok,Status};

do_handle(40607,Status,[])->
    #player_status{id=RoleID,guild=GuildStatus}=Status,
    #status_guild{id=GuildID} = GuildStatus,
    GuildID>0 andalso mod_guild_boss_mgr:get_role_inspire(RoleID,GuildID),
    {ok,Status};

do_handle(40608,Status,[OpTy])->
    #player_status{
        id=RoleID,
        figure = Figure,
        scene = Scene,
        scene_pool_id=PoolId,
        guild=GuildStatus
    }=Status,
    #status_guild{
        id=GuildID,
        name = GName
    } = GuildStatus,
    #figure{name=RName}=Figure,
    case GuildID>0 of
        true->
            Max = get_inspire_max(OpTy),
            case catch  mod_guild_boss_mgr:get_role_inspire_data(OpTy,RoleID,GuildID) of
                {ok,true,Times,RTimes,Totals} when Times<Max->
                    RecRank = #rec_rank{
                        rid = RoleID,
                        gid = GuildID,
                        name = RName,
                        gname = GName
                    },
                    {Value,_} = data_guild_boss:get(bgold_encourage_cost),
                    Cost = [{?TYPE_BGOLD,?GOODS_ID_BGOLD,Value}],
                    Res = case OpTy of
                        0->
                            {true,Status,Status};
                        _->
                            lib_goods_api:cost_object_list_with_check(Status, Cost, boss_inspire, ["boss inspire"])
                    end,
                    case Res of
                        {true,NStatus,_}->
                            %% 发突破奖励
                            Produce = #produce{
                                 reward = [{?TYPE_HONOUR,?GOODS_ID_HONOR,20}]
                                ,type = boss_inspire, show_tips = 1, remark = "社团boss"
                            },
                            lib_goods_api:send_reward_with_mail(RoleID, Produce, ?MOD_GUILD_BOSS),
                            mod_guild_boss_mgr:inspire_anyone(OpTy,RecRank),
                            {ok,Bin} = pt_406:write(40612,[Figure,RTimes+1,Totals+1]),
                            lib_server_send:send_to_scene(Scene,PoolId,Bin);
                        {false,Err,_}->
                            {ok, Bin} = pt_406:write(40608, [Err,OpTy]),
                            lib_server_send:send_to_uid(RoleID, Bin),
                            NStatus = Status
                    end;
                {ok,true,_,_,_}->
                    {ok,Bin} = pt_406:write(40608,[?ERRCODE(err406_inspire_times_limit),OpTy]),
                    lib_server_send:send_to_uid(RoleID,Bin),
                    NStatus = Status;
                _->
                    {ok,Bin} = pt_406:write(40608,[?ERRCODE(err406_not_open),OpTy]),
                    lib_server_send:send_to_uid(RoleID,Bin),
                    NStatus = Status
            end;
        _->
            NStatus = Status
    end,
    {ok,NStatus};

%%掉落拾取
do_handle(40610,Status,[0,DropID])->
    #player_status{id=RoleID}=Status,
    Now = utime:unixtime(),
    put(role_drop_args,{DropID,Now+3}),
    {ok,Bin} = pt_406:write(40610,[1,0,DropID]),
    lib_server_send:send_to_uid(RoleID,Bin),
    {ok,Status};
do_handle(40610,Status,[1,DropID])->
    #player_status{id=RoleID}=Status,
    Now = utime:unixtime(),
    case get_drop_args() of
        {DropID,ETime} when Now>=ETime->
            %%时间够
            do_drop_pick(DropID,Status),
            {ok,Bin} = pt_406:write(40610,[1,1,DropID]),
            erase(role_drop_args);
        {DropID,_ETime}->
            {ok,Bin} = pt_406:write(40610,[?ERRCODE(err406_now_collecting),1,DropID]);
        _->
            {ok,Bin} = pt_406:write(40610,[?ERRCODE(err406_need_finish_collect),1,DropID])
    end,
    lib_server_send:send_to_uid(RoleID,Bin),
    {ok,Status};
do_handle(40610,Status,[OpTy,DropID])->
    #player_status{id=RoleID}=Status,
    erase(role_drop_args),
    {ok,Bin} = pt_406:write(40610,[1,OpTy,DropID]),
    lib_server_send:send_to_uid(RoleID,Bin),
    {ok,Status};

%% 掉落信息
do_handle(40613, Status,[]) ->
    #player_status{id = RoleId, guild = StatusGuild}=Status,
    #status_guild{id = GuildId} = StatusGuild,
    mod_guild_boss_mgr:drops_roll_info(RoleId, GuildId),
    {ok,Status};

%% 掉落roll点 2021-6-21 删除拒绝roll点逻辑
do_handle(40614, Status, [Option, OrderId]) ->
    #player_status{scene = SceneId} = Status,
    case data_scene:get(SceneId) of
        #ets_scene{type = ?SCENE_TYPE_GUILD} ->
            case Option == 1 of
                true ->
                    #player_status{id = RoleId, guild = StatusGuild}=Status,
                    #status_guild{id = GuildId} = StatusGuild,
                    mod_guild_boss_mgr:roll_drops(RoleId, GuildId, Option, OrderId);
                _ ->
                    ignore
            end;
        _ ->
            ignore
    end,
    {ok,Status};

%% 单个掉落的roll点信息
do_handle(40616, Status, [OrderId]) ->
    #player_status{scene = SceneId} = Status,
    case data_scene:get(SceneId) of
        #ets_scene{type = ?SCENE_TYPE_GUILD} ->
            #player_status{id = RoleId, guild = StatusGuild}=Status,
            #status_guild{id = GuildId} = StatusGuild,
            mod_guild_boss_mgr:drop_multi_roll_info(RoleId, GuildId, OrderId);
        _ ->
            ignore
    end,
    {ok,Status};

do_handle(_Cmd, _Player, _Data) ->
    tool:back_trace(),
    erlang:display({?MODULE, ?LINE, _Cmd, _Data}),
    {ok,_Player}.


get_inspire_max(0)->
    data_guild_boss:get(free_encourage);
get_inspire_max(_)->
    {_,T} = data_guild_boss:get(bgold_encourage_cost),
    T.

get_drop_args()->
    case get(role_drop_args) of
        {DropID,ETime}->
            {DropID,ETime};
        _->
            put(role_drop_args,{0,0}),
            {0,0}
    end.

do_drop_pick(DropID,Status)->
    IsCluster = lib_scene:is_clusters_scene(Status#player_status.scene),
    case DropID rem 2 of %% 偶数为跨服掉落
        0 when IsCluster ->
            PsArgs = lib_drop:trans_to_ps_args(Status, ?NODE_CENTER),
            Node = clusters_node:get_node(),
            clusters_node:apply_cast(mod_drop, drop_pickup, [Node, DropID, PsArgs]);
        _ ->
            PsArgs = lib_drop:trans_to_ps_args(Status, ?NODE_GAME),
            mod_drop:drop_pickup(none, DropID, PsArgs)
    end.