%%%------------------------------------
%%% @Module  : lib_consume_rank_act
%%% @Author  :  xiaoxiang
%%% @Created :  2016-11-17
%%% @Description: 充值消费榜活动
%%%------------------------------------

-module(lib_consume_rank_act).

-include("server.hrl").
-include("def_fun.hrl").
-include("consume_rank_act.hrl").
-include("figure.hrl").
-include("common.hrl").
-include("custom_act.hrl").
-include("predefine.hrl").
-include("def_module.hrl").
-include("rec_event.hrl").
-include("def_event.hrl").

-compile(export_all).

make_record(RankType, SubType, RoleId, Value) ->
    #rank_role{
        role_key  = {RankType, RoleId},           %% 键
        rank_type = RankType,                     %% 榜单类型
        sub_type  = SubType,                      %% 活动子类型
        role_id   = RoleId,                       %% 玩家id
        value     = Value,                        %% 魅力值
        time      = utime:unixtime()              %% 时间
    }.


refresh_recharge(SubType, #player_status{id = RoleId, mark = 0} = PS, Num) ->
    pp_custom_act:handle(33104, PS, [?CUSTOM_ACT_TYPE_RECHARGE_RANK, SubType]),
    refresh_common_rank(?RANK_TYPE_RECHARGE, SubType, RoleId, Num);
refresh_recharge(_,_,_) ->skip.

refresh_consume(PS) ->
    case lib_custom_act_api:get_open_custom_act_infos(?CUSTOM_ACT_TYPE_CONSUME_RANK) of
        [_|_] = ActInfos ->
            [ refresh_consume(SubType, PS, lib_consume_data:get_gold_consume_between(PS#player_status.id, max(STime, util:get_merge_time()), ETime))
                ||  #act_info{key = {_, SubType}, stime = STime, etime = ETime} <- ActInfos];
        _ -> ok
    end.

refresh_consume(SubType, #player_status{id = RoleId, mark = 0} = PS, Num) ->
    pp_custom_act:handle(33104, PS, [?CUSTOM_ACT_TYPE_CONSUME_RANK, SubType]),
    refresh_common_rank(?RANK_TYPE_CONSUME, SubType, RoleId, Num);
refresh_consume(_, _, _) -> skip.

%% 榜单
refresh_common_rank(RankType, SubType, RoleId, Num) ->
    RankRole = make_record(RankType, SubType, RoleId, Num),
    mod_consume_rank_act:refresh_common_rank(RankType, SubType, RankRole).

is_exist(_I, []) -> false;
is_exist(I, [H | T]) ->
    case I == H of
        true ->
            true;
        false ->
            is_exist(I, T)
    end.

%% 转换定制活动类型
change_act_type(RankType) ->
    case RankType of
        ?RANK_TYPE_RECHARGE -> ?CUSTOM_ACT_TYPE_RECHARGE_RANK;
        ?RANK_TYPE_CONSUME -> ?CUSTOM_ACT_TYPE_CONSUME_RANK;
        _ -> 0
    end.


%% 配置榜单最大长度
get_max_len(RankType, SubType) ->
    ActType = change_act_type(RankType),
    case lib_custom_act_util:get_act_cfg_info(ActType, SubType) of
        #custom_act_cfg{condition = Condition} when is_list(Condition) ->
            case lists:keyfind(rank_len, 1, Condition) of
                {_, MaxLen} when is_integer(MaxLen) -> MaxLen;
                _ -> 0
            end;
        _ -> 0
    end.

%% 配置上榜阈值
get_rank_limit(RankType, SubType) ->
    ActType = change_act_type(RankType),
    case lib_custom_act_util:get_act_cfg_info(ActType, SubType) of
        #custom_act_cfg{condition = Condition} when is_list(Condition) ->
            case lists:keyfind(rank_limit, 1, Condition) of
                {_, Limit} when is_integer(Limit) -> Limit;
                _ -> 0
            end;
        _ -> 0
    end.

%% 最低奖励阈值
get_reward_num(RankType, SubType) ->
    ActType = change_act_type(RankType),
    case lib_custom_act_util:get_act_cfg_info(ActType, SubType) of
        #custom_act_cfg{condition = Condition} when is_list(Condition) ->
            case lists:keyfind(reward_num, 1, Condition) of
                {_, RNum} when is_integer(RNum) -> RNum;
                _ -> 9999999
            end;
        _ -> 9999999
    end.

get_participate_reward_grade(RankType, SubType) ->
    Type = change_act_type(RankType),
    case lib_custom_act_util:get_act_cfg_info(Type, SubType) of
        #custom_act_cfg{condition = Condition} when is_list(Condition) ->
            case lists:keyfind(participate_reward, 1, Condition) of
                {_, GradeList} when is_list(GradeList) -> GradeList;
                _ -> []
            end;
        _ -> []
    end.

%% 榜单奖励
get_rank_reward(RankType, SubType, RankRole) ->
    ActType = change_act_type(RankType),
    GradeList = lib_custom_act_util:get_act_reward_grade_list(ActType, SubType),
    do_get_rank_reward(ActType, SubType, RankRole, GradeList, []).

do_get_rank_reward(_, _, _, [], TmpL) -> TmpL;
do_get_rank_reward(ActType, SubType, RankRole, [H | T], TmpL) ->
    #rank_role{rank = Rank, award_state = AwardState} = RankRole,
    TmpReward = case lib_custom_act_util:get_act_reward_cfg_info(ActType, SubType, H) of
                    #custom_act_reward_cfg{condition = Condition, reward = Reward} when is_list(Condition) andalso is_list(Reward) ->
                        case lists:keyfind(rank, 1, Condition) of
                            {_, {Min, Max}} ->
                                case Max >= Rank andalso Rank >= Min of
                                    true ->
                                       case lists:keyfind(H, 1, AwardState) of
                                            {_, 1} ->
                                                TmpL;
                                            _ ->
                                                Reward ++ TmpL
                                        end;
                                    _ -> TmpL
                                end;
                            _ -> TmpL
                        end;
                    _ -> TmpL
                end,
    do_get_rank_reward(ActType, SubType, RankRole, T, TmpReward).

handle_merge_recharge() ->
    [ mod_consume_rank_act:clear_recharge_rank_act(0, SubType)
        ||  SubType <- lib_custom_act_api:get_open_subtype_ids(?CUSTOM_ACT_TYPE_RECHARGE_RANK) ].
handle_merge_consume() ->
    [ mod_consume_rank_act:clear_consume_rank_act(0, SubType)
        ||  SubType <- lib_custom_act_api:get_open_subtype_ids(?CUSTOM_ACT_TYPE_CONSUME_RANK) ].

%% 判断是否属于参与奖, 是否达到领取条件
is_participate_reward(RankType, SubType, GradeId, Num) ->
    GradeIdList = lib_consume_rank_act:get_participate_reward_grade(RankType, SubType),
    LimitNum = lib_consume_rank_act:get_reward_num(RankType, SubType),
    case lists:member(GradeId, GradeIdList) of
        true when Num >= LimitNum ->
            true;
        _ ->
            false
    end.
