%%-----------------------------------------------------------------------------
%% @Module  :       lib_custom_act_do_act_exchange
%% @Author  :       Ywf
%% @Email   :       349162727@qq.com
%% @Created :       2019-05-13
%% @Description:	活动兑换
%%-----------------------------------------------------------------------------
-module (lib_custom_act_do_act_exchange).

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

%% 活动兑换
do_receive_reward(Player, #act_info{wlv = WLv, key = {?CUSTOM_ACT_TYPE_ACT_EXCHANGE = Type, SubType}}, RewardList, RewardCfg) ->
    #custom_act_reward_cfg{grade = Grade, condition = Condition} = RewardCfg,
    {_, ExGoods, _, _} = lists:keyfind(goods_exchange, 1, Condition),
    case lib_goods_api:cost_object_list(Player, ExGoods, exchange_act, "") of
        {true, NewPs} ->
            %% 日志
            #player_status{figure = #figure{lv = Lv, vip = Vip}} = Player,
            lib_log_api:log_custom_act_reward(Player#player_status.id, Type, SubType, Grade, WLv, RewardList, [],
                Vip, Lv, Player#player_status.gold, NewPs#player_status.gold, Player#player_status.bgold, NewPs#player_status.bgold, utime:unixtime()),
            lib_goods_api:send_reward(NewPs, RewardList, exchange_act, 0);
        {false, Res, _} ->
            {false, Res}
    end.

%% 活动兑换
check_receive_reward_conditions(Player, #act_info{key = {?CUSTOM_ACT_TYPE_ACT_EXCHANGE, _}}, RwCfg) ->
    #custom_act_reward_cfg{condition = Conditions} = RwCfg,
    case lists:keyfind(goods_exchange, 1, Conditions) of
        {goods_exchange, ExGoods, _ModId, _CId} ->
            case lib_goods_api:check_object_list(Player, ExGoods) of
                true -> true;
                Res -> Res
            end;
        _ ->
            {false, ?ERRCODE(err331_act_can_not_get)}
    end.

%% 活动兑换
check_reward_receive_times(Player, #act_info{key = {?CUSTOM_ACT_TYPE_ACT_EXCHANGE = Type, SubType}, stime = Stime, etime = Etime}, #custom_act_reward_cfg{grade = GradeId}) ->
    UnixDate = utime:unixdate(),
    #custom_act_cfg{condition = Condition} = lib_custom_act_util:get_act_cfg_info(Type, SubType),
    {_, ClearType} = lists:keyfind(clear_type, 1, Condition),
    #custom_act_reward_cfg{condition = RewardCondition} = lib_custom_act_util:get_act_reward_cfg_info(Type, SubType, GradeId),
    {_, Limit} = lists:keyfind(exchange_limit, 1, RewardCondition),
    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} ->
            case ClearType  of
                day ->
                    case Utime >= UnixDate of
                        true -> ?IF(ReceiveTimes >= Limit, false, {true, ReceiveTimes + 1});
                        false -> {true, 1}
                    end;
                _ ->
                    case Utime >= Stime andalso Utime =< Etime of
                        true -> ?IF(ReceiveTimes >= Limit, false, {true, ReceiveTimes + 1});
                        false -> {true, 1}
                    end
            end;
        _ -> {true, 1}
    end.