using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Xml;
using ZionRobot.Helpers;

namespace ZionRobot.Models.Source
{
	// Token: 0x02000029 RID: 41
	public class Action
	{
		// Token: 0x17000061 RID: 97
		// (get) Token: 0x06000104 RID: 260 RVA: 0x000070EE File Offset: 0x000052EE
		// (set) Token: 0x06000105 RID: 261 RVA: 0x000070F6 File Offset: 0x000052F6
		public string Name { get; set; }

		// Token: 0x17000062 RID: 98
		// (get) Token: 0x06000106 RID: 262 RVA: 0x000070FF File Offset: 0x000052FF
		// (set) Token: 0x06000107 RID: 263 RVA: 0x00007107 File Offset: 0x00005307
		public string Type { get; set; }

		// Token: 0x17000063 RID: 99
		// (get) Token: 0x06000108 RID: 264 RVA: 0x00007110 File Offset: 0x00005310
		// (set) Token: 0x06000109 RID: 265 RVA: 0x00007118 File Offset: 0x00005318
		public string Check { get; set; }

		// Token: 0x17000064 RID: 100
		// (get) Token: 0x0600010A RID: 266 RVA: 0x00007121 File Offset: 0x00005321
		// (set) Token: 0x0600010B RID: 267 RVA: 0x00007129 File Offset: 0x00005329
		public string Regex { get; set; }

		// Token: 0x17000065 RID: 101
		// (get) Token: 0x0600010C RID: 268 RVA: 0x00007132 File Offset: 0x00005332
		// (set) Token: 0x0600010D RID: 269 RVA: 0x0000713A File Offset: 0x0000533A
		public string Replace { get; set; }

		// Token: 0x17000066 RID: 102
		// (get) Token: 0x0600010E RID: 270 RVA: 0x00007143 File Offset: 0x00005343
		// (set) Token: 0x0600010F RID: 271 RVA: 0x0000714B File Offset: 0x0000534B
		public string Attribute { get; set; }

		// Token: 0x17000067 RID: 103
		// (get) Token: 0x06000110 RID: 272 RVA: 0x00007154 File Offset: 0x00005354
		// (set) Token: 0x06000111 RID: 273 RVA: 0x0000715C File Offset: 0x0000535C
		public int MaxLength { get; set; }

		// Token: 0x17000068 RID: 104
		// (get) Token: 0x06000112 RID: 274 RVA: 0x00007165 File Offset: 0x00005365
		// (set) Token: 0x06000113 RID: 275 RVA: 0x0000716D File Offset: 0x0000536D
		public int Index { get; set; }

		// Token: 0x17000069 RID: 105
		// (get) Token: 0x06000114 RID: 276 RVA: 0x00007176 File Offset: 0x00005376
		// (set) Token: 0x06000115 RID: 277 RVA: 0x0000717E File Offset: 0x0000537E
		public bool Wait { get; set; }

		// Token: 0x1700006A RID: 106
		// (get) Token: 0x06000116 RID: 278 RVA: 0x00007187 File Offset: 0x00005387
		// (set) Token: 0x06000117 RID: 279 RVA: 0x0000718F File Offset: 0x0000538F
		public bool Initialize { get; set; }

		// Token: 0x1700006B RID: 107
		// (get) Token: 0x06000118 RID: 280 RVA: 0x00007198 File Offset: 0x00005398
		// (set) Token: 0x06000119 RID: 281 RVA: 0x000071A0 File Offset: 0x000053A0
		public List<Action> InnerActions { get; set; }

		// Token: 0x1700006C RID: 108
		// (get) Token: 0x0600011A RID: 282 RVA: 0x000071A9 File Offset: 0x000053A9
		// (set) Token: 0x0600011B RID: 283 RVA: 0x000071B1 File Offset: 0x000053B1
		public List<Action> ResultTrue { get; set; }

		// Token: 0x1700006D RID: 109
		// (get) Token: 0x0600011C RID: 284 RVA: 0x000071BA File Offset: 0x000053BA
		// (set) Token: 0x0600011D RID: 285 RVA: 0x000071C2 File Offset: 0x000053C2
		public List<Action> ResultFalse { get; set; }

		// Token: 0x1700006E RID: 110
		// (get) Token: 0x0600011E RID: 286 RVA: 0x000071CB File Offset: 0x000053CB
		// (set) Token: 0x0600011F RID: 287 RVA: 0x000071D3 File Offset: 0x000053D3
		public string Where
		{
			get
			{
				return this.where;
			}
			set
			{
				this.where = value;
			}
		}

		// Token: 0x1700006F RID: 111
		// (get) Token: 0x06000120 RID: 288 RVA: 0x000071DC File Offset: 0x000053DC
		// (set) Token: 0x06000121 RID: 289 RVA: 0x000071E4 File Offset: 0x000053E4
		public string Value
		{
			get
			{
				return this.value;
			}
			set
			{
				this.value = value;
			}
		}

		// Token: 0x06000122 RID: 290 RVA: 0x000071ED File Offset: 0x000053ED
		public Action()
		{
			this.Initialize = true;
		}

		// Token: 0x06000123 RID: 291 RVA: 0x00007200 File Offset: 0x00005400
		public Action(XmlNode node)
		{
			try
			{
				XmlAttribute xmlAttribute = node.Attributes["Name"];
				this.Name = (((xmlAttribute != null) ? xmlAttribute.Value : null) ?? null);
				XmlAttribute xmlAttribute2 = node.Attributes["Type"];
				this.Type = (((xmlAttribute2 != null) ? xmlAttribute2.Value : null) ?? null);
				XmlAttribute xmlAttribute3 = node.Attributes["Where"];
				this.Where = (((xmlAttribute3 != null) ? xmlAttribute3.Value : null) ?? null);
				XmlAttribute xmlAttribute4 = node.Attributes["Check"];
				this.Check = (((xmlAttribute4 != null) ? xmlAttribute4.Value : null) ?? null);
				XmlAttribute xmlAttribute5 = node.Attributes["Regex"];
				this.Regex = (((xmlAttribute5 != null) ? xmlAttribute5.Value : null) ?? null);
				XmlAttribute xmlAttribute6 = node.Attributes["Replace"];
				this.Replace = (((xmlAttribute6 != null) ? xmlAttribute6.Value : null) ?? null);
				XmlAttribute xmlAttribute7 = node.Attributes["Attribute"];
				this.Attribute = (((xmlAttribute7 != null) ? xmlAttribute7.Value : null) ?? null);
				int maxLength;
				if (node.Attributes["MaxLength"] == null)
				{
					maxLength = 0;
				}
				else
				{
					XmlAttribute xmlAttribute8 = node.Attributes["MaxLength"];
					maxLength = int.Parse((xmlAttribute8 != null) ? xmlAttribute8.Value : null);
				}
				this.MaxLength = maxLength;
				XmlAttribute xmlAttribute9 = node.Attributes["Value"];
				this.Value = (((xmlAttribute9 != null) ? xmlAttribute9.Value.Replace("&amp;", "&") : null) ?? null);
				this.Index = ((node.Attributes["Index"] != null) ? int.Parse(node.Attributes["Index"].Value) : -1);
				this.Wait = (node.Attributes["Wait"] == null || bool.Parse(node.Attributes["Wait"].Value));
				this.InnerActions = new List<Action>();
				foreach (object obj in node.SelectNodes("Action"))
				{
					XmlNode i = (XmlNode)obj;
					this.InnerActions.Add(new Action(i));
				}
				this.ResultTrue = new List<Action>();
				foreach (object obj2 in node.SelectNodes("ResultTrue/Action"))
				{
					XmlNode action = (XmlNode)obj2;
					this.ResultTrue.Add(new Action(action));
				}
				this.ResultFalse = new List<Action>();
				foreach (object obj3 in node.SelectNodes("ResultFalse/Action"))
				{
					XmlNode action2 = (XmlNode)obj3;
					this.ResultFalse.Add(new Action(action2));
				}
			}
			catch (Exception ex)
			{
				throw new Exception(ex.Message);
			}
		}

		// Token: 0x06000124 RID: 292 RVA: 0x000075A4 File Offset: 0x000057A4
		public XmlElement AppendTo(XmlDocument doc)
		{
			XmlElement root = doc.CreateElement("Action");
			bool flag = this.Name != null;
			if (flag)
			{
				root.SetAttribute("Name", this.Name);
			}
			bool flag2 = this.Type != null;
			if (flag2)
			{
				root.SetAttribute("Type", this.Type);
			}
			bool flag3 = this.Where != null;
			if (flag3)
			{
				root.SetAttribute("Where", this.Where);
			}
			bool flag4 = this.Check != null;
			if (flag4)
			{
				root.SetAttribute("Check", this.Check);
			}
			bool flag5 = this.Regex != null;
			if (flag5)
			{
				root.SetAttribute("Regex", this.Regex);
			}
			bool flag6 = this.Replace != null;
			if (flag6)
			{
				root.SetAttribute("Replace", this.Replace);
			}
			bool flag7 = this.Attribute != null;
			if (flag7)
			{
				root.SetAttribute("Attribute", this.Attribute);
			}
			bool flag8 = this.Value != null;
			if (flag8)
			{
				root.SetAttribute("Value", this.Value);
			}
			bool flag9 = this.MaxLength > 0;
			if (flag9)
			{
				root.SetAttribute("MaxLength", this.MaxLength.ToString());
			}
			bool flag10 = !this.Wait;
			if (flag10)
			{
				root.SetAttribute("Wait", this.Wait.ToString());
			}
			bool flag11 = this.Index >= 0;
			if (flag11)
			{
				root.SetAttribute("Index", this.Index.ToString());
			}
			bool flag12 = this.InnerActions.Count > 0;
			if (flag12)
			{
				XmlElement eLoop = doc.CreateElement("Loop");
				this.InnerActions.ForEach(delegate(Action innerAction)
				{
					eLoop.AppendChild(innerAction.AppendTo(doc));
				});
				root.AppendChild(eLoop);
			}
			bool flag13 = this.ResultTrue.Count > 0;
			if (flag13)
			{
				XmlElement resultTrue = doc.CreateElement("ResultTrue");
				this.InnerActions.ForEach(delegate(Action innerAction)
				{
					resultTrue.AppendChild(innerAction.AppendTo(doc));
				});
				root.AppendChild(resultTrue);
			}
			bool flag14 = this.ResultFalse.Count > 0;
			if (flag14)
			{
				XmlElement resultFalse = doc.CreateElement("ResultFalse");
				this.InnerActions.ForEach(delegate(Action innerAction)
				{
					resultFalse.AppendChild(innerAction.AppendTo(doc));
				});
				root.AppendChild(resultFalse);
			}
			return root;
		}

		// Token: 0x06000125 RID: 293 RVA: 0x00007898 File Offset: 0x00005A98
		public string ReplaceWithDictionary(string value, Dictionary<string, string> dictionaryValues)
		{
			string result = value;
			List<string> parts = (from Match m in System.Text.RegularExpressions.Regex.Matches(value, "\\{[^\\}]*\\}")
			select m.Value).ToList<string>();
			parts.ForEach(delegate(string part)
			{
				string temp = part.Trim(new char[]
				{
					'{'
				}).Trim(new char[]
				{
					'}'
				});
				string add = string.Empty;
				bool flag = dictionaryValues.ContainsKey(temp);
				if (flag)
				{
					bool flag2 = this.Where == "input[id=data_da_disponibilizStr]" && dictionaryValues["SearchElements.Fields.Tipo"] == "Publicação";
					if (flag2)
					{
						add += this.getBusinessDay(DateTime.Parse(dictionaryValues[temp]), -1).ToString("dd/MM/yyyy");
					}
					else
					{
						bool flag3 = this.Where == "input[id=data_de_publicacaoStr]" && (dictionaryValues["SearchElements.Fields.Tipo"] == "Disponibilização" || dictionaryValues["SearchElements.Fields.Tipo"] == "Divulgação");
						if (flag3)
						{
							add += this.getBusinessDay(DateTime.Parse(dictionaryValues[temp]), 1).ToString("dd/MM/yyyy");
						}
						else
						{
							add += dictionaryValues[temp];
						}
					}
					bool flag4 = this.Replace != null;
					if (flag4)
					{
						this.Replace.Split(new char[]
						{
							';'
						}).ToList<string>().ForEach(delegate(string x)
						{
							string[] p = x.Split(new char[]
							{
								','
							});
							add = System.Text.RegularExpressions.Regex.Replace(add, p[0], p[1]);
						});
					}
				}
				result = result.Replace(part, add.Replace("&gt;", ">"));
			});
			result = ((result != string.Empty) ? result : value);
			return (this.MaxLength > 0) ? result.Substring(0, Math.Min(result.Length, this.MaxLength)) : result;
		}

		// Token: 0x06000126 RID: 294 RVA: 0x00007968 File Offset: 0x00005B68
		public string ReplaceValue(string value, Dictionary<string, string> dictionaryValues)
		{
			string result = string.Empty;
			List<string> parts = (from Match m in System.Text.RegularExpressions.Regex.Matches(value, "\\{[^\\}]*\\}|([:=\\[\\(\\{]{0}[a-zA-Z0-9_%@\\-\\.\\/:?=&;]{1,})")
			select m.Value).ToList<string>();
			parts.ForEach(delegate(string part)
			{
				string temp = part.Trim(new char[]
				{
					'{'
				}).Trim(new char[]
				{
					'}'
				});
				string add = string.Empty;
				bool flag = System.Text.RegularExpressions.Regex.IsMatch(temp, "\\$_Now");
				if (flag)
				{
					DateTime dateNow = DateTime.Now;
					int day = 0;
					string[] p = temp.Split(new char[]
					{
						'?'
					});
					bool flag2 = p.Count<string>() == 1;
					if (flag2)
					{
						bool flag3 = System.Text.RegularExpressions.Regex.IsMatch(p[0], "\\.");
						if (flag3)
						{
							day = int.Parse(temp.Split(new char[]
							{
								'.'
							}).Last<string>());
						}
					}
					else
					{
						string @operator = (from Match match in System.Text.RegularExpressions.Regex.Matches(p[0], "([!=<>]{1,2}[<>=]?)")
						select match.Value).First<string>();
						string[] s = p[0].Split(new string[]
						{
							@operator
						}, StringSplitOptions.RemoveEmptyEntries);
						int hours = int.Parse(s[1]);
						string[] days = p[1].Split(new char[]
						{
							':'
						});
						bool flag4 = (@operator == "==" && dateNow.Hour == hours) || (@operator == "!=" && dateNow.Hour != hours) || (@operator == "<=" && dateNow.Hour <= hours) || (@operator == ">=" && dateNow.Hour >= hours) || (@operator == ">" && dateNow.Hour > hours) || (@operator == "<" && dateNow.Hour < hours);
						if (flag4)
						{
							day = int.Parse(days[0]);
						}
						else
						{
							day = int.Parse(days[1]);
						}
					}
					add = ((day == 0) ? dateNow.ToString("dd/MM/yyyy") : this.getBusinessDay(DateTime.Now, day).ToString("dd/MM/yyyy"));
				}
				bool flag5 = dictionaryValues.ContainsKey(temp);
				if (flag5)
				{
					bool flag6 = this.Where == "input[id=data_da_disponibilizStr]" && dictionaryValues["SearchElements.Fields.Tipo"] == "Publicação";
					if (flag6)
					{
						add += this.getBusinessDay(DateTime.Parse(dictionaryValues[temp]), -1).ToString("dd/MM/yyyy");
					}
					else
					{
						bool flag7 = this.Where == "input[id=data_de_publicacaoStr]" && (dictionaryValues["SearchElements.Fields.Tipo"] == "Disponibilização" || dictionaryValues["SearchElements.Fields.Tipo"] == "Divulgação");
						if (flag7)
						{
							add += this.getBusinessDay(DateTime.Parse(dictionaryValues[temp]), 1).ToString("dd/MM/yyyy");
						}
						else
						{
							add += dictionaryValues[temp];
						}
					}
				}
				bool flag8 = add != string.Empty;
				if (flag8)
				{
					result += add;
				}
				else
				{
					result += System.Text.RegularExpressions.Regex.Replace(part, "amp;", string.Empty);
				}
			});
			result = ((result != string.Empty) ? result : value);
			return (this.MaxLength > 0) ? result.Substring(0, Math.Min(result.Length, this.MaxLength)) : result;
		}

		// Token: 0x06000127 RID: 295 RVA: 0x00007A3C File Offset: 0x00005C3C
		private DateTime getBusinessDay(DateTime dateTime, int day)
		{
			DateTime currentDay = dateTime;
			bool flag;
			do
			{
				currentDay = currentDay.AddDays((double)day);
				flag = Shift.isDuringTheWeek(currentDay);
			}
			while (!flag);
			return currentDay;
		}

		// Token: 0x0400005B RID: 91
		private string where;

		// Token: 0x0400005C RID: 92
		private string value;
	}
}
