%%%-------------------------------------------------------------------
%%% @author liushl
%%% @doc
%%%     充值活动
%%% @end
%%%-------------------------------------------------------------------
-module(lib_custom_act_do_recharge).
-author("liushl").

-include("custom_act.hrl").
-include("language.hrl").
-include("server.hrl").
-include("errcode.hrl").

%% API
-export([
    receive_reward/4,
    count_reward_status/4,
    send_forgot_rewards/2
]).

%% @doc 活动派发接口：领取
receive_reward(Player, Type, SubType, GradeId) ->
    Res =
    case custom_act_personal:is_personal_act_open(Player, Type, SubType) of
        {true, ActInfo} ->
            case lib_custom_act_check:check_receive_reward_normal(Player, ActInfo, GradeId) of
                {true, ActInfo, NewReceiveTimes, RewardList, RewardCfg} ->
                    %% 先更新购买状态
                    Player1 = lib_custom_act:update_receive_times(Player, Type, SubType, GradeId, NewReceiveTimes),
                    case lib_custom_act_dispatcher:do_receive_reward(Player1, ActInfo, RewardList, RewardCfg) of
                        {false, ErrCode} -> {false, ErrCode};
                        Player2 -> {true, Player2}
                    end;
                {false, Error} ->
                    {false, Error}
            end;
        false ->
            {false, ?ERRCODE(err331_act_closed)}
    end,
    case Res of
        {true, NewPlayer} ->
            {ok, BinData} = pt_331:write(33105, [?SUCCESS, Type, SubType, GradeId]),
            lib_server_send:send_to_sid(Player#player_status.sid, BinData),
            {ok, NewPlayer};
        {false, Code} ->
            {ok, BinData} = pt_331:write(33105, [Code, Type, SubType, GradeId]),
            lib_server_send:send_to_sid(Player#player_status.sid, BinData),
            ok
    end.

%% 计算可领取状态
count_reward_status(Player, ActInfo, Progress, RewardCfg) ->
    case lib_custom_act_check:check_reward_receive_times(Player, ActInfo, RewardCfg) of
        false -> ?ACT_REWARD_HAS_GET;
        _ ->
            #custom_act_reward_cfg{condition = Conditions} = RewardCfg,
            case lists:keyfind(gold, 1, Conditions) of
                {gold, Value} when Progress >= Value ->
                    ?ACT_REWARD_CAN_GET;
                _ ->
                    ?ACT_REWARD_CAN_NOT_GET
            end
    end.

%% @doc 发送未领取的奖励
send_forgot_rewards(Type, SubType) ->
    SQL = io_lib:format(<<"SELECT role_id,s_time,e_time FROM custom_act_personal WHERE type=~p and subtype=~p">>, [Type, SubType]),
    AllRole = db:get_all(SQL),
    spawn(
        fun() -> send_forgot_rewards_do(Type, SubType, AllRole) end
    ).

send_forgot_rewards_do(Type, SubType, AllRole) ->
    timer:sleep(10000),
    GradeIds = lib_custom_act_util:get_act_reward_grade_list(Type, SubType),
    lists:foldl(
        fun([RoleId, Stime, Etime], N) ->
            TotalRecharge = lib_recharge_data:get_recharge_between(RoleId, Stime, Etime),
            case is_integer(TotalRecharge) andalso TotalRecharge > 0 of
                true ->
                    ActInfo = #act_info{key = {Type, SubType}, stime = Stime, etime = Etime},
                    send_forgot_rewards_by_role(RoleId, TotalRecharge, 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, TotalRecharge, #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, TotalRecharge, 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),
                    lib_custom_act_api:log_recharge_consume_act_do(RoleId, Type, SubType, TotalRecharge, 0, 1, Award, utime:unixtime());
                _ ->
                    ok
            end;
        false ->
            ok
    end.

role_send_forgot_rewards_by_grade(_RwStatus, _TotalRecharge, _ActInfo, [], ForgetAward) -> ForgetAward;
role_send_forgot_rewards_by_grade(RwStatus, TotalRecharge, #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(gold, 1, Conditions) of
                {gold, Value} when TotalRecharge >= 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, TotalRecharge, ActInfo, T, ForgetAward);
                        false ->
                            role_send_forgot_rewards_by_grade(RwStatus, TotalRecharge, ActInfo, T, Reward ++ ForgetAward)
                    end;
                _ ->
                    ForgetAward
            end;
        _ ->
            ForgetAward
    end.