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

-export([
    do_receive_reward/4,
    check_receive_reward_conditions/3,
    check_reward_receive_times/3
    ,count_receive_times/3
    ,update_receive_times/3
]).

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

%% 集字活动
do_receive_reward(Player, #act_info{wlv = WLv, key = {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, col_world_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, col_world_act, 0);
        {false, Res, _} ->
            {false, Res}
    end.

%% 集字兑换是否有足够的物品兑换,是否达到兑换次数上限
check_receive_reward_conditions(Player, #act_info{key = {_, SubType}} = _ActInfo, RwCfg) ->
    % ReceiveTimes = lib_custom_act:count_receive_times(Player, ActInfo, RwCfg),
    Type = calc_counter_type(SubType, RwCfg#custom_act_reward_cfg.grade),
    ReceiveTimes = lib_daily:get_count(Player#player_status.id,
        ?MOD_AC_CUSTOM, ?CUSTOM_ACT_TYPE_COLWORD, Type
    ),
    #custom_act_reward_cfg{condition = Conditions} = RwCfg,
    case lists:keyfind(goods_exchange, 1, Conditions) of
        {goods_exchange, ExGoods} ->
            case lib_goods_api:check_object_list(Player, ExGoods) of
                true ->
                    case lists:keyfind(exchange_limit, 1, Conditions) of
                        {exchange_limit, LimitTime} ->
                        	?IF(LimitTime > ReceiveTimes, true, {false, ?ERRCODE(err331_receive_time_limit)});
                        _ -> true
                    end;
                Res -> Res
            end;
        _ -> {false, ?ERRCODE(err331_act_can_not_get)}
    end.

%% 开服集字活动有次数限制
check_reward_receive_times(Player, #act_info{key = {ActType, SubType}} = _ActInfo, RwInfo) ->
    case lib_custom_act_util:get_act_cfg_info(ActType, SubType) of
        #custom_act_cfg{condition = Condition} ->
            case lists:keyfind(clear_type, 1, Condition) of
                {_, act} ->
                    % OldTimes = lib_custom_act:count_receive_times(Player, ActInfo, RwInfo),
                    Type = calc_counter_type(SubType, RwInfo#custom_act_reward_cfg.grade),
                    OldTimes = lib_daily:get_count(Player#player_status.id,
                        ?MOD_AC_CUSTOM, ?CUSTOM_ACT_TYPE_COLWORD, Type
                    ),
                    NewTimes = OldTimes + 1;
                _ -> NewTimes = 0
            end,
            case lists:keyfind(role_lv, 1, Condition) of
                {_, DefLv} when NewTimes =/= 0 ->
                    if
                        Player#player_status.figure#figure.lv < DefLv ->
                            {false, ?ERRCODE(err331_act_can_not_get)};
                        true ->
                            {true, NewTimes}
                    end;
                _ ->
                    {false, ?ERRCODE(err_config)}
            end
    end.

%% 计算领取次数
count_receive_times(Player, #act_info{key = {_, SubType}} = _ActInfo, RewardCfg) ->
    Type  = calc_counter_type(SubType, RewardCfg#custom_act_reward_cfg.grade),
    Count = lib_daily:get_count(Player#player_status.id,
        ?MOD_AC_CUSTOM, ?CUSTOM_ACT_TYPE_COLWORD, Type
    ),
    Count.

%% 更新兑换次数
update_receive_times(Player, SubType, GradeId) ->
    Type = calc_counter_type(SubType, GradeId),
    lib_daily:increment(Player#player_status.id,
        ?MOD_AC_CUSTOM, ?CUSTOM_ACT_TYPE_COLWORD, Type
    ),
    Player.

calc_counter_type(SubType, GradeId) ->
    SubType * 1000 + GradeId.