%% ---------------------------------
%% desc     :社团午间采集协议映射模块
%% module   :pp_guild_collect.erl
%% author   :hh
%% time     :20/03/01
%% ---------------------------------

-module(pp_guild_collect).

-export([
    handle/3
    ]).

-include("server.hrl").
-include("guild_collect.hrl").
-include("def_module.hrl").
-include("common.hrl").
-include("guild.hrl").
-include ("errcode.hrl").
-include ("figure.hrl").
-include ("def_fun.hrl").

handle(Cmd, Player, Data) ->
    case lib_module:is_open(Player, ?MOD_GUILD_COLLECT) of
        true ->
            do_handle(Cmd, Player, Data);
        _ ->
            ok
    end.

%% 获取社团采集活动状态
do_handle(40801, Player, _) ->
    guild_collect_mgr:get_collect_info(Player#player_status.sid);

do_handle(40802, Player, _) ->
    #player_status{id = RoleId, sid = Sid} = Player,
    DailyCount = lib_daily:get_count(RoleId, ?MOD_GUILD_COLLECT, ?GUILD_COLLECT_2),
    lib_server_send:send_to_sid(Sid, pt_408, 40802, [DailyCount]),
    ok;

%% 获取个人封印次数
do_handle(40803, Player, _) ->
    #player_status{id = RoleId, sid = Sid, guild_collect = GuildCollect} = Player,
    case GuildCollect of
        #role_guild_collect{last_collect_time = LastCollectTime, collect_count = CollectCount} ->
            DailyCount = lib_daily:get_count(RoleId, ?MOD_GUILD_COLLECT, ?GUILD_COLLECT_2),
            guild_collect_mgr:get_role_collect_count(Sid, DailyCount, CollectCount, LastCollectTime);
        _ ->
            {ok, BinData} = pt_408:write(40803, [0, 0]),
            lib_server_send:send_to_sid(Sid, BinData)
    end;

do_handle(40806, Player, _) ->
    #player_status{sid = Sid, scene = Scene, guild = Guild} = Player,
    case Guild of
        #status_guild{id = GuildId} when GuildId =/= 0 ->
           case lib_guild:is_guild_scene(Scene) of
            true ->
                guild_collect_mgr:get_guild_scene_collect_mon(Sid, GuildId);
            _ ->
                lib_server_send:send_to_sid(Sid, pt_408, 40806, [0])
            end;
        _ ->
            lib_server_send:send_to_sid(Sid, pt_408, 40806, [0])
    end;

%% 获取协助单的Boss唯一Id
do_handle(40808, Player, [SupportSid]) ->
    #player_status{sid = Sid, id = RoleId, action_lock = ActionLock, figure = #figure{guild_id = GuildId}} = Player,
    EnterRet = ?IF(ActionLock == free, 0, 1),
    if 
        GuildId =/= 0 ->
            lib_guild_support_mod:get_support_collect_data(RoleId, GuildId, SupportSid, EnterRet);
            % guild_collect_mgr:get_support_mon_data(Sid, GuildId, SupportSid, EnterRet);
        true ->
            lib_server_send:send_to_sid(Sid, pt_408, 40808, [EnterRet, 0])
    end;

%% 前往协助
do_handle(40809, Player, [SupportSid]) ->
    #player_status{sid = Sid, id = _RoleId, scene = _SceneId, action_lock = ActionLock, figure = #figure{guild_id = GuildId}} = Player,
    % IsGuildScene = lib_guild:is_guild_scene(SceneId),
    if
        ActionLock =/= free ->
            lib_server_send:send_to_sid(Sid, pt_408, 40809, [ActionLock, SupportSid]);
        GuildId =/= 0 ->
            pp_guild:handle(40052, Player, [SupportSid]);
            % guild_collect_mgr:response_collect_support(Sid, RoleId, self(), GuildId, SupportSid, IsGuildScene);
        true ->
            lib_server_send:send_to_sid(Sid, pt_408, 40809, [?ERRCODE(err400_no_guild), SupportSid])
    end;

%% 取消协助
do_handle(40810, Player, []) ->
    #player_status{sid = Sid, id = _RoleId, figure = #figure{guild_id = GuildId}} = Player,
    if GuildId =/= 0 ->
            pp_guild:handle(40053, Player, []),
            ok;
            % guild_collect_mgr:cancel_collect_support(Sid, RoleId, GuildId);
        true ->
            lib_server_send:send_to_sid(Sid, pt_408, 40810, [?ERRCODE(err400_no_guild)])
    end;

do_handle(_Cmd, _, _) ->
    ?ERR("error cmd ~p", [_Cmd]),
    ok.