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: 0x0200001F RID: 31
	internal class Input
	{
		// Token: 0x1700002E RID: 46
		// (get) Token: 0x060000AF RID: 175 RVA: 0x000061EC File Offset: 0x000043EC
		public bool Running
		{
			get
			{
				return this.running;
			}
		}

		// Token: 0x060000B0 RID: 176 RVA: 0x000061F4 File Offset: 0x000043F4
		public Input()
		{
			this.running = false;
		}

		// Token: 0x060000B1 RID: 177 RVA: 0x0000621C File Offset: 0x0000441C
		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: 0x0400003C RID: 60
		private bool running;

		// Token: 0x0400003D RID: 61
		private Browser handler;

		// Token: 0x0400003E RID: 62
		private int limitTime = 960;

		// Token: 0x0400003F RID: 63
		private Stopwatch stopwatch = new Stopwatch();
	}
}
