%%-----------------------------------------------------------------------------
%% @Module  :       mod_red_envelopes
%% @Author  :       Czc
%% @Email   :       389853407@qq.com
%% @Created :       2017-09-20
%% @Description:    红包进程(通用框架)
%%-----------------------------------------------------------------------------
-module(mod_red_envelopes).

-include("red_envelopes.hrl").
-include("common.hrl").
-export([start_link/0]).
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).

-compile([export_all]).

start_link() ->
    gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).

init([]) ->
    lib_red_envelopes_mod:init(),
    {ok, []}.

handle_cast(Msg, State) ->
    case catch do_handle_cast(Msg, State) of
        {ok, NewState}->
            {noreply, NewState};
        Err ->
            ?ERR("handle_cast Msg:~p error:~p~n", [Msg, Err]),
            {noreply, State}
    end.

handle_info(Info, State) ->
    case catch do_handle_info(Info, State) of
        {ok, NewState}->
            {noreply, NewState};
        Err ->
            ?ERR("handle_info Info:~p error:~p~n", [Info, Err]),
            {noreply, State}
    end.

do_handle_info(_Info, State) -> {ok, State}.

handle_call(Request, _From, State) ->
    case catch do_handle_call(Request, State) of
        {ok, Reply} ->
            {reply, Reply, State};
        Err ->
            ?ERR("handle_call Request:~p error:~p~n", [Request, Err]),
            {noreply, State}
    end.

do_handle_call(_Request, _State) -> {ok, ok}.

terminate(_Reason, _State) -> ok.

code_change(_OldVsn, State, _Extra) -> {ok, State}.

%% 发送红包列表
send_red_envelopes_list(OwnershipType, OwnershipId, RoleId) ->
    gen_server:cast(?PID, {'send_red_envelopes_list', OwnershipType, OwnershipId, RoleId}).

%% 增加一个新红包
add_red_envelopes(Args) ->
    gen_server:cast(?PID, {'add_red_envelopes', Args}).

%% 打开红包
open_red_envelopes(OwnershipType, OwnershipId, RoleId, RedEnvelopesId, Args) ->
    gen_server:cast(?PID, {'open_red_envelopes', OwnershipType, OwnershipId, RoleId, RedEnvelopesId, Args}).


%%红包发送
handout_red_envelopes(OwnershipType,OwnershipId, RoleID, RedEnvelopesId)->
    gen_server:cast(?PID, {'handout_red_envelopes',OwnershipType,OwnershipId, RoleID, RedEnvelopesId}).


%% 发送红包相关红点信息(是否有可领取的红包)
send_red_point_info(OwnershipType, OwnershipId, RoleId) ->
    gen_server:cast(?PID, {'send_red_point_info', OwnershipType, OwnershipId, RoleId}).
%% 退出帮派
quit_guild(RoleId, GuildId) ->
    gen_server:cast(?PID, {'quit_guild', RoleId, GuildId}).

%% 解散帮派
disband_guild(GuildId) ->
    gen_server:cast(?PID, {'disband_guild', GuildId}).

%% 清理活动红包
clear_act_red_envelopes() ->
    gen_server:cast(?PID, {'clear_red_envelopes', ?ACT_RED_ENVELOPES}).
%% 日常事务
daily_timer(Delay) ->
    gen_server:cast(?PID, {'daily_timer', Delay}).


do_handle_cast({'send_red_envelopes_list', ?GUILD_RED_ENVELOPES = OwnershipType, OwnershipId, RoleId}, State) ->
    RedEnveLopesL = lib_red_envelopes_data:get_red_envelopes_list(OwnershipType, OwnershipId),
    PackList = lib_red_envelopes:pack_red_envelopes_list(RoleId, RedEnveLopesL),
    {ok, BinData} = pt_339:write(33901, [PackList]),
    lib_server_send:send_to_uid(RoleId, BinData),
    {ok, State};

do_handle_cast({'add_red_envelopes', Args}, State) ->
    lib_red_envelopes_mod:add_red_envelopes(Args),
    {ok, State};

do_handle_cast({'handout_red_envelopes',OwnershipType,OwnershipId, RoleID, RedEnvelopesId},State)->
    lib_red_envelopes_mod:handout_red_envelopes(OwnershipType, OwnershipId, RoleID, RedEnvelopesId),
    {ok, State};

do_handle_cast({'open_red_envelopes', OwnershipType, OwnershipId, RoleId, RedEnvelopesId, Args}, State) ->
    lib_red_envelopes_mod:open_red_envelopes(OwnershipType, OwnershipId, RoleId, RedEnvelopesId, Args),
    {ok, State};
do_handle_cast({'send_red_point_info', ?ACT_RED_ENVELOPES = OwnershipType, OwnershipId, RoleId}, State) ->
    RedEnveLopesL = lib_red_envelopes_data:get_red_envelopes_list(OwnershipType, OwnershipId),
    NowTime = utime:unixtime(),
    F = fun(RedEnvelopes) ->
        case lib_red_envelopes:is_vaild_red_envelopes(RedEnvelopes, NowTime) of
            true ->
                #red_envelopes{status = Status, recipients_lists = RecipientsList} = RedEnvelopes,
                case Status == ?HAS_SEND of
                    true ->
                        case lists:keyfind(RoleId, #recipients_record.role_id, RecipientsList) of
                            false -> true;
                            _ -> false
                        end;
                    false -> false
                end;
            false -> false
        end
    end,
    Result = case lists:any(F, RedEnveLopesL) of
        true -> 1;
        _ -> 0
    end,
    {ok, BinData} = pt_331:write(33155, [Result]),
    lib_server_send:send_to_uid(RoleId, BinData),
    {ok, State};

do_handle_cast({'quit_guild', RoleId, GuildId}, State) ->
    db:execute(io_lib:format(?sql_del_red_envelopes_by_guild_id_and_role_id, [GuildId, RoleId])),
    RedEnvelopesMap = lib_red_envelopes_data:get_red_envelopes_map(),
    GuildRedEnvelopesMap = maps:get(?GUILD_RED_ENVELOPES, RedEnvelopesMap, #{}),
    RedEnvelopesList = maps:get(GuildId, GuildRedEnvelopesMap, []),
    NewRedEnvelopesList = lists:filter(fun(Tmp) ->
        case Tmp of
            #red_envelopes{
                id = RedEnvelopesId,
                ownership_type = ?GUILD_RED_ENVELOPES,
                ownership_id = GuildId,
                owner_id = RoleId,
                status = Status,
                type = Type
            } ->
                %% 日志
                lib_log_api:log_red_envelopes(RedEnvelopesId, ?GUILD_RED_ENVELOPES, GuildId, RoleId, ?EXPIRED, Type, [{pre_status, Status}, {quit_guild}]),
                false;
            _ -> true
        end
    end, RedEnvelopesList),
    NewGuildRedEnvelopesMap = maps:put(GuildId, NewRedEnvelopesList, GuildRedEnvelopesMap),
    NewRedEnvelopesMap = maps:put(?GUILD_RED_ENVELOPES, NewGuildRedEnvelopesMap, RedEnvelopesMap),
    lib_red_envelopes_data:save_red_envelopes_map(NewRedEnvelopesMap),
    {ok, State};

do_handle_cast({'disband_guild', GuildId}, State) ->
    RedEnvelopesMap = lib_red_envelopes_data:get_red_envelopes_map(),
    GuildRedEnvelopesMap = maps:get(?GUILD_RED_ENVELOPES, RedEnvelopesMap, #{}),
    DelList = maps:get(GuildId, GuildRedEnvelopesMap, []),
    spawn(fun() ->
        lib_red_envelopes_mod:add_expired_log(DelList, [{disband_guild, GuildId}])
    end),
    NewGuildRedEnvelopesMap = maps:without([GuildId], GuildRedEnvelopesMap),
    NewRedEnvelopesMap = maps:put(?GUILD_RED_ENVELOPES, NewGuildRedEnvelopesMap, RedEnvelopesMap),
    lib_red_envelopes_data:save_red_envelopes_map(NewRedEnvelopesMap),
    % lib_red_envelopes_data:del_obtain_record(?GUILD_RED_ENVELOPES, GuildId),
    {ok, State};

do_handle_cast({'clear_red_envelopes', OwnershipType}, State) ->
    db:execute(io_lib:format(?sql_del_red_envelopes_by_ownership_type_and_id, [OwnershipType, 0])),
    RedEnvelopesMap = lib_red_envelopes_data:get_red_envelopes_map(),
    NewRedEnvelopesMap = maps:remove(OwnershipType, RedEnvelopesMap),
    lib_red_envelopes_data:save_red_envelopes_map(NewRedEnvelopesMap),
    {ok, State};
do_handle_cast({'daily_timer', _DelaySec}, State) ->
    lib_red_envelopes_mod:daily_clear_red_envelopes(),
    {ok, State};


do_handle_cast(_Msg, State) -> {ok, State}.


