%%%-------------------------------------------------------------------
%%% @author liushl
%%% @doc
%%%
%%% @end
%%%-------------------------------------------------------------------
-module(lib_custom_act_do_consume).
-author("liushl").

-include("custom_act.hrl").

%% API
-export([send_forgot_rewards/1]).

%% @doc 发送未领取的奖励
send_forgot_rewards(ActInfo) ->
    #act_info{
        stime = Stime,
        etime = ETime
    } = ActInfo,
    AllRole = lib_consume_data:get_consume_gold_roles(Stime, ETime) ,
    spawn(
        fun() -> send_forgot_rewards_do(ActInfo, AllRole) end
    ).

send_forgot_rewards_do(ActInfo, AllRole) ->
    #act_info{
        key = {Type, SubType}
    } = ActInfo,
    timer:sleep(10000),
    GradeIds = lib_custom_act_util:get_act_reward_grade_list(Type, SubType),
    lists:foldl(
        fun([RoleId, ConsumeGold|_], N) ->
            case is_integer(ConsumeGold) andalso ConsumeGold > 0 of
                true ->
                    send_forgot_rewards_by_role(RoleId, ConsumeGold, ActInfo, GradeIds);
                false ->
                    ok
            end,
            case N > 5 of
                true ->
                    timer:sleep(1000),
                    0;
                false ->
                    N + 1
            end
        end, 0, AllRole
    ).

send_forgot_rewards_by_role(RoleId, ConsumeGold, #act_info{key = {Type, SubType}} = ActInfo, GradeIds) ->
    RwStatus = lib_custom_act_api:load_custom_act_reward(RoleId),
    Award = role_send_forgot_rewards_by_grade(RwStatus, ConsumeGold, ActInfo, GradeIds, []),
    case Award =/= [] of
        true  ->
            case lib_custom_act_util:get_act_cfg_info(Type, SubType) of
                #custom_act_cfg{name = Name} ->
                    Title = utext:get(3310001),
                    Content = utext:get(3310002, [Name]),
                    lib_mail_api:send_sys_mail([RoleId], Title, Content, Award);
                _ ->
                    ok
            end;
        false ->
            ok
    end.

role_send_forgot_rewards_by_grade(_RwStatus, _ConsumeGold, _ActInfo, [], ForgetAward) -> ForgetAward;
role_send_forgot_rewards_by_grade(RwStatus, ConsumeGold, #act_info{key = {Type, SubType}} = ActInfo, [GradeId|T], ForgetAward) ->
    case lib_custom_act_util:get_act_reward_cfg_info(Type, SubType, GradeId) of
        RwCfg when is_record(RwCfg, custom_act_reward_cfg) ->
            #custom_act_reward_cfg{condition = Conditions, reward = Reward} = RwCfg,
            case lists:keyfind(money_type, 1, Conditions) of
                {money_type, gold, Value} when ConsumeGold >= Value ->
                    GotTimes = lib_custom_act_api:calc_got_reward_times(RwStatus, ActInfo, GradeId),
                    case GotTimes > 0 of
                        true  ->
                            role_send_forgot_rewards_by_grade(RwStatus, ConsumeGold, ActInfo, T, ForgetAward);
                        false ->
                            role_send_forgot_rewards_by_grade(RwStatus, ConsumeGold, ActInfo, T, Reward ++ ForgetAward)
                    end;
                _ ->
                    ForgetAward
            end;
        _ ->
            ForgetAward
    end.