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

namespace ZionRobot.Tasks
{
	// Token: 0x02000017 RID: 23
	internal class JobManager : ZionFoundation.Threading.Thread
	{
		// Token: 0x1700001D RID: 29
		// (get) Token: 0x0600007B RID: 123 RVA: 0x000052CB File Offset: 0x000034CB
		// (set) Token: 0x0600007C RID: 124 RVA: 0x000052D3 File Offset: 0x000034D3
		internal Workflow Workflow
		{
			get
			{
				return this.workflow;
			}
			set
			{
				this.workflow = value;
			}
		}

		// Token: 0x0600007D RID: 125 RVA: 0x000052DC File Offset: 0x000034DC
		public JobManager(Workflow workflow, bool execute = true)
		{
			this.workflow = workflow;
			this.interval = 900000;
			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: 0x0600007E RID: 126 RVA: 0x00005364 File Offset: 0x00003564
		public override void Execute()
		{
			bool flag = !this.tasksIsAlive();
			if (flag)
			{
				this.workflow.InputConfigurations.ForEach(delegate(Origin source)
				{
					this.TInput.Execute(source);
					while (this.TInput.Running)
					{
						System.Threading.Thread.Sleep(2000);
					}
				});
				List<File> filesExtraction = this.workflow.Queues.Extraction;
				bool flag2 = filesExtraction.Count > 0;
				if (flag2)
				{
					filesExtraction.ForEach(delegate(File file)
					{
						this.TExtractor.Execute(file, this.workflow.Blueprints);
					});
					filesExtraction.Clear();
				}
				List<File> filesIntegration = new List<File>();
				using (List<File>.Enumerator enumerator = this.workflow.Queues.Integration.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						File file = enumerator.Current;
						bool flag3 = string.IsNullOrEmpty(file.LawsuitNumber);
						if (flag3)
						{
							filesIntegration.Add(file);
						}
						else
						{
							File fi = (from f in filesIntegration
							where f.LawsuitNumber == file.LawsuitNumber
							select f).FirstOrDefault<File>();
							bool flag4 = fi != null;
							if (flag4)
							{
								Action<Field> <>9__5;
								fi.METAObject.Pages.ForEach(delegate(Page page)
								{
									List<Field> list = page.SearchElements.ToList<Field>();
									Action<Field> action;
									if ((action = <>9__5) == null)
									{
										action = (<>9__5 = delegate(Field element)
										{
											bool flag6 = element.Name == "ConteudoCompleto";
											if (flag6)
											{
												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 flag5 = filesIntegration.Count > 0;
				if (flag5)
				{
					this.workflow.OutputConfigurations.ForEach(delegate(Origin source)
					{
						filesIntegration.ForEach(delegate(File file)
						{
							this.TOutput.Execute(source, file);
							while (this.TOutput.Running)
							{
								System.Threading.Thread.Sleep(2000);
							}
						});
					});
				}
			}
		}

		// Token: 0x0600007F RID: 127 RVA: 0x00005588 File Offset: 0x00003788
		private bool tasksIsAlive()
		{
			return this.TInput.Running || this.TExtractor.Running || this.TOutput.Running || this.TSynchronizer.Running;
		}

		// Token: 0x04000028 RID: 40
		private Workflow workflow;

		// Token: 0x04000029 RID: 41
		private Input TInput;

		// Token: 0x0400002A RID: 42
		private Extractor TExtractor;

		// Token: 0x0400002B RID: 43
		private Output TOutput;

		// Token: 0x0400002C RID: 44
		private Synchronizer TSynchronizer;
	}
}
