%%%-------------------------------------------------------------------
%%% @doc 境界学位--检查模块
%%% Module: lib_degree_check.erl
%%% Created : 2019-11-25
%%% @Author: tyl
%%%-------------------------------------------------------------------
-module(lib_degree_check).

-include("common.hrl").
-include("server.hrl").
-include("degree.hrl").
-include("figure.hrl").
-include("errcode.hrl").

-export([
	check_upgrade/2,
	check_receive/2
]).

%% 学位等级晋升检查
check_upgrade([], Player) -> {true, Player};
check_upgrade([Check|L], Player) ->
	case do_check_upgrade(Check, Player) of
		{true, NewPlayer} -> check_upgrade(L, NewPlayer);
		{false, Res, _NewPlayer} -> {false, Res}
	end.

%% 领取日常功课奖励检查
check_receive([], Player) -> {true, Player};
check_receive([Check|L], Player) ->
	case do_check_receive(Check, Player) of
		{true, NewPlayer} -> check_receive(L, NewPlayer);
		{false, Res, _NewPlayer} -> {false, Res}
	end.

do_check_upgrade({need_exp, NowExp}, Player) ->
	#player_status{degree = Degree} = Player,
	#degree{degree_id = DegreeId} = Degree,
	NewDegreeId = DegreeId+1,
	case data_degree:get_degree_cfg(NewDegreeId) of
		#degree_cfg{need_exp = NeedExp} ->
			if
				NowExp >= NeedExp ->
					{true, Player};
				true -> {false, ?ERRCODE(err166_exp_no_enough), Player}
			end;
		_ -> {false, ?ERRCODE(err166_max_degree), Player}
	end;
do_check_upgrade({finish_dun, DunState, DegreeId}, Player) ->
	{NowDunId, NowState} =
	case DunState of
		[{DunId, State}] -> {DunId, State};
		_ -> {0, 0}
	end,
	case data_degree:get_degree_cfg(DegreeId+1) of
		#degree_cfg{finish_dungeon = TDunId} ->
			if
				NowDunId == TDunId andalso NowState == 1 -> {true, Player};
				true -> {false, ?ERRCODE(err166_dun_unfinish), Player}
			end;
		_ -> {false, ?ERRCODE(err166_max_degree), Player}

	end;
do_check_upgrade({collect_material, DegreeId}, Player) ->
	NewDegreeId = DegreeId+1,
	case data_degree:get_degree_cfg(NewDegreeId) of
		[] -> {false, ?ERRCODE(err166_max_degree), Player};
		#degree_cfg{need_matrial = NeedMaterial} ->
			[{_Type, GTypeId, Num}| _] = NeedMaterial,
			[{_, OwnNum}] = lib_goods_api:get_goods_num(Player, [GTypeId]),
			case OwnNum >= Num of
				true -> {true, Player};
				_ -> {false, ?ERRCODE(err166_matrial_no_enough), Player}
			end
	end;
do_check_upgrade(_Check, _Player) ->
	?ERR("<<Error check_upgrade>>").

do_check_receive({has_receive, ClassId, ClassStatusMap}, Player) ->
	case maps:get(ClassId, ClassStatusMap, []) of
		State when is_integer(State) ->
			case State == ?ClassFinish of
				true -> {false, ?ERRCODE(err166_reward_received), Player};
				_ -> {true, Player}
			end;
		_ -> {false, ?ERRCODE(err166_classwork_no_get), Player}
	end;
do_check_receive({finish_classwork, ClassId, ClassProgressMap}, Player) ->
	case maps:get(ClassId, ClassProgressMap, []) of
		[_Type, _Id, Num] ->
			case data_degree:get_degree_timetable_cfg(ClassId) of
				#degree_timetable_cfg{target = [{_TargetType, _TargetId, TargetNum}]} ->
					case Num >= TargetNum of
						true -> {true, Player};
						false -> {false, ?ERRCODE(err166_classwork_unfinish), Player}
					end;
				_ -> {false, ?ERRCODE(err166_classwork_no_exit), Player}
			end;
		_ -> {false, ?ERRCODE(err166_classwork_no_get), Player}
	end;
do_check_receive(_Check, _Player) ->
	?ERR("<<Error check_receive>>").
