using System;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
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: 0x02000022 RID: 34
	internal class Synchronizer
	{
		// Token: 0x17000031 RID: 49
		// (get) Token: 0x060000C2 RID: 194 RVA: 0x0000678A File Offset: 0x0000498A
		public bool Running
		{
			get
			{
				return this.running;
			}
		}

		// Token: 0x060000C3 RID: 195 RVA: 0x00006792 File Offset: 0x00004992
		public Synchronizer()
		{
			this.running = false;
		}

		// Token: 0x060000C4 RID: 196 RVA: 0x000067BC File Offset: 0x000049BC
		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);
									}
									else
									{
										bool flag2 = activity.Action.ToUpper() == "CREATE";
										if (flag2)
										{
											this.handler.CreateSubActivity(activity);
										}
										else
										{
											bool flag3 = activity.Action.ToUpper() == "CREATECASE";
											if (flag3)
											{
												this.handler.CreateCase(activity);
											}
											else
											{
												bool flag4 = activity.Action.ToUpper() == "CLOSE";
												if (flag4)
												{
													this.handler.CloseCase(activity);
												}
												else
												{
													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: 0x060000C5 RID: 197 RVA: 0x0000682C File Offset: 0x00004A2C
		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.SyncAction.ToUpper() == "UPDATE";
									if (flag)
									{
										this.handler.UpdateLawsuit(lawsuit);
									}
									bool flag2 = lawsuit.SyncAction.ToUpper() == "CREATE";
									if (flag2)
									{
										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: 0x060000C6 RID: 198 RVA: 0x0000689C File Offset: 0x00004A9C
		public void Execute(Origin source, LawsuitPhase lawsuitPhase)
		{
			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 = lawsuitPhase.SyncAction.ToUpper() == "CREATE";
									if (flag)
									{
										this.handler.CreateLawsuitPhase(lawsuitPhase);
									}
								});
							}
							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: 0x060000C7 RID: 199 RVA: 0x0000690C File Offset: 0x00004B0C
		public void Execute(Origin source, LawsuitPhaseProgress lawsuitPhaseProgress)
		{
			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 = lawsuitPhaseProgress.SyncAction.ToUpper() == "CREATE";
									if (flag)
									{
										this.handler.CreateLawsuitPhaseProgress(lawsuitPhaseProgress);
									}
								});
							}
							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: 0x060000C8 RID: 200 RVA: 0x0000697C File Offset: 0x00004B7C
		public void Execute(Origin source, LawsuitDemand lawsuitDemand)
		{
			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 = lawsuitDemand.SyncAction.ToUpper() == "CREATE";
									if (flag)
									{
										this.handler.CreateLawsuitDemand(lawsuitDemand);
									}
								});
							}
							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: 0x060000C9 RID: 201 RVA: 0x000069EC File Offset: 0x00004BEC
		public void SyncFromLocal(Origin source, string json, string type)
		{
			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__9;
			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;
									BlockElement blockElement = null;
									bool flag = type.ToUpper() == "ANDAMENTOS";
									if (flag)
									{
										blockElement = (from be in source.BlockElements
										where be.Name == "CreateLawsuitPhaseProgressLocal"
										select be).FirstOrDefault<BlockElement>();
									}
									bool flag2 = type.ToUpper() == "DEPOSITOS";
									if (flag2)
									{
										blockElement = (from be in source.BlockElements
										where be.Name == "CreateLawsuitDepositLocal"
										select be).FirstOrDefault<BlockElement>();
									}
									bool flag3 = type.ToUpper() == "PEDIDOS";
									if (flag3)
									{
										blockElement = (from be in source.BlockElements
										where be.Name == "CreateLawsuitDemandLocal"
										select be).FirstOrDefault<BlockElement>();
									}
									bool flag4 = type.ToUpper() == "PROCESSOS";
									if (flag4)
									{
										blockElement = (from be in source.BlockElements
										where be.Name == "CreateLawsuitLocal"
										select be).FirstOrDefault<BlockElement>();
									}
									this.handler.CreateWithLocal(json, blockElement);
								});
							}
							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__9) == null)
						{
							callback = (<>9__9 = delegate()
							{
								this.handler.Close();
							});
						}
						dispatcher.Invoke(callback);
						this.running = false;
					});
				}
				new Thread(start2).Start();
			}).Start();
		}

		// Token: 0x060000CA RID: 202 RVA: 0x00006A64 File Offset: 0x00004C64
		private string convertDate(string value)
		{
			return DateTime.Parse(value, new CultureInfo("pt-BR")).ToString("dd/MM/yyyy");
		}

		// Token: 0x04000046 RID: 70
		private bool running;

		// Token: 0x04000047 RID: 71
		private Browser handler;

		// Token: 0x04000048 RID: 72
		private int limitTime = 300;

		// Token: 0x04000049 RID: 73
		private Stopwatch stopwatch = new Stopwatch();
	}
}
