using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using ZionFoundation.Threading;
using ZionRobot.Models.Items;
using ZionRobot.Models.Process;
using ZionRobot.Models.Source;
using ZionRobot.Models.ZFlow;
using ZionRobot.Persistence;
using ZionRobot.WebService;
using ZionVault.Binder.Output;

namespace ZionRobot.Tasks
{
	// Token: 0x0200001F RID: 31
	internal class JobManager : ZionFoundation.Threading.Thread
	{
		// Token: 0x1700002F RID: 47
		// (get) Token: 0x060000AD RID: 173 RVA: 0x00005EC7 File Offset: 0x000040C7
		// (set) Token: 0x060000AE RID: 174 RVA: 0x00005ECF File Offset: 0x000040CF
		internal Workflow Workflow
		{
			get
			{
				return this.workflow;
			}
			set
			{
				this.workflow = value;
			}
		}

		// Token: 0x060000AF RID: 175 RVA: 0x00005ED8 File Offset: 0x000040D8
		public JobManager(Workflow workflow, bool execute = true)
		{
			this.workflow = workflow;
			this.interval = 60000;
			this.TInput = new Input();
			this.TExtractor = new Extractor();
			this.TOutput = new Output();
			this.TSynchronizer = new Synchronizer();
			this.workflow.Folders.Add(new Folder(Basic.SearchResultFolder, true));
			base.Initilize(execute);
			this.workflow.Queue.Start();
		}

		// Token: 0x060000B0 RID: 176 RVA: 0x00005F6C File Offset: 0x0000416C
		public override void Execute()
		{
			bool flag = !this.tasksIsAlive();
			if (flag)
			{
				bool flag2 = this.sync == "Local";
				if (flag2)
				{
					string rootPath = "C:\\Temp\\html\\";
					Origin source = this.workflow.SyncConfigurations[0];
					Directory.GetFiles(rootPath + "andamentos", "*.json").ToList<string>().ForEach(delegate(string file)
					{
						string fileName = Path.GetFileName(file);
						this.TSynchronizer.SyncFromLocal(source, file, "Andamentos");
						while (this.TSynchronizer.Running)
						{
							System.Threading.Thread.Sleep(2000);
						}
						System.IO.File.Move(file, rootPath + "sincronizados\\andamentos\\" + fileName);
					});
				}
				bool flag3 = this.sync == "Demo";
				if (flag3)
				{
					this.workflow.OutputConfigurations.ForEach(delegate(Origin source)
					{
						this.TOutput.Execute(source);
						while (this.TOutput.Running)
						{
							System.Threading.Thread.Sleep(2000);
						}
					});
				}
				bool flag4 = this.sync == "Publications";
				if (flag4)
				{
					this.workflow.InputConfigurations.ForEach(delegate(Origin source)
					{
						this.TInput.Execute(source);
						while (this.TInput.Running)
						{
							System.Threading.Thread.Sleep(2000);
						}
					});
					List<ZionRobot.Models.Items.File> filesExtraction = this.workflow.Queues.Extraction;
					bool flag5 = filesExtraction.Count > 0;
					if (flag5)
					{
						filesExtraction.ForEach(delegate(ZionRobot.Models.Items.File file)
						{
							this.TExtractor.Execute(file, this.workflow.Blueprints);
						});
						filesExtraction.Clear();
					}
					List<ZionRobot.Models.Items.File> filesIntegration = new List<ZionRobot.Models.Items.File>();
					using (List<ZionRobot.Models.Items.File>.Enumerator enumerator = this.workflow.Queues.Integration.GetEnumerator())
					{
						while (enumerator.MoveNext())
						{
							ZionRobot.Models.Items.File file = enumerator.Current;
							bool flag6 = string.IsNullOrEmpty(file.LawsuitNumber);
							if (flag6)
							{
								filesIntegration.Add(file);
							}
							else
							{
								ZionRobot.Models.Items.File fi = (from f in filesIntegration
								where f.LawsuitNumber == file.LawsuitNumber
								select f).FirstOrDefault<ZionRobot.Models.Items.File>();
								bool flag7 = fi != null;
								if (flag7)
								{
									Action<ZionVault.Binder.Output.Field> <>9__7;
									fi.METAObject.Pages.ForEach(delegate(Page page)
									{
										List<ZionVault.Binder.Output.Field> list = page.SearchElements.ToList<ZionVault.Binder.Output.Field>();
										Action<ZionVault.Binder.Output.Field> action;
										if ((action = <>9__7) == null)
										{
											action = (<>9__7 = delegate(ZionVault.Binder.Output.Field element)
											{
												bool flag16 = element.Name == "ConteudoCompleto";
												if (flag16)
												{
													element.Value = string.Format("{0}<hr>{1}", element.Value, file.PublicationContent);
												}
											});
										}
										list.ForEach(action);
									});
									fi.METAObject.Save(fi.META, false, false);
									file.METAObject.Status = "GROUPED";
									file.METAObject.Save(file.META, false, false);
									file.Status = "INTEGRATED";
								}
								else
								{
									filesIntegration.Add(file);
								}
							}
						}
					}
					bool flag8 = filesIntegration.Count > 0;
					if (flag8)
					{
						this.workflow.OutputConfigurations.ForEach(delegate(Origin source)
						{
							filesIntegration.ForEach(delegate(ZionRobot.Models.Items.File file)
							{
								this.TOutput.Execute(source, file);
								while (this.TOutput.Running)
								{
									System.Threading.Thread.Sleep(2000);
								}
							});
						});
					}
				}
				bool flag9 = this.sync == "Lawsuits";
				if (flag9)
				{
					List<ZionRobot.Models.ZFlow.Lawsuit> lawsuitsToSync = new ZionRobot.WebService.Lawsuit().LawsuitsToSync();
					bool flag10 = lawsuitsToSync.Count > 0;
					if (flag10)
					{
						this.workflow.SyncConfigurations.ForEach(delegate(Origin source)
						{
							lawsuitsToSync.ForEach(delegate(ZionRobot.Models.ZFlow.Lawsuit lawsuit)
							{
								this.TSynchronizer.Execute(source, lawsuit);
								while (this.TSynchronizer.Running)
								{
									System.Threading.Thread.Sleep(2000);
								}
							});
						});
					}
					List<ZionRobot.Models.ZFlow.LawsuitPhase> lawsuitPhasesToSync = new ZionRobot.WebService.LawsuitPhase().LawsuitPhasesToSync();
					bool flag11 = lawsuitPhasesToSync.Count > 0;
					if (flag11)
					{
						this.workflow.SyncConfigurations.ForEach(delegate(Origin source)
						{
							List<ZionRobot.Models.ZFlow.LawsuitPhase> x = (from l in lawsuitPhasesToSync
							where l.SyncAction == "Create"
							select l).ToList<ZionRobot.Models.ZFlow.LawsuitPhase>();
							x.ForEach(delegate(ZionRobot.Models.ZFlow.LawsuitPhase lawsuitPhase)
							{
								bool flag16 = lawsuitPhase.SyncAction == "Create";
								if (flag16)
								{
									this.TSynchronizer.Execute(source, lawsuitPhase);
									while (this.TSynchronizer.Running)
									{
										System.Threading.Thread.Sleep(2000);
									}
								}
							});
						});
					}
					List<ZionRobot.Models.ZFlow.LawsuitPhaseProgress> lawsuitPhaseProgressToSync = new ZionRobot.WebService.LawsuitPhaseProgress().LawsuitPhaseProgressToSync();
					bool flag12 = lawsuitPhaseProgressToSync.Count > 0;
					if (flag12)
					{
						this.workflow.SyncConfigurations.ForEach(delegate(Origin source)
						{
							List<ZionRobot.Models.ZFlow.LawsuitPhaseProgress> x = (from l in lawsuitPhaseProgressToSync
							where l.SyncAction == "Create"
							select l).ToList<ZionRobot.Models.ZFlow.LawsuitPhaseProgress>();
							x.ForEach(delegate(ZionRobot.Models.ZFlow.LawsuitPhaseProgress lawsuitPhaseProgress)
							{
								bool flag16 = lawsuitPhaseProgress.SyncAction == "Create";
								if (flag16)
								{
									this.TSynchronizer.Execute(source, lawsuitPhaseProgress);
									while (this.TSynchronizer.Running)
									{
										System.Threading.Thread.Sleep(2000);
									}
								}
							});
						});
					}
					List<ZionRobot.Models.ZFlow.LawsuitDemand> lawsuitDemandsToSync = new ZionRobot.WebService.LawsuitDemand().LawsuitDemandsToSync();
					bool flag13 = lawsuitDemandsToSync.Count > 0;
					if (flag13)
					{
						this.workflow.SyncConfigurations.ForEach(delegate(Origin source)
						{
							List<ZionRobot.Models.ZFlow.LawsuitDemand> x = (from l in lawsuitDemandsToSync
							where l.SyncAction == "Create"
							select l).ToList<ZionRobot.Models.ZFlow.LawsuitDemand>();
							x.ForEach(delegate(ZionRobot.Models.ZFlow.LawsuitDemand lawsuitDemand)
							{
								bool flag16 = lawsuitDemand.SyncAction == "Create";
								if (flag16)
								{
									this.TSynchronizer.Execute(source, lawsuitDemand);
									while (this.TSynchronizer.Running)
									{
										System.Threading.Thread.Sleep(2000);
									}
								}
							});
						});
					}
				}
				bool flag14 = this.sync == "Activities";
				if (flag14)
				{
					List<ZionRobot.Models.ZFlow.Activity> activitiesToSync = new ZionRobot.WebService.Activity().ActivityToSync();
					bool flag15 = activitiesToSync.Count > 0;
					if (flag15)
					{
						this.workflow.SyncConfigurations.ForEach(delegate(Origin source)
						{
							activitiesToSync.ForEach(delegate(ZionRobot.Models.ZFlow.Activity activity)
							{
								this.TSynchronizer.Execute(source, activity);
								while (this.TSynchronizer.Running)
								{
									System.Threading.Thread.Sleep(2000);
								}
							});
						});
					}
				}
			}
		}

		// Token: 0x060000B1 RID: 177 RVA: 0x00006414 File Offset: 0x00004614
		private bool tasksIsAlive()
		{
			return this.TInput.Running || this.TExtractor.Running || this.TOutput.Running || this.TSynchronizer.Running;
		}

		// Token: 0x0400003A RID: 58
		private Workflow workflow;

		// Token: 0x0400003B RID: 59
		private Input TInput;

		// Token: 0x0400003C RID: 60
		private Extractor TExtractor;

		// Token: 0x0400003D RID: 61
		private Output TOutput;

		// Token: 0x0400003E RID: 62
		private Synchronizer TSynchronizer;

		// Token: 0x0400003F RID: 63
		private string sync = "Activities";
	}
}
