%%-----------------------------------------------------------------------------
%% @Module  :       lib_custom_act_do_daily_target
%% @Author  :       Ywf
%% @Email   :       349162727@qq.com
%% @Created :       2019-05-08
%% @Description:	每日目标
%%-----------------------------------------------------------------------------
-module (lib_custom_act_do_daily_target).

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

-export ([
    check_receive_reward_conditions/3,
    get_extra_log/1
    ]).
-export ([
    handle_event/2,
    send_info/2,
    refresh_target/2
    ]).

-define(STAGE_ADD, 1000).   % 坐骑阶数星数转换

%% 检测领取条件是否满足
check_receive_reward_conditions(PS, _ActInfo, RwCfg) ->
    #custom_act_reward_cfg{condition = Condition} = RwCfg,
    case lib_custom_act_check:check_act_condtion([target_type], Condition) of
        [TargetType] ->
            {FirstVal, SecondVal} = get_value_by_conditions(Condition),
            case {FirstVal, SecondVal} of
                {0, 0} -> %% 配置出错
                    {false, ?ERRCODE(err_config)};
                _ ->
                    {SelFirstVal, SelSecondVal} = get_self_value(PS, TargetType), %% 获取自己的值
                    ?IF(SelFirstVal >= FirstVal andalso SelSecondVal >= SecondVal, true, {false, ?ERRCODE(err331_act_can_not_get)})
            end;
        _ -> {false, ?ERRCODE(err331_act_can_not_get)}
    end.

%% 日志额外记录
get_extra_log(#custom_act_reward_cfg{condition = Condition}) ->
    {FirstVal, SecondVal} = get_value_by_conditions(Condition),
    io_lib:format("first_val:~p second_val:~p", [FirstVal, SecondVal]).

% %% 升级
% handle_event(PS, #event_callback{type_id = ?EVENT_LV_UP}) when is_record(PS, player_status) ->
%     refresh_target(PS, ?DAILY_TARGET_TYPE_LV),
%     {ok, PS};
% %% 战力
% handle_event(PS, #event_callback{type_id = ?EVENT_COMBAT_POWER}) when is_record(PS, player_status) ->
%     refresh_target(PS, ?DAILY_TARGET_TYPE_COMBAT),
%     {ok, PS};
handle_event(PS, _) ->
    {ok, PS}.

%% 获取活动界面
send_info(PS, SubType) ->
	ActType = ?CUSTOM_ACT_TYPE_DAILY_TARGET,
    #player_status{sid = Sid, status_custom_act = #status_custom_act{reward_map = RewardMap}} = PS,
    case lib_custom_act_util:get_custom_act_open_info(ActType, SubType) of
        #act_info{stime = Stime} ->
            RewardIds = lib_custom_act_util:get_act_reward_grade_list(ActType, SubType),
            Fun = fun(RewardId, RewardList) ->
                RewardCfg = lib_custom_act_util:get_act_reward_cfg_info(ActType, SubType, RewardId),
                #custom_act_reward_cfg{desc = Descript, condition = Condition, reward = Reward} = RewardCfg,
                case lib_custom_act_check:check_act_condtion([target_type], Condition) of
                    [TargetType] ->
                        {FirstVal, SecondVal} = get_value_by_conditions(Condition),
                        case {FirstVal, SecondVal} of
                            {0, 0} -> %% 配置出错
                                {false, ?ERRCODE(err_config)};
                            _ ->
                                {SelFirstVal, SelSecondVal} = get_self_value(PS, TargetType), %% 获取自己的值
                                case maps:get({ActType, SubType, RewardId}, RewardMap, []) of
                                    #reward_status{utime = Utime, receive_times = ReceiveTimes} ->
                                        State = case Utime >= Stime andalso ReceiveTimes > 0 of
                                            true -> ?ACT_REWARD_HAS_GET;
                                            _ -> ?IF(SelFirstVal >= FirstVal andalso SelSecondVal >= SecondVal, ?ACT_REWARD_CAN_GET, ?ACT_REWARD_CAN_NOT_GET)
                                        end;
                                    _ -> State = ?IF(SelFirstVal >= FirstVal andalso SelSecondVal >= SecondVal, ?ACT_REWARD_CAN_GET, ?ACT_REWARD_CAN_NOT_GET)
                                end,
                                RewardStr = util:term_to_string(Reward),
                                [{RewardId, TargetType, SelFirstVal, SelSecondVal, FirstVal, SecondVal, RewardStr, Descript, State}|RewardList]
                        end;
                    _ -> RewardList
                end
            end,
            RewardList = lists:foldl(Fun, [], RewardIds),
            lib_server_send:send_to_sid(Sid, pt_332, 33205, [SubType, RewardList]);
        _ -> skip
    end.

get_self_value(PS, TargetType) ->
    #player_status{combat_power = CombatPower, hightest_combat_power = HightestCombatPower, daily_growth = DailyGrowth, figure = #figure{lv = RoleLv}} = PS,
    case TargetType of %% 获取自己的值
        ?DAILY_TARGET_TYPE_START_SOUL   -> {lib_star_soul_util:get_all_combat(PS), 0}; %% 星灵{战力, 0}
        ?DAILY_TARGET_TYPE_STONE_TYPE_LV-> {lib_stone:get_type_stone_lv(PS, att), lib_stone:get_type_stone_lv(PS, def)}; %% 宝石升级 {攻击向宝石等级，防御向宝石等级}
        ?DAILY_TARGET_TYPE_COMBAT       -> {max(CombatPower, HightestCombatPower), max(HightestCombatPower - maps:get(0, DailyGrowth, HightestCombatPower), 0)}; %% {战力, 每日战力提升数据}
        ?DAILY_TARGET_TYPE_FASHION      -> %% 时装{战力， 每日提升星级}
            FashionStar = lib_fashion:get_fashion_total_star(PS) + lib_fashion:get_fashion_active_num(PS),
            DateFashionStar = maps:get(1, DailyGrowth, FashionStar),
            {lib_fashion:get_fashion_combat_power(PS), max(FashionStar - DateFashionStar, 0)};
        ?DAILY_TARGET_TYPE_LV           -> {RoleLv, 0}; %% 角色等级{等级, 0}
        ?DAILY_TARGET_TYPE_EQUIP        -> {0, 0}; %% 装备{战力, 0}
        ?DAILY_TARGET_TYPE_STONE_TOTAL_LV -> {0, 0}; %% 宝石的总等级{总等级, 0}
        _-> {0, 0}
    end.

get_value_by_conditions(Condition) ->
    case lib_custom_act_check:check_act_condtion([first_val], Condition) of %% 条件1
        [FirstVal] -> ok;
        _ -> FirstVal = 0
    end,
    case lib_custom_act_check:check_act_condtion([second_val], Condition) of %% 条件2
        [SecondVal] -> ok;
        _ -> SecondVal = 0
    end,
    {FirstVal, SecondVal}.

%% 刷新榜单（先判断当天是否开启了相关的活动）
refresh_target(PS, TargetType) ->
    ActType = ?CUSTOM_ACT_TYPE_DAILY_TARGET,
    SubTypeIds = lib_custom_act_api:get_open_subtype_ids(ActType),
    F = fun(SubType, NeedUpdateSubType) ->
        GradeList = lib_custom_act_util:get_act_reward_grade_list(ActType, SubType),
        F2 = fun(GradeId, TmpNeedUpdateSubType) ->
            #custom_act_reward_cfg{condition = Condition} = lib_custom_act_util:get_act_reward_cfg_info(ActType, SubType, GradeId),
            case lib_custom_act_check:check_act_condtion([target_type], Condition) of
                [TargetType] -> [SubType | lists:delete(SubType, TmpNeedUpdateSubType)];
                _ -> TmpNeedUpdateSubType
            end
        end,
        lists:foldl(F2, NeedUpdateSubType, GradeList)
    end,
    NeedUpdateSubType = lists:foldl(F, [], SubTypeIds),
    do_refresh_target(PS, NeedUpdateSubType, TargetType),
    ok.

do_refresh_target(PS, SubTypeIds, _TargetType) ->
    ActType = ?CUSTOM_ACT_TYPE_DAILY_TARGET,
    Fun = fun(SubType, AllNeedUpdateSubType) ->
        GradeList = lib_custom_act_util:get_act_reward_grade_list(ActType, SubType),
        Fun1 = fun(GradeId, NeedUpdateSubType) ->
            case lib_custom_act_check:check_receive_reward(PS, ActType, SubType, GradeId) of
                {true, _ActInfo, _NewReceiveTimes, _RewardList, _RewardCfg} ->
                    [SubType | lists:delete(SubType, NeedUpdateSubType)];
                _ ->
                    NeedUpdateSubType
            end
        end,
        lists:foldl(Fun1, AllNeedUpdateSubType, GradeList)
    end,
    AllNeedUpdateSubType = lists:foldl(Fun, [], SubTypeIds),
    [send_info(PS, SubType) || SubType <- AllNeedUpdateSubType].
