using System;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading;
using System.Windows;
using System.Windows.Threading;
using ZionRobot.Handlers;
using ZionRobot.Models.Items;
using ZionRobot.Models.Source;
using ZionRobot.Persistence;
using ZionVault.Binder.Output;

namespace ZionRobot.Tasks
{
	// Token: 0x02000018 RID: 24
	internal class Output
	{
		// Token: 0x1700001E RID: 30
		// (get) Token: 0x06000082 RID: 130 RVA: 0x00005624 File Offset: 0x00003824
		public bool Running
		{
			get
			{
				return this.running;
			}
		}

		// Token: 0x06000083 RID: 131 RVA: 0x0000562C File Offset: 0x0000382C
		public Output()
		{
			this.running = false;
		}

		// Token: 0x06000084 RID: 132 RVA: 0x00005654 File Offset: 0x00003854
		public void Execute(Origin source, ZionRobot.Models.Items.File file)
		{
			this.running = true;
			this.stopwatch.Start();
			Application.Current.Dispatcher.Invoke(delegate()
			{
				this.handler = Browser.Instance;
			});
			System.Action <>9__4;
			ThreadStart <>9__2;
			System.Action <>9__5;
			ThreadStart <>9__3;
			new Thread(delegate()
			{
				ThreadStart start;
				if ((start = <>9__2) == null)
				{
					start = (<>9__2 = delegate()
					{
						try
						{
							Dispatcher dispatcher = Application.Current.Dispatcher;
							System.Action callback;
							if ((callback = <>9__4) == null)
							{
								callback = (<>9__4 = delegate()
								{
									this.map(file);
									this.handler.Source = source;
									Directory.CreateDirectory(Path.Combine(Basic.DoneFolder, source.Title));
									this.handler.ExecuteAndMove(file, Path.Combine(Basic.DoneFolder, source.Title));
								});
							}
							dispatcher.Invoke(callback);
						}
						catch (Exception ex)
						{
							file.Status = "ERROR";
						}
					});
				}
				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 flag = this.stopwatch.Elapsed.TotalSeconds > (double)this.limitTime;
							if (flag)
							{
								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;
					});
				}
				new Thread(start2).Start();
			}).Start();
		}

		// Token: 0x06000085 RID: 133 RVA: 0x000056C4 File Offset: 0x000038C4
		private void map(ZionRobot.Models.Items.File file)
		{
			(from p in file.METAObject.Pages
			select p.SearchElements).ToList<ConcurrentBag<Field>>().ForEach(delegate(ConcurrentBag<Field> PageElements)
			{
				PageElements.ToList<Field>().ForEach(delegate(Field f)
				{
					bool flag = f.Type == "Date";
					if (flag)
					{
						f.Value = DateTime.Parse(f.Value).ToString("dd/MM/yyyy");
					}
					this.handler.DictionaryValues.Add(f.FullName, f.Value);
				});
			});
		}

		// Token: 0x0400002D RID: 45
		private bool running;

		// Token: 0x0400002E RID: 46
		private Browser handler;

		// Token: 0x0400002F RID: 47
		private int limitTime = 1200;

		// Token: 0x04000030 RID: 48
		private Stopwatch stopwatch = new Stopwatch();
	}
}
