%% -------------------------------------
%% desc  :放置经验api
%% module:exp_dun_drop_api.erl
%% author:hh
%% time  :20/01/17
%% -------------------------------------

-module(exp_dun_drop_api).

-export([
    notice_exp_dun_achieve/2
    ,query_exp_drop_reward_info/2
    ,get_exp_drop_reward/3

    ,get_exp_dun_drop_info/2
    ,get_cache_time_last/2
    ,use_goods/3

    ,gm_reset_role_exp_dun_drop/2
    ,gm_reset_exp_drop_state/4
    ]).

-include("server.hrl").
-include("figure.hrl").
-include("exp_dun_drop.hrl").
-include("dungeon.hrl").
-include("common.hrl").
-include ("def_module.hrl").
-include("goods.hrl").
%% 更新玩家经验本历史最佳波数
notice_exp_dun_achieve(RoleId, Wave) ->
    exp_dun_drop_mgr:update_exp_dun_wave_point(RoleId, Wave).


%% 放置经验奖励查询
query_exp_drop_reward_info(RoleId, _Type) ->
    exp_dun_drop_mgr:role_query_exp_drop_reward_info(RoleId).

%% 放置经验奖励领取
get_exp_drop_reward(RoleId, _Type, UpdateArgs) ->
    exp_dun_drop_mgr:role_get_exp_drop_reward(RoleId, UpdateArgs).


%% 查询玩家掉落信息
get_exp_dun_drop_info(RoleId, {Now, TimeLast, LastTime, NewTimeLast}) ->
	exp_dun_drop_mgr:get_exp_dun_drop_info(RoleId, {Now, TimeLast, LastTime, NewTimeLast}).

%% 同步更新玩家放置经验信息
% update_extra_data(Player) ->
%     #player_status{id = RoleId} = Player,
%     ResoureLimit = lib_pet_api:pet_life_skill_about_exp_drop_sum_time(Player),
%     if ResoureLimit =/= 0 ->
%             exp_dun_drop_mgr:update_exp_drop_extra_data(RoleId, [{?MOD_PET, ResoureLimit}]);
%         true ->
%             skip
%     end.

%% 秘籍重置玩家的经验副本通关数据
gm_reset_role_exp_dun_drop(Player, NewMaxWave) ->
    #player_status{
        id = RoleId,
        dungeon_record  = DungeonRecs
        % exp_dun_achieve = RoleExpDunAchieve
        } = Player,
    % ?ERR("gm reset exp drop", []),
    DunId     = 5001,
    DunRec    = maps:get(DunId, DungeonRecs, []),
    NewDunRec = lists:keystore(?DUNGEON_REC_MAX_SCORE, 1, DunRec, {?DUNGEON_REC_MAX_SCORE, NewMaxWave}),
    NewDungeonRecs = maps:put(DunId, NewDunRec, DungeonRecs),
    lib_dungeon:save_dungeon_record(RoleId, DunId, NewDunRec),
    db:execute(<<"DELETE FROM role_exp_dun_achieve WHERE role_id = ~p AND dun_id = ~p">>, [RoleId, DunId]),
    {NeedUpdate, NewAddWaves} = lib_dungeon_exp:calc_new_achive_wave(0, NewMaxWave),
    RoleExpDunAchieve = lib_dungeon_exp:update_exp_dun_achieve(NeedUpdate, RoleId, DunId, NewAddWaves, []),
    exp_dun_drop_mgr:apply_cast(?MODULE, gm_reset_exp_drop_state, [RoleId, NewMaxWave, utime:unixtime()]),
    Player#player_status{
        dungeon_record = NewDungeonRecs, exp_dun_achieve = RoleExpDunAchieve
    }.


gm_reset_exp_drop_state(_State, RoleId, NewMaxWave, Time) ->
    case exp_dun_drop_logic:get_role_drop(RoleId) of
        undefined ->
            skip;
        DropRole = #drop_role{} ->
            NewDropRole = DropRole#drop_role{
                wave_point = [{NewMaxWave, Time}],      %% 最佳波数通关节点 [{wave, reach_time},...]
                start_time = Time,    %% 结算开启时间(领取完或玩家结算开启),个人活动状态决定开启时间
                calc_time = Time,        %% 最近一次结算时间,公共进程结算个人时更新此时间
                calc_goods = [],      %% 累积物品道具(最多不超过配置规定时限)
                goods_limit = [],     %% 限制的物品 产出数量
                is_rewarding = 0,     %% 是否在领奖,若领奖且正结算,则忽略 0否|1是
                remain = 0            %% 上次结算结余时间
            },
            db:execute(?REPLACE_INTO_DROP_INFO, [RoleId, Time, Time, util:term_to_bitstring([])]),
            db:execute(<<"DELETE FROM role_exp_dun_wave_info WHERE role_id = ~p">>, [RoleId]),
            db:execute(?REPLACE_INTOEXP_DUN_WAVE, [RoleId, NewMaxWave, Time]),
            db:execute(?DELETE_ROLE_ALL_GOODS, [RoleId]),
            exp_dun_drop_logic:save_role_drop(NewDropRole),
            skip;
        _ ->
            skip
    end.

use_goods(Player, GoodsInfo, GoodsNum) ->
    #player_status{id = RoleId, exp_dun_drop = RoleExpDunDrop} = Player,
    PerAdd = data_exp_dun_drop:get_kv(11),
    % Efficiency = data_exp_dun_drop:get_kv(12),
    AddLimit = data_exp_dun_drop:get_kv(13),
    Now = utime:unixtime(),
    {LastTime, _TimeLast, NewTimeLast} = get_cache_time_last(RoleExpDunDrop, Now),
    case exp_dun_drop_mgr:use_goods_check(RoleId, [PerAdd, GoodsNum, AddLimit, LastTime, NewTimeLast]) of
        true ->
            lib_goods_api:cost_object_list(Player, [{0, GoodsInfo#goods.goods_id, GoodsNum}], goods_use, ""),
            {ok, Player, GoodsNum};
        {fail, Errcode} ->
            {fail, Errcode}
    end.

%% return: {上次请求时间, 缓存的累计时间, 最新的累计时间}
get_cache_time_last(ExpDunDrop, Now) ->
    case ExpDunDrop of
        #role_exp_dun_drop{cache_time_last = TimeLast, last_time = LastTime} -> ok;
        _ -> TimeLast = 0, LastTime = 0
    end,
    if LastTime =/= 0 -> NewTimeLast = Now - LastTime + TimeLast;
        true -> NewTimeLast = 0
    end,
    {LastTime, TimeLast, NewTimeLast}.