%% -------------------------------------
%% desc     :讨伐副本
%% module   :lib_dungeon_crusade.erl
%% author   :hh
%% time     :20/02/25
%% -------------------------------------

-module(lib_dungeon_crusade).

-include("dungeon.hrl").
-include("figure.hrl").
-include("common.hrl").
-include("dungeon_multi.hrl").
-include("predefine.hrl").
-include("server.hrl").
-include("scene.hrl").
-include("mon.hrl").
-include("battle.hrl").
-include("drop.hrl").
-include("def_module.hrl").

-export([
    push_settlement/2
    ,get_dun_dummy_args/2
    ,handle_disconnect/4
    ,get_send_reward/2
    ,get_dun_result_succ_args/2
    ,get_role_typical_data/4
    ]).


%% 推送结算展示
push_settlement(DungeonState, DungeonRole) ->
    #dungeon_state{result_type = ResultType, result_subtype = ResultSubType,
        dun_type = DunType, dun_id = DunId, scene_id = SceneId} = DungeonState,
        #dungeon_role{sid = Sid, node = Node, figure = RoleFigure} = DungeonRole,
        %% 伤害数据
        NewPackData = dungeon_logic:get_mon_hurt_info(DungeonState),
        %% 奖励
        RewardList = lib_dungeon:calc_base_reward_list(DungeonRole),
        %% 定制活动多倍奖励
        MultiTimes = lib_dungeon_api:get_custom_act_reward_times(DunType, DunId, RoleFigure#figure.lv),
        case MultiTimes > 1 of
            true ->
                CustomRewards = [{TGoodsType1, TGoodsId1, TGoodsNum1*max(1, MultiTimes-1)}||{TGoodsType1, TGoodsId1, TGoodsNum1}<-RewardList];
            _ ->
                CustomRewards = []
        end,
        DunRating = dungeon_default:calc_dun_rating(DunId, DungeonRole),
        % ?ERR("~p", [RewardList]),
        {ok, BinData} = pt_610:write(61027, [ResultType, ResultSubType, DunId, SceneId, DunRating, RewardList, CustomRewards, NewPackData]),
        lib_server_send:send_to_sid(Node, Sid, BinData).

%% 假人创建参数
get_dun_dummy_args(_, DummyMember) ->
    #dun_multi_member{figure = Figure, power = Power} = DummyMember,
    [{warning_range, 4000}, {tracing_distance, 400}, {lv, Figure#figure.lv}, {combat_power, Power}].

%% 中途退出处理(不走正常退出流程的操作)
handle_disconnect(RoleId, _Hp, _HpLim, State) ->
    #dungeon_state{dun_id = DunId, team_id = MulTeamId, role_list = RoleList, is_end = IsEnd} = State,
    if IsEnd == ?DUN_IS_END_YES ->
            State;
        true ->
            DunRole = lists:keyfind(RoleId, #dungeon_role.id, RoleList),
            if 
                not is_record(DunRole, dungeon_role) ->
                    State;
                true ->
                        RoleLen = length(RoleList),
                        ResultSubType = ?DUN_RESULT_SUBTYPE_ROLE_DISCONNECT,
                        %% 没人了先结算
                        if RoleLen > 1 -> 
                                ResultType = ?DUN_RESULT_TYPE_NO,
                                NewState0 = State;
                            true ->
                                {ResultType, _} = check_quit_state(State),
                                NewState0 = lib_dungeon_distribute:dungeon_result(State, ResultType, ResultSubType)
                        end,
                        
                        NewDunRole  = DunRole#dungeon_role{online = ?DUN_ONLINE_NO},
                        NewRoleList1 = lists:keystore(RoleId, #dungeon_role.id, RoleList, NewDunRole),
                        NewState    = NewState0#dungeon_state{role_list = NewRoleList1},
                        LogType     = ?DUN_LOG_TYPE_DISCONNECT, 
                        %% 退队
                        Msg = {leave_team, {RoleId}, MulTeamId},
                        lib_dun_multi_cluster:cast_dun_multi_hall_cluster(DunId, Msg),
                        dungeon_default:handle_role_out_normal(RoleId, LogType, ResultType, ResultSubType, NewState)
            end
    end.

%% 退出检查结果类型
check_quit_state(State) ->
    #dungeon_state{is_end = IsEnd, role_list = RoleList, result_type = ResultType, result_subtype = ResultSubType} = State,
    RoleLen = length(RoleList),
    if 
        RoleLen > 1 ->
            {ResultType, ResultSubType};
        IsEnd =/= ?DUN_IS_END_YES ->
            {?DUN_RESULT_TYPE_FAIL, ?DUN_RESULT_SUBTYPE_LOGOUT};
        true ->
            {ResultType, ResultSubType}
    end.

%% 副本默认奖励
get_send_reward(State, DungeonRole) ->
    #dungeon_state{dun_id = DunId, result_type = ResultType} = State,
    #dungeon_role{help_type = HelpType, typical_data = TypicalData} = DungeonRole,
    if
        ResultType =:= ?DUN_RESULT_TYPE_SUCCESS andalso HelpType =:= ?DUN_TEAM_HELP_NO ->
            case data_dungeon:get_dun(DunId) of
                #dun_cfg{base_reward = BaseRewards} -> ok;
                _ -> BaseRewards = []
            end,
            PlusCount = maps:get(?DUN_ROLE_DAILY_ENTER_COUNT, TypicalData, 1),
            PassRewards = [{GoodsType, GoodsId, round(GoodsNum*PlusCount)} || {GoodsType, GoodsId, GoodsNum} <- BaseRewards],
            FinalRewards = lib_goods_api:make_reward_unique(PassRewards),
            DungeonRole#dungeon_role{calc_reward_list = FinalRewards};
        true -> % 助战没有奖励
            DungeonRole
    end.

get_dun_result_succ_args(DunRole, [_ResultSubType|_]) ->
    #dungeon_role{typical_data = TypicalData} = DunRole,
    PlusCount = maps:get(?DUN_ROLE_DAILY_ENTER_COUNT, TypicalData, 1),
    [PlusCount].

get_max_count(_DunType, DunId) ->
    #dun_cfg{count_cond = CountConditions} = data_dungeon:get_dun(DunId),
    [{_, MaxCount}|_] = CountConditions,
    MaxCount.

get_dun_count(RoleId, DunType, DunId) ->
    CountType = lib_dungeon_api:get_daily_count_type(DunType, DunId),
    DailyCount = lib_daily:get_count(RoleId, ?MOD_DUNGEON, ?MOD_DUNGEON_ENTER, CountType),
    DailyCount.

get_role_typical_data(RoleId, DunId, MergeType, TypicalData) ->
    #dun_cfg{type = DunType} = data_dungeon:get_dun(DunId),  
    get_role_typical_data_1(DunType, RoleId, DunId, MergeType, TypicalData).

get_role_typical_data_1(DunType = ?DUNGEON_TYPE_CRUSADE, RoleId, DunId, MergeType, TypicalData) ->
    if MergeType > 0 ->
            MaxCount = get_max_count(DunType, DunId),
            DailyCount = get_dun_count(RoleId, DunType, DunId),
            PlusCount = MaxCount - DailyCount,
            TypicalData1 = maps:put(?DUN_ROLE_DAILY_ENTER_COUNT, PlusCount, TypicalData),
            maps:put(?DUN_ROLE_COUNT_MERGE, 1, TypicalData1);
        true ->
            maps:put(?DUN_ROLE_DAILY_ENTER_COUNT, 1, TypicalData)
    end;
get_role_typical_data_1(_, _, _, _, TypicalData) ->
    TypicalData.

