using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using Comfort.Common;
using EFT;
using EFT.Quests;
using EFT.UI;
using Microsoft.CSharp.RuntimeBinder;
using stupid.solutions;
using stupid.solutions.Utils;
using UnityEngine;

public class QuestModule : MonoBehaviour
{
	[CompilerGenerated]
	private static class _003C_003Eo__4
	{
		public static CallSite<Func<CallSite, object, object>> _003C_003Ep__0;

		public static CallSite<Func<CallSite, object, object, object>> _003C_003Ep__1;

		public static CallSite<Func<CallSite, object, bool>> _003C_003Ep__2;

		public static CallSite<Func<CallSite, object, object>> _003C_003Ep__3;

		public static CallSite<Func<CallSite, object, EQuestStatus, object>> _003C_003Ep__4;

		public static CallSite<Func<CallSite, object, bool>> _003C_003Ep__5;

		public static CallSite<Action<CallSite, object, object, bool>> _003C_003Ep__6;

		public static CallSite<Func<CallSite, object, object>> _003C_003Ep__7;

		public static CallSite<Func<CallSite, object, object>> _003C_003Ep__8;

		public static CallSite<Func<CallSite, _EA29, object, bool, object>> _003C_003Ep__9;

		public static CallSite<Action<CallSite, object, EQuestStatus, bool, bool>> _003C_003Ep__10;

		public static CallSite<Func<CallSite, object, object>> _003C_003Ep__11;

		public static CallSite<Func<CallSite, object, EQuestStatus, object>> _003C_003Ep__12;

		public static CallSite<Func<CallSite, object, bool>> _003C_003Ep__13;

		public static CallSite<Func<CallSite, object, object>> _003C_003Ep__14;

		public static CallSite<Func<CallSite, object, EQuestStatus, object>> _003C_003Ep__15;

		public static CallSite<Func<CallSite, object, object>> _003C_003Ep__16;

		public static CallSite<Func<CallSite, object, EQuestStatus, object>> _003C_003Ep__17;

		public static CallSite<Func<CallSite, object, object, object>> _003C_003Ep__18;

		public static CallSite<Func<CallSite, object, bool>> _003C_003Ep__19;

		public static CallSite<Func<CallSite, object, bool>> _003C_003Ep__20;

		public static CallSite<Func<CallSite, object, object>> _003C_003Ep__21;

		public static CallSite<Func<CallSite, object, EQuestStatus, object>> _003C_003Ep__22;

		public static CallSite<Func<CallSite, object, bool>> _003C_003Ep__23;

		public static CallSite<Func<CallSite, object, object>> _003C_003Ep__24;

		public static CallSite<Func<CallSite, object, object, object>> _003C_003Ep__25;

		public static CallSite<Func<CallSite, object, bool>> _003C_003Ep__26;

		public static CallSite<Func<CallSite, object, object>> _003C_003Ep__27;

		public static CallSite<Func<CallSite, object, object>> _003C_003Ep__28;

		public static CallSite<Func<CallSite, object, object>> _003C_003Ep__29;

		public static CallSite<Func<CallSite, string, object, object>> _003C_003Ep__30;

		public static CallSite<Func<CallSite, object, string, object>> _003C_003Ep__31;

		public static CallSite<Action<CallSite, Type, object>> _003C_003Ep__32;

		public static CallSite<Func<CallSite, object, object>> _003C_003Ep__33;

		public static CallSite<Func<CallSite, object, object>> _003C_003Ep__34;

		public static CallSite<Action<CallSite, object, object>> _003C_003Ep__35;

		public static CallSite<Func<CallSite, object, object>> _003C_003Ep__36;

		public static CallSite<Func<CallSite, object, object>> _003C_003Ep__37;

		public static CallSite<Action<CallSite, object, object>> _003C_003Ep__38;

		public static CallSite<Func<CallSite, object, object>> _003C_003Ep__39;

		public static CallSite<Func<CallSite, object, IEnumerable>> _003C_003Ep__40;

		public static CallSite<Func<CallSite, object, object>> _003C_003Ep__41;

		public static CallSite<Func<CallSite, object, object>> _003C_003Ep__42;

		public static CallSite<Func<CallSite, object, IEnumerable>> _003C_003Ep__43;

		public static CallSite<Action<CallSite, object, EQuestStatus, bool, bool>> _003C_003Ep__44;

		public static CallSite<Action<CallSite, object, EQuestStatus, bool, bool>> _003C_003Ep__45;

		public static CallSite<Action<CallSite, object, object, bool>> _003C_003Ep__46;

		public static CallSite<Func<CallSite, object, object>> _003C_003Ep__47;

		public static CallSite<Func<CallSite, object, object>> _003C_003Ep__48;

		public static CallSite<Func<CallSite, _EA29, object, bool, bool, object>> _003C_003Ep__49;

		public static CallSite<Func<CallSite, object, IResult>> _003C_003Ep__50;

		public static CallSite<Func<CallSite, object, IEnumerable>> _003C_003Ep__51;
	}

	[StructLayout(LayoutKind.Auto)]
	[CompilerGenerated]
	private struct _003CFinishQuests_003Ed__4 : IAsyncStateMachine
	{
		private static class _003C_003Eo__4
		{
			public static CallSite<Func<CallSite, object, object>> _003C_003Ep__0;

			public static CallSite<Func<CallSite, object, object>> _003C_003Ep__1;

			public static CallSite<Func<CallSite, object, bool>> _003C_003Ep__2;

			public static CallSite<Action<CallSite, object>> _003C_003Ep__3;

			public static CallSite<Func<CallSite, object, object>> _003C_003Ep__4;

			public static CallSite<Func<CallSite, object, object>> _003C_003Ep__5;

			public static CallSite<Func<CallSite, object, bool>> _003C_003Ep__6;

			public static CallSite<Func<CallSite, object, object>> _003C_003Ep__7;
		}

		public int _003C_003E1__state;

		public AsyncVoidMethodBuilder _003C_003Et__builder;

		private _EA29 _003Csession_003E5__2;

		private object _003CquestController_003E5__3;

		private IEnumerator _003C_003E7__wrap3;

		private object _003Cquest_003E5__5;

		private object _003C_003Eu__1;

		private int _003Ctries_003E5__6;

		private Func<CallSite, object, IResult> _003C_003E7__wrap6;

		private CallSite<Func<CallSite, object, IResult>> _003C_003E7__wrap7;

		private TaskAwaiter _003C_003Eu__2;

		private void MoveNext()
		{
			int num = _003C_003E1__state;
			try
			{
				if ((uint)num <= 2u)
				{
					goto IL_0132;
				}
				Player localPlayer = Main.LocalPlayer;
				if (!(localPlayer == null))
				{
					_tarkovApplication.GetClientBackEndSession();
					_003Csession_003E5__2 = _tarkovApplication.GetClientBackEndSession();
					if (_003Csession_003E5__2 != null)
					{
						FieldInfo fieldInfo = typeof(Player).GetRuntimeFields().FirstOrDefault((FieldInfo x) => x.Name == "_questController");
						_003CquestController_003E5__3 = fieldInfo.GetValue(localPlayer);
						dynamic val = ((dynamic)_003CquestController_003E5__3).Quests;
						_003C_003E7__wrap3 = ((IEnumerable)val).GetEnumerator();
						goto IL_0132;
					}
					ConsoleScreen.Log("Session is null");
				}
				goto end_IL_0007;
				IL_0132:
				try
				{
					TaskAwaiter awaiter;
					dynamic val2;
					switch (num)
					{
					case 0:
						val2 = _003C_003Eu__1;
						_003C_003Eu__1 = null;
						num = (_003C_003E1__state = -1);
						goto IL_05a0;
					case 1:
						try
						{
							if (num != 1)
							{
								_003Ctries_003E5__6 = 0;
								goto IL_1185;
							}
							dynamic val3 = _003C_003Eu__1;
							_003C_003Eu__1 = null;
							num = (_003C_003E1__state = -1);
							goto IL_157f;
							IL_157f:
							val2 = val3.GetResult();
							IResult result = _003C_003E7__wrap6(_003C_003E7__wrap7, (object)val2);
							_003C_003E7__wrap6 = null;
							_003C_003E7__wrap7 = null;
							if (!result.Succeed && _003Ctries_003E5__6 <= 15)
							{
								_003Ctries_003E5__6++;
								Thread.Sleep(500);
							}
							goto IL_1185;
							IL_1185:
							((dynamic)_003Cquest_003E5__5).SetStatus(EQuestStatus.AvailableForFinish, true, true);
							((dynamic)_003Cquest_003E5__5).SetStatus(EQuestStatus.Success, true, true);
							ConsoleScreen.Log("Sending Network Request");
							((dynamic)_003CquestController_003E5__3).FinishQuest((dynamic)_003Cquest_003E5__5, true);
							if (QuestModule._003C_003Eo__4._003C_003Ep__50 == null)
							{
								QuestModule._003C_003Eo__4._003C_003Ep__50 = CallSite<Func<CallSite, object, IResult>>.Create(Microsoft.CSharp.RuntimeBinder.Binder.Convert(CSharpBinderFlags.None, typeof(IResult), typeof(QuestModule)));
							}
							_003C_003E7__wrap6 = QuestModule._003C_003Eo__4._003C_003Ep__50.Target;
							_003C_003E7__wrap7 = QuestModule._003C_003Eo__4._003C_003Ep__50;
							val3 = _003Csession_003E5__2.QuestComplete(((dynamic)_003Cquest_003E5__5).Template.Id, false, false).GetAwaiter();
							if (!(bool)val3.IsCompleted)
							{
								num = (_003C_003E1__state = 1);
								_003C_003Eu__1 = val3;
								ICriticalNotifyCompletion awaiter2 = val3 as ICriticalNotifyCompletion;
								if (awaiter2 == null)
								{
									INotifyCompletion awaiter3 = (INotifyCompletion)(object)val3;
									_003C_003Et__builder.AwaitOnCompleted(ref awaiter3, ref this);
									awaiter3 = null;
								}
								else
								{
									_003C_003Et__builder.AwaitUnsafeOnCompleted(ref awaiter2, ref this);
								}
								awaiter2 = null;
								return;
							}
							goto IL_157f;
						}
						catch (Exception ex)
						{
							ConsoleScreen.Log(ex.ToString());
						}
						awaiter = Task.Delay(2500).GetAwaiter();
						if (!awaiter.IsCompleted)
						{
							num = (_003C_003E1__state = 2);
							_003C_003Eu__2 = awaiter;
							_003C_003Et__builder.AwaitUnsafeOnCompleted(ref awaiter, ref this);
							return;
						}
						goto IL_168e;
					case 2:
						awaiter = _003C_003Eu__2;
						_003C_003Eu__2 = default(TaskAwaiter);
						num = (_003C_003E1__state = -1);
						goto IL_168e;
					default:
						{
							while (_003C_003E7__wrap3.MoveNext())
							{
								_003Cquest_003E5__5 = _003C_003E7__wrap3.Current;
								if ((dynamic)_003Cquest_003E5__5 == null)
								{
									continue;
								}
								goto IL_01fe;
							}
							break;
						}
						IL_05a0:
						val2.GetResult();
						((dynamic)_003Cquest_003E5__5).SetStatus(EQuestStatus.Started, true, true);
						goto IL_0667;
						IL_0667:
						if (!((((dynamic)_003Cquest_003E5__5).QuestStatus == EQuestStatus.Success) ? true : false))
						{
							val2 = ((dynamic)_003Cquest_003E5__5).QuestStatus == EQuestStatus.Locked;
							if (!(val2 ? true : false) && !((val2 | (((dynamic)_003Cquest_003E5__5).QuestStatus == EQuestStatus.AvailableForStart)) ? true : false) && !((((dynamic)_003Cquest_003E5__5).QuestStatus != EQuestStatus.Started) ? true : false) && !((((dynamic)_003Cquest_003E5__5).Template == null) ? true : false))
							{
								ConsoleScreen.Log($"Trying to finish quest: {(object)((dynamic)_003Cquest_003E5__5).Template.Name}");
								IEnumerator enumerator = ((IEnumerable)((dynamic)_003Cquest_003E5__5).Template.Conditions).GetEnumerator();
								try
								{
									while (enumerator.MoveNext())
									{
										dynamic current = enumerator.Current;
										ConsoleScreen.Log("===== " + current.Key + " =====");
										IEnumerator enumerator2 = ((IEnumerable)current.Value).GetEnumerator();
										try
										{
											while (enumerator2.MoveNext())
											{
												dynamic current2 = enumerator2.Current;
												((dynamic)_003Cquest_003E5__5).CompletedConditions.Add(current2.id);
												if (!(current2 is ConditionCounterCreator conditionCounterCreator))
												{
													continue;
												}
												IEnumerator<Condition> enumerator3 = conditionCounterCreator.Conditions.GetEnumerator();
												try
												{
													while (enumerator3.MoveNext())
													{
														_ = enumerator3.Current;
														((dynamic)_003Cquest_003E5__5).CompletedConditions.Add(current2.id);
													}
												}
												finally
												{
													if (num < 0)
													{
														enumerator3?.Dispose();
													}
												}
											}
										}
										finally
										{
											if (num < 0 && enumerator2 is IDisposable disposable)
											{
												disposable.Dispose();
											}
										}
									}
								}
								finally
								{
									if (num < 0 && enumerator is IDisposable disposable2)
									{
										disposable2.Dispose();
									}
								}
								goto case 1;
							}
						}
						goto default;
						IL_01fe:
						if (((dynamic)_003Cquest_003E5__5).QuestStatus == EQuestStatus.AvailableAfter)
						{
							((dynamic)_003CquestController_003E5__3).AcceptQuest((dynamic)_003Cquest_003E5__5, true);
							val2 = _003Csession_003E5__2.QuestAccept(((dynamic)_003Cquest_003E5__5).Template.id, false).GetAwaiter();
							if (!(bool)val2.IsCompleted)
							{
								num = (_003C_003E1__state = 0);
								_003C_003Eu__1 = val2;
								ICriticalNotifyCompletion awaiter2 = val2 as ICriticalNotifyCompletion;
								if (awaiter2 == null)
								{
									INotifyCompletion awaiter3 = (INotifyCompletion)(object)val2;
									_003C_003Et__builder.AwaitOnCompleted(ref awaiter3, ref this);
									awaiter3 = null;
								}
								else
								{
									_003C_003Et__builder.AwaitUnsafeOnCompleted(ref awaiter2, ref this);
								}
								awaiter2 = null;
								return;
							}
							goto IL_05a0;
						}
						goto IL_0667;
						IL_168e:
						awaiter.GetResult();
						_003Cquest_003E5__5 = null;
						goto default;
					}
				}
				finally
				{
					if (num < 0 && _003C_003E7__wrap3 is IDisposable disposable3)
					{
						disposable3.Dispose();
					}
				}
				_003C_003E7__wrap3 = null;
				end_IL_0007:;
			}
			catch (Exception exception)
			{
				_003C_003E1__state = -2;
				_003Csession_003E5__2 = null;
				_003CquestController_003E5__3 = null;
				_003C_003Et__builder.SetException(exception);
				return;
			}
			_003C_003E1__state = -2;
			_003Csession_003E5__2 = null;
			_003CquestController_003E5__3 = null;
			_003C_003Et__builder.SetResult();
		}

		void IAsyncStateMachine.MoveNext()
		{
			//ILSpy generated this explicit interface implementation from .override directive in MoveNext
			this.MoveNext();
		}

		[DebuggerHidden]
		private void SetStateMachine(IAsyncStateMachine stateMachine)
		{
			_003C_003Et__builder.SetStateMachine(stateMachine);
		}

		void IAsyncStateMachine.SetStateMachine(IAsyncStateMachine stateMachine)
		{
			//ILSpy generated this explicit interface implementation from .override directive in SetStateMachine
			this.SetStateMachine(stateMachine);
		}
	}

	public static TarkovApplication _tarkovApplication;

	public bool finishquest;

	public void Update()
	{
		if (finishquest)
		{
			FinishQuests();
			finishquest = true;
		}
		if (_tarkovApplication == null)
		{
			_tarkovApplication = LocationsFixerV2._tarkovApplication;
		}
		if (!Settings.hidenick)
		{
			return;
		}
		_tarkovApplication.GetClientBackEndSession();
		_EA29 clientBackEndSession = _tarkovApplication.GetClientBackEndSession();
		if (!(_tarkovApplication == null) && clientBackEndSession != null && clientBackEndSession.Profile != null)
		{
			Profile profile = clientBackEndSession.Profile;
			string text = "Empty";
			if (profile.Nickname != text)
			{
				profile.Info.Nickname = text;
				ConsoleScreen.Log("NickPatched :-)");
			}
		}
	}

	public unsafe async void FinishQuests()
	{
		Player localPlayer = Main.LocalPlayer;
		if (localPlayer == null)
		{
			return;
		}
		_tarkovApplication.GetClientBackEndSession();
		_EA29 session = _tarkovApplication.GetClientBackEndSession();
		if (session == null)
		{
			ConsoleScreen.Log("Session is null");
			return;
		}
		FieldInfo fieldInfo = typeof(Player).GetRuntimeFields().FirstOrDefault((FieldInfo x) => x.Name == "_questController");
		dynamic questController = fieldInfo.GetValue(localPlayer);
		dynamic val = questController.Quests;
		AsyncVoidMethodBuilder asyncVoidMethodBuilder = default(AsyncVoidMethodBuilder);
		foreach (dynamic quest in val)
		{
			if (quest == null)
			{
				continue;
			}
			dynamic awaiter;
			if (quest.QuestStatus == EQuestStatus.AvailableAfter)
			{
				questController.AcceptQuest(quest, true);
				awaiter = session.QuestAccept(quest.Template.id, false).GetAwaiter();
				if (!(bool)awaiter.IsCompleted)
				{
					ICriticalNotifyCompletion awaiter2 = awaiter as ICriticalNotifyCompletion;
					if (awaiter2 == null)
					{
						INotifyCompletion awaiter3 = (INotifyCompletion)(object)awaiter;
						asyncVoidMethodBuilder.AwaitOnCompleted(ref awaiter3, ref *(_003CFinishQuests_003Ed__4*)/*Error near IL_0567: stateMachine*/);
					}
					else
					{
						asyncVoidMethodBuilder.AwaitUnsafeOnCompleted(ref awaiter2, ref *(_003CFinishQuests_003Ed__4*)/*Error near IL_057a: stateMachine*/);
					}
					/*Error near IL_0583: leave MoveNext - await not detected correctly*/;
				}
				awaiter.GetResult();
				quest.SetStatus(EQuestStatus.Started, true, true);
			}
			if (quest.QuestStatus == EQuestStatus.Success)
			{
				continue;
			}
			awaiter = quest.QuestStatus == EQuestStatus.Locked;
			if ((awaiter) || (awaiter | (quest.QuestStatus == EQuestStatus.AvailableForStart)) || (quest.QuestStatus != EQuestStatus.Started) || ((quest.Template == null) ? true : false))
			{
				continue;
			}
			ConsoleScreen.Log($"Trying to finish quest: {(object)quest.Template.Name}");
			foreach (dynamic item in quest.Template.Conditions)
			{
				ConsoleScreen.Log("===== " + item.Key + " =====");
				foreach (dynamic item2 in item.Value)
				{
					quest.CompletedConditions.Add(item2.id);
					if (!(item2 is ConditionCounterCreator conditionCounterCreator))
					{
						continue;
					}
					foreach (Condition condition in conditionCounterCreator.Conditions)
					{
						_ = condition;
						quest.CompletedConditions.Add(item2.id);
					}
				}
			}
			try
			{
				int tries = 0;
				dynamic awaiter4;
				while (true)
				{
					quest.SetStatus(EQuestStatus.AvailableForFinish, true, true);
					quest.SetStatus(EQuestStatus.Success, true, true);
					ConsoleScreen.Log("Sending Network Request");
					questController.FinishQuest(quest, true);
					if (_003C_003Eo__4._003C_003Ep__50 == null)
					{
						_003C_003Eo__4._003C_003Ep__50 = CallSite<Func<CallSite, object, IResult>>.Create(Microsoft.CSharp.RuntimeBinder.Binder.Convert(CSharpBinderFlags.None, typeof(IResult), typeof(QuestModule)));
					}
					Func<CallSite, object, IResult> target = _003C_003Eo__4._003C_003Ep__50.Target;
					CallSite<Func<CallSite, object, IResult>> _003C_003Ep__ = _003C_003Eo__4._003C_003Ep__50;
					awaiter4 = session.QuestComplete(quest.Template.Id, false, false).GetAwaiter();
					if (!(bool)awaiter4.IsCompleted)
					{
						break;
					}
					awaiter = awaiter4.GetResult();
					if (!target(_003C_003Ep__, (object)awaiter).Succeed && tries <= 15)
					{
						tries++;
						Thread.Sleep(500);
					}
				}
				ICriticalNotifyCompletion awaiter2 = awaiter4 as ICriticalNotifyCompletion;
				if (awaiter2 == null)
				{
					INotifyCompletion awaiter3 = (INotifyCompletion)(object)awaiter4;
					asyncVoidMethodBuilder.AwaitOnCompleted(ref awaiter3, ref *(_003CFinishQuests_003Ed__4*)/*Error near IL_1546: stateMachine*/);
				}
				else
				{
					asyncVoidMethodBuilder.AwaitUnsafeOnCompleted(ref awaiter2, ref *(_003CFinishQuests_003Ed__4*)/*Error near IL_1559: stateMachine*/);
				}
				/*Error near IL_1562: leave MoveNext - await not detected correctly*/;
			}
			catch (Exception ex)
			{
				ConsoleScreen.Log(ex.ToString());
			}
			await Task.Delay(2500);
		}
	}
}
