%%%-------------------------------------------------------------------
%%% @doc     精英考核(任务考核)
%%% Module  : lib_elite_assessment.erl
%%% Created : 2020-07-13
%%% @Author : tyl
%%%-------------------------------------------------------------------
-module(lib_elite_assess_trigger).

-include("elite_assessment.hrl").
-include("common.hrl").

-export([trigger/3]).

trigger([], _, EliteAssessInfo) -> EliteAssessInfo;
%%--------------------------------------------------
%% @doc 最新版任务 @end
%% date: 2021-07-21
%%--------------------------------------------------
trigger([{fast_dun, Wave}|L], [{fast_dun, _DunId, NeedWave}] = Condition, EliteAssessInfo) when is_integer(Wave) ->
	case Wave >= NeedWave of
		true ->
			NewProgress = NeedWave,
			NewStatus = ?CAN_RECEIVE,
			NewEliteAssessInfo = EliteAssessInfo#elite_assess{progress = NewProgress, status = NewStatus};
		false ->
			NewEliteAssessInfo = EliteAssessInfo#elite_assess{progress = Wave}
	end,
	trigger(L, Condition, NewEliteAssessInfo);
trigger([{normal_tower_dun, DunId}|L], [{normal_tower_dun, NeedDunId}] = Condition, EliteAssessInfo) when is_integer(DunId) ->
	ConfigMinDun = data_tower_dun:get_min_dun(),
	case DunId >= NeedDunId of
		true ->
			NewProgress = max(1, NeedDunId-ConfigMinDun+1),
			NewStatus = ?CAN_RECEIVE,
			NewEliteAssessInfo = EliteAssessInfo#elite_assess{progress = NewProgress, status = NewStatus};
		false ->
			Progress = max(1, DunId-ConfigMinDun+1),
			NewEliteAssessInfo = EliteAssessInfo#elite_assess{progress = Progress}
	end,
	trigger(L, Condition, NewEliteAssessInfo);
trigger([{diff_tower_dun, DunId}|L], [{diff_tower_dun, NeedDunId}] = Condition, EliteAssessInfo) when is_integer(DunId) ->
	case DunId >= NeedDunId of
		true ->
			NewProgress = 1,
			NewStatus = ?CAN_RECEIVE,
			NewEliteAssessInfo = EliteAssessInfo#elite_assess{progress = NewProgress, status = NewStatus};
		false ->
			NewEliteAssessInfo = EliteAssessInfo
	end,
	trigger(L, Condition, NewEliteAssessInfo);
trigger([{visus_dun, {DunId, Wave}}|L], [{visus_dun, NeedDunId, NeedWave}] = Condition, EliteAssessInfo) ->
	case DunId > NeedDunId of
		true ->
			NewProgress = NeedWave,
			NewStatus = ?CAN_RECEIVE,
			NewEliteAssessInfo = EliteAssessInfo#elite_assess{progress = NewProgress, status = NewStatus};
		false ->
			case DunId == NeedDunId of
				true ->
					case Wave >= NeedWave of
						true ->
							NewProgress = NeedWave,
							NewStatus = ?CAN_RECEIVE,
							NewEliteAssessInfo = EliteAssessInfo#elite_assess{progress = NewProgress, status = NewStatus};
						_ ->
							NewEliteAssessInfo = EliteAssessInfo#elite_assess{progress = Wave}
					end;
				_ ->
					NewEliteAssessInfo = EliteAssessInfo
			end
	end,
	trigger(L, Condition, NewEliteAssessInfo);
trigger([{person_boss, {Grade, Num}}|L], [{person_boss, NeedGrade, NeedNum}] = Condition, EliteAssessInfo) ->
	case Grade == NeedGrade of
		true ->
			case Num >= NeedNum of
				true ->
					NewProgress = 1,
					NewStatus = ?CAN_RECEIVE,
					NewEliteAssessInfo = EliteAssessInfo#elite_assess{progress = NewProgress, status = NewStatus};
				false ->
					NewEliteAssessInfo = EliteAssessInfo
			end,
			trigger(L, Condition, NewEliteAssessInfo);
		_ ->
			trigger(L, Condition, EliteAssessInfo)
	end;
trigger([{pet_dun, DunId}|L], [{pet_dun, NeedDunId}] = Condition, EliteAssessInfo) when is_integer(DunId) ->
	case DunId >= NeedDunId of
		true ->
			NewProgress = max(1, NeedDunId-12001+1),
			NewStatus = ?CAN_RECEIVE,
			NewEliteAssessInfo = EliteAssessInfo#elite_assess{progress = NewProgress, status = NewStatus};
		false ->
			NewProgress = max(1, DunId-12001+1),
			NewEliteAssessInfo = EliteAssessInfo#elite_assess{progress = NewProgress}
	end,
	trigger(L, Condition, NewEliteAssessInfo);
trigger([{reward_boss, BossId}|L], [{reward_boss, NeedBossId, NeedNum}] = Condition, EliteAssessInfo) ->
	#elite_assess{progress = Progress} = EliteAssessInfo,
	case BossId == NeedBossId of
		true ->
			NewProgress = Progress + 1,
			case NewProgress >= NeedNum of
				true ->
					NewStatus = ?CAN_RECEIVE,
					NewEliteAssessInfo = EliteAssessInfo#elite_assess{progress = min(NewProgress, NeedNum), status = NewStatus};
				_ ->
					NewEliteAssessInfo = EliteAssessInfo#elite_assess{progress = NewProgress}
			end;
		false ->
			NewEliteAssessInfo = EliteAssessInfo
	end,
	trigger(L, Condition, NewEliteAssessInfo);
trigger([{deserted_boss, BossId}|L], [{deserted_boss, NeedBossId, NeedNum}] = Condition, EliteAssessInfo) ->
	#elite_assess{progress = Progress} = EliteAssessInfo,
	case BossId == NeedBossId of
		true ->
			NewProgress = Progress + 1,
			case NewProgress >= NeedNum of
				true ->
					NewStatus = ?CAN_RECEIVE,
					NewEliteAssessInfo = EliteAssessInfo#elite_assess{progress = min(NewProgress, NeedNum), status = NewStatus};
				_ ->
					NewEliteAssessInfo = EliteAssessInfo#elite_assess{progress = NewProgress}
			end;
		false ->
			NewEliteAssessInfo = EliteAssessInfo
	end,
	trigger(L, Condition, NewEliteAssessInfo);
trigger([{enter_dungeon, DunId}|L], [{enter_dungeon, _DunType, NeedDunId}] = Condition, EliteAssessInfo) ->
	case DunId == NeedDunId of
		true ->
			NewProgress = 1,
			NewStatus = ?CAN_RECEIVE,
			NewEliteAssessInfo = EliteAssessInfo#elite_assess{progress = NewProgress, status = NewStatus};
		false ->
			NewEliteAssessInfo = EliteAssessInfo
	end,
	trigger(L, Condition, NewEliteAssessInfo);

trigger([{lv, Lv}|L], [{lv, NeedLv}] = Condition, EliteAssessInfo) when is_integer(Lv) ->
	case Lv >= NeedLv of
		true ->
			NewProgress = NeedLv,
			NewStatus = ?CAN_RECEIVE,
			NewEliteAssessInfo = EliteAssessInfo#elite_assess{progress = NewProgress, status = NewStatus};
		false ->
			NewEliteAssessInfo = EliteAssessInfo#elite_assess{progress = Lv}
	end,
	trigger(L, Condition, NewEliteAssessInfo);
trigger([{login_day, Day}|L], [{login_day, NeedDay}] = Condition, EliteAssessInfo) ->
	case Day >= NeedDay of
		true ->
			NewProgress = NeedDay,
			NewStatus = ?CAN_RECEIVE,
			NewEliteAssessInfo = EliteAssessInfo#elite_assess{progress = NewProgress, status = NewStatus};
		false ->
			NewEliteAssessInfo = EliteAssessInfo#elite_assess{progress = Day}
	end,
	trigger(L, Condition, NewEliteAssessInfo);
trigger([{power, Power}|L], [{power, NeedPower}] = Condition, EliteAssessInfo) ->
	case Power >= NeedPower of
		true ->
			NewProgress = NeedPower,
			NewStatus = ?CAN_RECEIVE,
			NewEliteAssessInfo = EliteAssessInfo#elite_assess{progress = NewProgress, status = NewStatus};
		false ->
			NewEliteAssessInfo = EliteAssessInfo#elite_assess{progress = Power}
	end,
	trigger(L, Condition, NewEliteAssessInfo);
trigger([{kill_mon, MonId}|L], [{kill_mon, NeedNum},{mon_id, NeedMon},{scene_id, _}] = Condition, EliteAssessInfo) ->
	#elite_assess{progress = Progress} = EliteAssessInfo,
	case MonId == NeedMon of
		true ->
			case Progress + 1 >= NeedNum of
				true ->
					NewProgress = NeedNum,
					NewStatus = ?CAN_RECEIVE,
					NewEliteAssessInfo = EliteAssessInfo#elite_assess{progress = NewProgress, status = NewStatus};
				false ->
					NewEliteAssessInfo = EliteAssessInfo#elite_assess{progress = Progress + 1}
			end;
		false ->
			NewEliteAssessInfo = EliteAssessInfo
	end,
	trigger(L, Condition, NewEliteAssessInfo);
trigger([{pass_dun, DunId}|L], [{pass_dun, NeedDunId}] = Condition, EliteAssessInfo) when is_integer(DunId) ->
	case DunId == NeedDunId of
		true ->
			NewProgress = 1,
			NewStatus = ?CAN_RECEIVE,
			NewEliteAssessInfo = EliteAssessInfo#elite_assess{progress = NewProgress, status = NewStatus};
		false ->
			NewEliteAssessInfo = EliteAssessInfo
	end,
	trigger(L, Condition, NewEliteAssessInfo);
trigger([{pass_dun_wave, {DunId, Wave}}|L], [{dun_wave, NeedDunWave},{dun_id, NeedDunId}] = Condition, EliteAssessInfo) ->
	case DunId == NeedDunId of
		true ->
			case Wave >= NeedDunWave of
				true ->
					NewProgress = NeedDunWave,
					NewStatus = ?CAN_RECEIVE,
					NewEliteAssessInfo = EliteAssessInfo#elite_assess{progress = NewProgress, status = NewStatus};
				false ->
					NewEliteAssessInfo = EliteAssessInfo#elite_assess{progress = Wave}
			end;
		_ ->
			NewEliteAssessInfo = EliteAssessInfo
	end,
	trigger(L, Condition, NewEliteAssessInfo);
trigger([{open_func, [{Module, SubModule}]}|L], [{open_func, NeedModule, NeedSubModule}] = Condition, EliteAssessInfo)  ->
	case Module == NeedModule andalso SubModule == NeedSubModule of
		true ->
			NewProgress = 1,
			NewStatus = ?CAN_RECEIVE,
			NewEliteAssessInfo = EliteAssessInfo#elite_assess{progress = NewProgress, status = NewStatus};
		_ ->
			NewEliteAssessInfo = EliteAssessInfo
	end,
	trigger(L, Condition, NewEliteAssessInfo);
trigger([{foster, {Foster, Stage}}|L], [{stage, NeedStage},{foster, NeedFoster}] = Condition, EliteAssessInfo) ->
	case Foster == NeedFoster of
		true ->
			case Stage >= NeedStage of
				true ->
					NewProgress = NeedStage,
					NewStatus = ?CAN_RECEIVE,
					NewEliteAssessInfo = EliteAssessInfo#elite_assess{progress = NewProgress, status = NewStatus};
				false ->
					NewEliteAssessInfo = EliteAssessInfo#elite_assess{progress = Stage}
			end;
		_ ->
			NewEliteAssessInfo = EliteAssessInfo
	end,
	trigger(L, Condition, NewEliteAssessInfo);
trigger([{activity_lv, Lv}|L], [{activity_lv, NeedLv}] = Condition, EliteAssessInfo) ->
	case Lv >= NeedLv of
		true ->
			NewProgress = NeedLv,
			NewStatus = ?CAN_RECEIVE,
			NewEliteAssessInfo = EliteAssessInfo#elite_assess{progress = NewProgress, status = NewStatus};
		false ->
			NewEliteAssessInfo = EliteAssessInfo#elite_assess{progress = Lv}
	end,
	trigger(L, Condition, NewEliteAssessInfo);
trigger([{equip_score, Score}|L], [{equip_score, NeedScore}] = Condition, EliteAssessInfo) ->
	case Score >= NeedScore of
		true ->
			NewProgress = NeedScore,
			NewStatus = ?CAN_RECEIVE,
			NewEliteAssessInfo = EliteAssessInfo#elite_assess{progress = NewProgress, status = NewStatus};
		false ->
			NewEliteAssessInfo = EliteAssessInfo#elite_assess{progress = Score}
	end,
	trigger(L, Condition, NewEliteAssessInfo);
trigger([{equip_wear, Data}|L], [{equip_num, NeedNum}, {equip_color, NeedColor}] = Condition, EliteAssessInfo) ->
	F = fun({_Suit, Color, Num}, Acc) ->
		if
			Color >= NeedColor ->
				Acc + Num;
			true ->
				Acc
		end
	    end,
	Sum = lists:foldl(F, 0, Data),
	case Sum >= NeedNum of
		true ->
			NewProgress = NeedNum,
			NewStatus = ?CAN_RECEIVE,
			NewEliteAssessInfo = EliteAssessInfo#elite_assess{progress = NewProgress, status = NewStatus};
		false ->
			NewEliteAssessInfo = EliteAssessInfo#elite_assess{progress = Sum}
	end,
	trigger(L, Condition, NewEliteAssessInfo);
trigger([{task, TaskId}|L], [{task, NeedTaskId}] = Condition, EliteAssessInfo) when is_integer(TaskId) ->
	case TaskId == NeedTaskId of
		true ->
			NewProgress = 1,
			NewStatus = ?CAN_RECEIVE,
			NewEliteAssessInfo = EliteAssessInfo#elite_assess{progress = NewProgress, status = NewStatus};
		false ->
			NewEliteAssessInfo = EliteAssessInfo
	end,
	trigger(L, Condition, NewEliteAssessInfo);
trigger([{world_boss, Num}|L], [{world_boss, NeedNum}] = Condition, EliteAssessInfo) ->
	#elite_assess{progress = Progress} = EliteAssessInfo,
	case Num + Progress >= NeedNum of
		true ->
			NewProgress = NeedNum,
			NewStatus = ?CAN_RECEIVE,
			NewEliteAssessInfo = EliteAssessInfo#elite_assess{progress = NewProgress, status = NewStatus};
		false ->
			NewEliteAssessInfo = EliteAssessInfo#elite_assess{progress = Num + Progress}
	end,
	trigger(L, Condition, NewEliteAssessInfo);
trigger([{market_buy, Num}|L], [{market_buy, NeedNum}] = Condition, EliteAssessInfo) ->
	#elite_assess{progress = Progress} = EliteAssessInfo,
	case Num + Progress >= NeedNum of
		true ->
			NewProgress = NeedNum,
			NewStatus = ?CAN_RECEIVE,
			NewEliteAssessInfo = EliteAssessInfo#elite_assess{progress = NewProgress, status = NewStatus};
		false ->
			NewEliteAssessInfo = EliteAssessInfo#elite_assess{progress = Num + Progress}
	end,
	trigger(L, Condition, NewEliteAssessInfo);
trigger([{market_show, Num}|L], [{market_show, NeedNum}] = Condition, EliteAssessInfo) ->
	#elite_assess{progress = Progress} = EliteAssessInfo,
	case Num + Progress >= NeedNum of
		true ->
			NewProgress = NeedNum,
			NewStatus = ?CAN_RECEIVE,
			NewEliteAssessInfo = EliteAssessInfo#elite_assess{progress = NewProgress, status = NewStatus};
		false ->
			NewEliteAssessInfo = EliteAssessInfo#elite_assess{progress = Num + Progress}
	end,
	trigger(L, Condition, NewEliteAssessInfo);
trigger([{multi_dun, {Type, Num}}|L], [{multi_dun, NeedNum}, {dun_type, NeedType}] = Condition, EliteAssessInfo) ->
	#elite_assess{progress = Progress} = EliteAssessInfo,
	case Type == NeedType of
		true ->
			case Num + Progress >= NeedNum of
				true ->
					NewProgress = NeedNum,
					NewStatus = ?CAN_RECEIVE,
					NewEliteAssessInfo = EliteAssessInfo#elite_assess{progress = NewProgress, status = NewStatus};
				false ->
					NewEliteAssessInfo = EliteAssessInfo#elite_assess{progress = Num + Progress}
			end,
			trigger(L, Condition, NewEliteAssessInfo);
		_ ->
			trigger(L, Condition, EliteAssessInfo)
	end;
trigger([{equip, Data}|L], [{equip, NeedNum}, {stage, NeedStage}] = Condition, EliteAssessInfo) ->
	F = fun({Stage, Num}, Acc) ->
		if
			Stage >= NeedStage ->
				Acc + Num;
			true ->
				Acc
		end end,
	Sum = lists:foldl(F, 0, Data),
	case Sum >= NeedNum of
		true ->
			NewProgress = NeedNum,
			NewStatus = ?CAN_RECEIVE,
			NewEliteAssessInfo = EliteAssessInfo#elite_assess{progress = NewProgress, status = NewStatus};
		false ->
			NewEliteAssessInfo = EliteAssessInfo#elite_assess{progress = Sum}
	end,
	trigger(L, Condition, NewEliteAssessInfo);
trigger([{equip_sum_star, Num}|L], [{equip_sum_star, NeedNum}] = Condition, EliteAssessInfo) ->
	case Num >= NeedNum of
		true ->
			NewProgress = NeedNum,
			NewStatus = ?CAN_RECEIVE,
			NewEliteAssessInfo = EliteAssessInfo#elite_assess{progress = NewProgress, status = NewStatus};
		false ->
			NewEliteAssessInfo = EliteAssessInfo#elite_assess{progress = Num}
	end,
	trigger(L, Condition, NewEliteAssessInfo);
trigger([{red_equip, EquipList}|L], [{red_equip, NeedNum}] = Condition, EliteAssessInfo) ->
	F = fun({_Lv, Num}, Acc) ->
			Acc + Num
	    end,
	Sum = lists:foldl(F, 0, EquipList),
	case Sum >= NeedNum of
		true ->
			NewProgress = NeedNum,
			NewStatus = ?CAN_RECEIVE,
			NewEliteAssessInfo = EliteAssessInfo#elite_assess{progress = NewProgress, status = NewStatus};
		false ->
			NewEliteAssessInfo = EliteAssessInfo#elite_assess{progress = Sum}
	end,
	trigger(L, Condition, NewEliteAssessInfo);
trigger([{war_soul, Num}|L], [{war_soul, NeedNum}] = Condition, EliteAssessInfo) ->
	case Num >= NeedNum of
		true ->
			NewProgress = NeedNum,
			NewStatus = ?CAN_RECEIVE,
			NewEliteAssessInfo = EliteAssessInfo#elite_assess{progress = NewProgress, status = NewStatus};
		false ->
			NewEliteAssessInfo = EliteAssessInfo#elite_assess{progress = Num}
	end,
	trigger(L, Condition, NewEliteAssessInfo);
trigger([{turn, Num}|L], [{turn, NeedNum}] = Condition, EliteAssessInfo) ->
	case Num >= NeedNum of
		true ->
			NewProgress = NeedNum,
			NewStatus = ?CAN_RECEIVE,
			NewEliteAssessInfo = EliteAssessInfo#elite_assess{progress = NewProgress, status = NewStatus};
		false ->
			NewEliteAssessInfo = EliteAssessInfo#elite_assess{progress = Num}
	end,
	trigger(L, Condition, NewEliteAssessInfo);
trigger([{uranium, Num}|L], [{uranium, NeedNum}] = Condition, EliteAssessInfo) ->
	case Num >= NeedNum of
		true ->
			NewProgress = NeedNum,
			NewStatus = ?CAN_RECEIVE,
			NewEliteAssessInfo = EliteAssessInfo#elite_assess{progress = NewProgress, status = NewStatus};
		false ->
			NewEliteAssessInfo = EliteAssessInfo#elite_assess{progress = Num}
	end,
	trigger(L, Condition, NewEliteAssessInfo);
trigger([_|L], Condition, AchievementInfo) ->
	trigger(L, Condition, AchievementInfo).