%% ---------------------------------------------------------------------------
%% @doc lib_red_envelopes_check.erl
%% @author  zengzy
%% @since  2018-06-14
%% @deprecated 公会红包
%% ---------------------------------------------------------------------------

-module(lib_red_envelopes_check).

-compile(export_all).

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

check_red_packet_ready(_Player, _Type, []) -> [];
check_red_packet_ready(Player, Type, Args) when is_list(Args) ->
    #player_status{guild = #status_guild{id = GuildId}} = Player,
    case GuildId =/= 0 of
        true ->
            IdList = data_red_envelopes:get_all_id_by_type(Type),
            [Id||Id<-IdList, check_red_packet_ready_help(Player, data_red_envelopes:get_envelope(Id), Args)];
        _ ->
            []
    end;
check_red_packet_ready(_Player, _, _) -> [].


check_red_packet_ready_help(Player, #base_red_envelopes_cfg{condition=Condi} = BaseGuildRedPacket, Args) when is_list(Condi) andalso Condi =/= [] ->
    #base_red_envelopes_cfg{id = PacketId, trigger_interval = TriggerInterval} = BaseGuildRedPacket,
    %%默认上限为1
    case TriggerInterval of
        ?LIFELONG -> %%默认上限为1
            Times = lib_counter:get_count(Player#player_status.id, ?MOD_RED_ENVELOPES, ?MOD_RED_ENVELOPES_TRIGGER, PacketId);
        ?WEEK ->
            Times = lib_week:get_count(Player#player_status.id, ?MOD_RED_ENVELOPES, ?MOD_RED_ENVELOPES_TRIGGER, PacketId);
        ?DAILY ->
            Times = lib_daily:get_count(Player#player_status.id, ?MOD_RED_ENVELOPES, ?MOD_RED_ENVELOPES_TRIGGER, PacketId);
        _ -> Times = 0
    end,
    IsLimit = Times =< 0,
    IsCheckArgs = check_red_packet_args(Player, BaseGuildRedPacket, Args),
    case {IsLimit, IsCheckArgs} of
        {true, true} -> true;
        _ -> false
    end;
check_red_packet_ready_help(_Player, _R, _) ->
    ?ERR("guild_red_packet error config :~p ~n", [_R]),
    false.

check_red_packet_args(_Player, #base_red_envelopes_cfg{}, []) -> true;
check_red_packet_args(Player, #base_red_envelopes_cfg{} = BaseGuildRedPacket, [H | T]) ->
    case do_check_red_packet_args(Player, BaseGuildRedPacket, H) of
        true ->
            check_red_packet_args(Player, BaseGuildRedPacket, T);
        _ ->
            false
    end.

do_check_red_packet_args(Player, BaseGuildRedPacket, {today_recharge = Key, Value}) ->
    #base_red_envelopes_cfg{condition=Condi} = BaseGuildRedPacket,
    case lists:keyfind(Key, 1, Condi) of
        {Key, ValueCfg} when Value >= ValueCfg->
            do_increment_count(Player, BaseGuildRedPacket),
            true;
        _ ->
            false
    end;
do_check_red_packet_args(Player, BaseGuildRedPacket, {Key, Value}) ->
    #base_red_envelopes_cfg{condition=Condi} = BaseGuildRedPacket,
    case lists:keyfind(Key, 1, Condi) of
        {Key, Value} ->
            do_increment_count(Player, BaseGuildRedPacket),
            true;
        _ ->
            false
    end;

do_check_red_packet_args(_,_,_) -> false.

%%增加次数
do_increment_count(Player, BaseGuildRedPacket) when BaseGuildRedPacket#base_red_envelopes_cfg.trigger_interval==?LIFELONG->
    lib_counter:increment(Player#player_status.id, ?MOD_RED_ENVELOPES, ?MOD_RED_ENVELOPES_TRIGGER, BaseGuildRedPacket#base_red_envelopes_cfg.id);
do_increment_count(Player, BaseGuildRedPacket) when BaseGuildRedPacket#base_red_envelopes_cfg.trigger_interval==?WEEK->
    lib_week:increment(Player#player_status.id, ?MOD_RED_ENVELOPES, ?MOD_RED_ENVELOPES_TRIGGER, BaseGuildRedPacket#base_red_envelopes_cfg.id);
do_increment_count(Player, BaseGuildRedPacket) when BaseGuildRedPacket#base_red_envelopes_cfg.trigger_interval==?DAILY->
    lib_daily:increment(Player#player_status.id, ?MOD_RED_ENVELOPES, ?MOD_RED_ENVELOPES_TRIGGER, BaseGuildRedPacket#base_red_envelopes_cfg.id);
do_increment_count(_Player, _BaseGuildRedPacket) ->
    ok.
