%%%-------------------------------------------------------------------
%%% @author : tyl
%%% @doc     精英考核副本(精英考核内置的副本，放在副本里便于管理)
%%% Module  : lib_dungeon_elite_assess.erl
%%% Created : 2021-07-19
%%% @end
%%%-------------------------------------------------------------------
-module(lib_dungeon_elite_assess).

-include("server.hrl").
-include("figure.hrl").
-include("predefine.hrl").
-include("errcode.hrl").
-include("common.hrl").
-include("elite_assessment.hrl").

%% API
-export([
	check_can_enter/2
]).

%% 判断是否可以进入副本
check_can_enter(Player, DunId) ->
	#player_status{id = RoleId, figure = Figure} = Player,
	case lib_elite_assessment:get_elite_assess_dict(RoleId, Figure#figure.lv) of
		{ok, StatusEliteAssess} ->
			#status_elite_assess{progress_list = ProgressList,
				current_category = CurrentCategory} = StatusEliteAssess,
			?INFO_TTY("CurrentCategory ~p~n",[CurrentCategory]),
			case data_elite_assessment:get_dun_task(CurrentCategory) of
				DunTaskId when is_integer(DunTaskId), DunTaskId > 0  ->
					?INFO_TTY("DunTaskId is ~p~n",[DunTaskId]),
					AllTask  = data_elite_assessment:get_all_task_cfg(CurrentCategory),
					TaskList = [EliteTask||#elite_assess{category = Category} = EliteTask <-ProgressList,
						Category == CurrentCategory],
					?INFO_TTY("TaskList is ~p~n",[TaskList]),
					case lists:keyfind(DunTaskId, #elite_assess.task_id, TaskList) of
						#elite_assess{status = Status} when Status >= ?CAN_RECEIVE ->
							{false, ?ERRCODE(err_452_has_finish_dun)}; %% todo
						_ ->
							FinishTask = [TaskId||#elite_assess{task_id = TaskId, status = Status} <-TaskList,
								Status >= ?CAN_RECEIVE],
							?INFO_TTY("FinishTask is ~p~n",[FinishTask]),
							case erlang:length(FinishTask) >= (erlang:length(AllTask) - 1) of
								true ->
									CfgDunId = map_category_dun(CurrentCategory),
									case DunId == CfgDunId of
										true -> true;
										_ -> {false, ?ERRCODE(err_452_cannot_enter)}
									end;
								_ -> {false, ?ERRCODE(err_452_must_finish_other)}       %% todo
							end
					end;
				_ ->
					{false, ?ERRCODE(err_452_elite_dun_err)}        %% todo
			end;
		_ ->
			{false, ?ERRCODE(err_452_elite_data_err)}       %% todo
	end.

map_category_dun(CurrentCategory) ->
	case CurrentCategory of
		1 -> 3606;
		2 -> 3607;
		3 -> 3608;
		4 -> 3609;
		_ -> 0
	end.