using System;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Windows;
using System.Windows.Threading;
using ZionRobot.Handlers;
using ZionRobot.Models.Source;
using ZionRobot.Persistence;

namespace ZionRobot.Tasks
{
	// Token: 0x02000016 RID: 22
	internal class Input
	{
		// Token: 0x1700001C RID: 28
		// (get) Token: 0x06000078 RID: 120 RVA: 0x00005224 File Offset: 0x00003424
		public bool Running
		{
			get
			{
				return this.running;
			}
		}

		// Token: 0x06000079 RID: 121 RVA: 0x0000522C File Offset: 0x0000342C
		public Input()
		{
			this.running = false;
		}

		// Token: 0x0600007A RID: 122 RVA: 0x00005254 File Offset: 0x00003454
		public void Execute(Origin source)
		{
			bool flag = !this.running;
			if (flag)
			{
				this.running = true;
				this.stopwatch.Start();
				Application.Current.Dispatcher.Invoke(delegate()
				{
					this.handler = Browser.Instance;
				});
				ThreadStart <>9__2;
				System.Action <>9__5;
				ThreadStart <>9__3;
				new Thread(delegate()
				{
					ThreadStart start;
					if ((start = <>9__2) == null)
					{
						start = (<>9__2 = delegate()
						{
							string outputFolder = Path.Combine(Basic.SearchResultFolder, source.OutputFolder, DateTime.Now.ToString("yyyyMMdd"));
							Application.Current.Dispatcher.Invoke(delegate()
							{
								this.handler.Source = source;
								this.handler.ExecuteAndSave(outputFolder);
							});
						});
					}
					Thread t = new Thread(start);
					t.Start();
					ThreadStart start2;
					if ((start2 = <>9__3) == null)
					{
						start2 = (<>9__3 = delegate()
						{
							while (this.handler.Main == null || this.handler.Main.IsAlive)
							{
								bool flag2 = this.stopwatch.Elapsed.TotalSeconds > (double)this.limitTime;
								if (flag2)
								{
									break;
								}
								Thread.Sleep(1000);
							}
							this.stopwatch.Stop();
							this.stopwatch.Reset();
							Dispatcher dispatcher = Application.Current.Dispatcher;
							System.Action callback;
							if ((callback = <>9__5) == null)
							{
								callback = (<>9__5 = delegate()
								{
									this.handler.Close();
								});
							}
							dispatcher.Invoke(callback);
							this.running = false;
							GC.Collect();
							GC.WaitForPendingFinalizers();
							GC.Collect();
						});
					}
					new Thread(start2).Start();
				}).Start();
			}
		}

		// Token: 0x04000024 RID: 36
		private bool running;

		// Token: 0x04000025 RID: 37
		private Browser handler;

		// Token: 0x04000026 RID: 38
		private int limitTime = 960;

		// Token: 0x04000027 RID: 39
		private Stopwatch stopwatch = new Stopwatch();
	}
}
