using System;
using System.Diagnostics;
using System.Globalization;
using System.Threading;
using System.Windows;
using System.Windows.Threading;
using ZionRobot.Handlers;
using ZionRobot.Models.Source;
using ZionRobot.Models.ZFlow;

namespace ZionRobot.Tasks
{
	// Token: 0x0200001A RID: 26
	internal class Synchronizer
	{
		// Token: 0x1700001F RID: 31
		// (get) Token: 0x0600008E RID: 142 RVA: 0x0000585A File Offset: 0x00003A5A
		public bool Running
		{
			get
			{
				return this.running;
			}
		}

		// Token: 0x0600008F RID: 143 RVA: 0x00005862 File Offset: 0x00003A62
		public Synchronizer()
		{
			this.running = false;
		}

		// Token: 0x06000090 RID: 144 RVA: 0x0000588C File Offset: 0x00003A8C
		public void Execute(Origin source, Activity activity)
		{
			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.handler.Source = source;
									bool flag = activity.Action.ToUpper() == "UPDATE" && !string.IsNullOrEmpty(activity.DataJuri);
									if (flag)
									{
										this.handler.UpdateActivity(activity);
									}
									bool flag2 = activity.Action.ToUpper() == "CREATE";
									if (flag2)
									{
										this.handler.CreateSubActivity(activity);
									}
									bool flag3 = activity.Action.ToUpper() == "CREATECASE";
									if (flag3)
									{
										this.handler.CreateCase(activity);
									}
									bool flag4 = activity.Action.ToUpper() == "CLOSE";
									if (flag4)
									{
										this.handler.CloseCase(activity);
									}
									bool flag5 = activity.Action.ToUpper() == "SHAREPOINT";
									if (flag5)
									{
										this.handler.UploadAttachments(activity);
									}
								});
							}
							dispatcher.Invoke(callback);
						}
						catch (Exception ex)
						{
							throw ex;
						}
					});
				}
				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: 0x06000091 RID: 145 RVA: 0x000058FC File Offset: 0x00003AFC
		public void Execute(Origin source, Lawsuit lawsuit)
		{
			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.handler.Source = source;
									bool flag = lawsuit.Action.ToUpper() == "CREATE";
									if (flag)
									{
										this.handler.CreateLawsuit(lawsuit);
									}
								});
							}
							dispatcher.Invoke(callback);
						}
						catch (Exception ex)
						{
							throw ex;
						}
					});
				}
				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: 0x06000092 RID: 146 RVA: 0x0000596C File Offset: 0x00003B6C
		private string convertDate(string value)
		{
			return DateTime.Parse(value, new CultureInfo("pt-BR")).ToString("dd/MM/yyyy");
		}

		// Token: 0x04000033 RID: 51
		private bool running;

		// Token: 0x04000034 RID: 52
		private Browser handler;

		// Token: 0x04000035 RID: 53
		private int limitTime = 600;

		// Token: 0x04000036 RID: 54
		private Stopwatch stopwatch = new Stopwatch();
	}
}
