%%-----------------------------------------------------------------------------
%% @Module  :       lib_custom_act_do_repeat_recharge_reward
%% @Author  :       Ywf
%% @Email   :       349162727@qq.com
%% @Created :       2019-05-13
%% @Description:	反复充值有礼
%%-----------------------------------------------------------------------------
-module (lib_custom_act_do_repeat_recharge_reward).

-compile(export_all).
-include("custom_act.hrl").
-include("common.hrl").
-include("server.hrl").
-include("errcode.hrl").
-include("def_fun.hrl").

%% 打开界面
send_info(PS, SubType) ->
	ActType = ?CUSTOM_ACT_TYPE_REPEAT_RECHARGE_REWARD,
    case {lib_custom_act_util:get_custom_act_open_info(ActType, SubType), lib_custom_act_util:get_act_reward_cfg_info(ActType, SubType, 1)} of
        {#act_info{stime = Stime, etime = Etime} = ActInfo, #custom_act_reward_cfg{} = RewardCfg} ->
            case lib_custom_act_util:keyfind_act_condition(ActType, SubType, recharge) of
                {recharge, Recharge} when is_integer(Recharge) -> % 单次领取需要多少充值
                    TotalRecharge = lib_recharge_data:get_my_recharge_between(PS#player_status.id, Stime, Etime),
                    AllTime = TotalRecharge div Recharge, % 一共有多少领取次数
                    ReceiveTimes = lib_custom_act:count_receive_times(PS, ActInfo, RewardCfg), % 已领取次数
                    LeftTime = max(0, AllTime - ReceiveTimes),
                    lib_server_send:send_to_sid(PS#player_status.sid, pt_332, 33201, [TotalRecharge, LeftTime]);
                _ -> skip
            end;
        _ -> skip
    end.

%% 计算领取次数
count_receive_times(Player, #act_info{key = {Type, SubType}, stime = Stime, etime = Etime}, #custom_act_reward_cfg{grade = GradeId}) ->
    RewardMap = Player#player_status.status_custom_act#status_custom_act.reward_map,
    case maps:get({Type, SubType, GradeId}, RewardMap, []) of
        #reward_status{utime = Utime, receive_times = ReceiveTimes} ->
            ?IF(Utime >= Stime andalso Utime =< Etime, ReceiveTimes, 0);
        _ -> 0
    end.

%% 计算领奖状态
count_reward_status(Player, #act_info{key = {ActType, SubType}, stime = Stime, etime = Etime} = ActInfo, _Progress, RewardCfg) ->
    case lib_custom_act_util:keyfind_act_condition(ActType, SubType, recharge) of
        {recharge, Recharge} -> % 单次领取需要多少充值
            TotalRecharge = lib_recharge_data:get_my_recharge_between(Player#player_status.id, Stime, Etime),
            AllTime = TotalRecharge div Recharge, % 一共有多少领取次数
            ReceiveTimes = lib_custom_act:count_receive_times(Player, ActInfo, RewardCfg), % 已领取次数
            LeftTimes = max(0, AllTime - ReceiveTimes),
            ?IF(LeftTimes > 0, ?ACT_REWARD_CAN_GET, ?ACT_REWARD_CAN_NOT_GET);
        _ -> ?ACT_REWARD_CAN_NOT_GET
    end.

%% 检测活动结束时是否有忘记领取的奖励
check_has_rewards_for_forget(RoleId, #act_info{key = {Type, SubType}, stime = STime, etime = ETime}, _RwCfg, GotTimes) ->
    %% 读数据库看他充了多少钱
    case lib_custom_act_util:keyfind_act_condition(Type, SubType, recharge) of
        {recharge, NeedRecharge} ->
            case lib_recharge_data:get_recharge_between(RoleId, STime, ETime) of
                TotalRecharge when is_integer(TotalRecharge) -> ok;
                _ -> TotalRecharge = 0
            end,
            TotalTimes = TotalRecharge div NeedRecharge,
            LeftTimes = TotalTimes - GotTimes,
            if
                LeftTimes > 0 -> {ok, LeftTimes};
                true -> false
            end;
        _ -> false
    end.

%% 获取参与活动的玩家
get_act_roles(#act_info{key = {?CUSTOM_ACT_TYPE_REPEAT_RECHARGE_REWARD = Type, SubType}}) -> %% 重复充值有礼
    case lib_custom_act_util:keyfind_act_condition(Type, SubType, recharge) of %% 根据最小充值需求来筛选玩家
        {recharge, MinRecharge} -> lib_recharge_data:get_player_pay_above(MinRecharge);
        _ -> []
    end.
