%% -----------------------------------
%% desc     :vip副本
%% module   :lib_dungeon_vip.erl
%% author   :hh
%% time     :21/04/22
%% -----------------------------------

-module (lib_dungeon_vip).

-export ([
    handle_wave_state/2
   ,handle_disconnect/4 
    ,role_refresh_anger/1
    % ,add_vip_tmp_skill/1
    ,reset_vip_tmp_skill/2
    ,calc_sweep_rewards/2
    ,login/2
    ]).

-include ("dungeon.hrl").
-include ("server.hrl").
-include ("predefine.hrl").
-include ("attr.hrl").
-include ("figure.hrl").
-include ("common.hrl").
-include ("def_module.hrl").
-include ("drop.hrl").
-include ("def_goods.hrl").

handle_wave_state(State, Args = [?WAVE_NEXT, _Wave|_]) ->
    #dungeon_state{dun_id = DunId, role_list = [#dungeon_role{id = RoleId}|_]} = State,
    NextWave = _Wave + 1,
    DunWaves = data_dungeon_wave:get_dun_waves(DunId),
    MaxWave = lists:max(DunWaves),
    if MaxWave == NextWave ->
            lib_player:apply_cast(RoleId, ?APPLY_CAST_STATUS, ?MODULE, role_refresh_anger, []);
        true -> skip
    end,
    dungeon_default:handle_wave_state_normal(State, Args);
handle_wave_state(State, Args) ->
    dungeon_default:handle_wave_state_normal(State, Args).

%% 中途退出处理
handle_disconnect(_, _Hp, _HpLim, State) ->
    #dungeon_state{role_list = RoleList, is_end = IsEnd} = State,
    if IsEnd == ?DUN_IS_END_YES ->
            State;
        true ->
            [#dungeon_role{id = RoleId}|_] = RoleList,
            {ResultType, ResultSubType} = {?DUN_RESULT_TYPE_SUCCESS, ?DUN_RESULT_SUBTYPE_ROLE_DISCONNECT},
            LogType  = ?DUN_LOG_TYPE_DISCONNECT,
            NewState = lib_dungeon_distribute:dungeon_result(State, ResultType, ResultSubType),
            dungeon_default:handle_role_out_normal(RoleId, LogType, ResultType, ResultSubType, NewState)
    end.

role_refresh_anger(Player) ->
    #player_status{sid = Sid, battle_attr = BattleAttr, figure = #figure{career = Career, vip = Vip}} = Player,
    NewAnger = ?ANGER_MAX,
    NewPlayer = Player#player_status{battle_attr = BattleAttr#battle_attr{anger = NewAnger}},
    mod_scene_agent:update(NewPlayer, [{anger, NewAnger}]),
    SkillId = lib_vip_api:get_vip_pre_skill_by_career(Career),
    if Vip =< 0 ->
            {ok, NewPlayer1} = lib_skill:add_tmp_skill_list(NewPlayer, SkillId, 1);
        true ->
            NewPlayer1 = NewPlayer
    end,
    Unixtime = utime:longunixtime(),
    {ok, Bin1} = pt_130:write(13086, [NewAnger, NewAnger]),
    {ok, Bin2} = pt_200:write(20018, [[{SkillId, Unixtime}]]),
    if Vip =< 0 ->
            SkillList1 = lib_skill:get_my_skill_list(NewPlayer1),
            SkillList = [{SkillId, 1}] ++ SkillList1,
            {ok, Bin3} = pt_210:write(21002, [SkillList]),
            lib_server_send:send_to_sid(Sid, <<Bin3/binary, Bin1/binary, Bin2/binary>>);
        true ->
            lib_server_send:send_to_sid(Sid, <<Bin1/binary, Bin2/binary>>)
    end,
    % ?ERR("refresh anger"),
    {ok, NewPlayer1}.

reset_vip_tmp_skill(Player, DunId) ->
    #player_status{sid = Sid, battle_attr = BattleAttr} = Player,
    case data_dungeon:get_dun_type(DunId) of
        ?DUNGEON_TYPE_VIP ->
            #player_status{figure = #figure{vip = Vip}} = Player,
            if Vip =< 0 ->
                    SkillId = lib_vip_api:get_vip_pre_skill_by_career(Player#player_status.figure#figure.career),
                    {ok, Bin1} = pt_130:write(13086, [0, ?ANGER_MAX]),
                    lib_server_send:send_to_sid(Sid, Bin1),
                    {ok, NewPlayer} = lib_skill:del_tmp_skill_list(Player, SkillId),
                    lib_skill:send_my_skill_list(NewPlayer),
                    NewPlayer;
                true ->
                    NewPlayer = Player#player_status{battle_attr = BattleAttr#battle_attr{anger = 0}},
                    mod_scene_agent:update(Player, [{anger, 0}]),
                    {ok, Bin1} = pt_130:write(13086, [0, ?ANGER_MAX]),
                    lib_server_send:send_to_sid(Sid, Bin1),
                    NewPlayer
            end;
        _ ->
            Player
    end.

calc_sweep_rewards(Player, DunIds) ->
    #player_status{id = RoleId} = Player,
    CountList = lib_daily:get_count(RoleId, ?MOD_DUNGEON, ?MOD_DUNGEON_ENTER, DunIds),
    lists:foldl(fun(DunId, {AccRewards, AccDunIds}) -> 
        case lists:keyfind(DunId, 1, CountList) of
            {_, 0} ->
                case data_dungeon:get_dun(DunId) of
                    #dun_cfg{base_reward = BaseRewards} ->
                        {lists:append(BaseRewards, AccRewards), [DunId|AccDunIds]};
                    _ -> {AccRewards, AccDunIds}
                end;
            _ -> {AccRewards, AccDunIds}
        end
    end, {[], []}, DunIds).

% calc_sweep_rewards(Player, DunIds) ->
%     #player_status{id = RoleId} = Player,
%     CountList = lib_daily:get_count(RoleId, ?MOD_DUNGEON, ?MOD_DUNGEON_ENTER, DunIds),
%     DropMons = data_dungeon_special:get(4, vip_mon_drop),
%     lists:foldl(fun(DunId, {AccRewards, AccDunIds}) -> 
%         case lists:keyfind(DunId, 1, CountList) of
%             {_, Count} when Count == 0 ->
%                 BaseRewards = calc_dun_mon_drop_rewards(Player, DunId, DropMons),
%                 % ?ERR("~p", [BaseRewards]),
%                 AccRewards1 = lists:append(BaseRewards, AccRewards),
%                 AccDunIds1  = [DunId|AccDunIds],
%                 {AccRewards1, AccDunIds1};
%             _ -> {AccRewards, AccDunIds}
%         end
%     end, {[], []}, DunIds).

% calc_dun_mon_drop_rewards(Player, DunId, DropMons) ->
%     case lists:keyfind(DunId, 1, DropMons) of
%         {_, MonList} ->
%             lists:foldl(fun({MonId, DropNum}, Acc) ->
%                 Rewards = calc_dun_mon_drop_rewards_do({MonId, DropNum}, Player, []),
%                 lists:append(Rewards, Acc)
%             end, [], MonList);
%         _ -> []
%     end.

% calc_dun_mon_drop_rewards_do({_MonId, DropNum}, _Player, GoodsList) when DropNum =< 0 ->
%     GoodsList;
% calc_dun_mon_drop_rewards_do({MonId, DropNum}, Player, GoodsList) ->
%     #player_status{figure = #figure{career = Career, lv = Lv, turn = Turn}} = Player,
%     case data_drop:get_rule_list(MonId) of
%         [Alloc|_] ->
%             case data_drop:get_rule(MonId, Alloc, Lv) of
%                 DropRule = #ets_drop_rule{} ->
%                     {StableGoods, RandGoods} = drop_logic:calc_drop_goods(Player, #mon_args{}, DropRule, Career),
%                     InitDropList = StableGoods ++ RandGoods,
%                     CondArgs = #cond_args{lv = Lv, career = Career, turn = Turn, turn_task_id = 0},
%                     DropList = drop:filter_drop_list_with_cond(CondArgs, InitDropList),
%                     Rewards = change_drop_goods_format(DropList, []),
%                     calc_dun_mon_drop_rewards_do({MonId, DropNum-1}, Player, lists:append(Rewards, GoodsList));
%                 _A -> calc_dun_mon_drop_rewards_do({MonId, DropNum-1}, Player, GoodsList)
%             end;
%         _ -> calc_dun_mon_drop_rewards_do({MonId, DropNum-1}, Player, GoodsList)
%     end.

% change_drop_goods_format([], GoodsList) -> GoodsList;
% change_drop_goods_format([DropGoods|Rest], GoodsList) ->
%     #ets_drop_goods{drop_thing_type = DTType, goods_id = GoodsTypeId, bind = Bind, num = GoodsNum
%     } = DropGoods,
%     if
%         DTType == ?TYPE_COIN -> Goods = [{?TYPE_COIN, GoodsNum}];
%         DTType == ?TYPE_GOODS andalso Bind == ?UNBIND ->
%             Goods = [{?TYPE_GOODS, GoodsTypeId, GoodsNum}];
%         DTType == ?TYPE_GOODS andalso Bind == ?BIND ->
%             Goods = [{?TYPE_BIND_GOODS, GoodsTypeId, GoodsNum}];
%         true -> Goods = []
%     end,
%     change_drop_goods_format(Rest, lists:append(Goods, GoodsList)).

%% 第二波才给刷怒气
login(State, _RoleId) ->
    #dungeon_state{wave_num = Wave, role_list = [#dungeon_role{id = RoleId}|_]} = State,
    if Wave == 2 -> lib_player:apply_cast(RoleId, ?APPLY_CAST_STATUS, ?MODULE, role_refresh_anger, []);
        true -> skip
    end,
    [].