using ICSharpCode.SharpZipLib.GZip;
using ICSharpCode.SharpZipLib.Tar;
using System;
using System.IO;
using System.Text;
using DataClients;
using System.Threading.Tasks;
using System.Threading;
using System.Linq;
using Mailing;
using System.Text.RegularExpressions;
using System.Collections;
using System.Collections.Generic;

public class CustomMath
{
	public static int multiplu(int a, int b)
	{
		return a * b;
	}
}

namespace Tests
{
	class Program
	{
		static void Main(string[] args)
		{
			test14();
			Console.ReadKey();
		}
		static void test1()
		{
			Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
			var enc = Encoding.GetEncoding(866);
			var filec = new FileClient();
			var test = filec.GetFile(DateTime.Now.AddDays(-46), 34, 0);
			//var netc = new NetClient();
			//var test = netc.SocketWork(NetClient.Cmd.download_nh, "20200707.230");
			//var netc = new NetClient();
			//var test = netc.SocketWork(NetClient.Cmd.user_flags);
			Console.WriteLine(BitConverter.ToString(test).Replace("-", " "));
			Console.WriteLine(enc.GetString(test));
			Console.ReadKey();
		}
		static void test2()
		{
			var a = new STPClient();
			var b = a.GetTechCycle(
				new DateTime(2020, 07, 1),
				new DateTime(2020, 07, 10),
				16);
			foreach (var e in b)
				Console.WriteLine(e.start.ToString(@"yyyy.MM.dd HH:mm:ss") + '\t' + Names.techCycle[(ushort)e.index]);

		}
		static void test3()
		{
			var a = new STPClient();
			var b = a.GetAnalogDiscret(
				new DateTime(2020, 07, 14, 0, 0, 0),
				new DateTime(2020, 07, 14, 23, 59, 59),
				37);
			foreach (var c in b.di)
			{
				Console.WriteLine(Names.discretFull[c.Key]);
				foreach (var d in c.Value)
					Console.WriteLine(d.Item1.ToString(@"yyyy.MM.dd HH:mm:ss.ff") + '\t' + d.Item2.ToString());
			}
			foreach (var c in b.an)
			{
				Console.WriteLine(Names.discretFull[c.Key]);
				foreach (var d in c.Value)
					Console.WriteLine(d.Item1.ToString(@"yyyy.MM.dd HH:mm:ss.ff") + '\t' + d.Item2.ToString());
			}

			Console.ReadKey();

		}
		static void test4()
		{
			var a = new STPClient();
			var ts = new DateTime(2020, 07, 13, 0, 0, 0);
			var te = new DateTime(2020, 07, 13, 23, 59, 59);
			var totTime = new TimeSpan();
			for (ushort i = 1; i <= 50; i++)
			{
				var tc = a.GetTechCycle(ts, te, i);
				var ad = a.GetAnalogDiscret(ts, te, i);

				Console.WriteLine("VDP " + i.ToString("D2"));

				var totalVdpTime = new TimeSpan();
				foreach (var e in tc)
				{
					int[] chk1 = {
						(int)TechCycle.Operation.cooling_ingot,
						(int)TechCycle.Operation.cooling_reflow,
						(int)TechCycle.Operation.cooling_welding };
					int[] chk2 =
					{
						(int)TechCycle.Operation.end_tech_cycle,
						(int)TechCycle.Operation.unloading_loading,
						(int)TechCycle.Operation.looking_welding,
						(int)TechCycle.Operation.unloading_kit
					};
					if (!chk1.Contains((int)e.index) && !chk2.Contains((int)e.index))
						continue;
					if (chk1.Contains((int)e.index))
					{
						var test =
									(from l in ad.an[13]
									 where l.Item1 >= e.start &&
										 l.Item1 <= e.end &&
										 l.Item2 >= 30
									 select l).ToArray();
						if (test.Length == 0)
							continue;
					}

					Tuple<DateTime, bool?> currstat = new Tuple<DateTime, bool?>(e.start, null);
					foreach (var d in ad.di[22])
					{
						if (
							currstat.Item1 <= e.end &&
							d.Item1 >= e.start &&
							currstat.Item2.HasValue &&
							currstat.Item2.Value
							)
						{
							var t1 = currstat.Item1 > e.start ? currstat.Item1 : e.start;
							var t2 = d.Item1 > e.end ? e.end : d.Item1;
							var res = t2 - t1;
							Console.Write(e.start.ToString(@"yyyy.MM.dd HH:mm:ss.ff") + '\t');
							Console.Write(Names.techCycle[(int)e.index] + '\t');
							Console.WriteLine(res.ToString(@"hh\:mm\:ss"));
							totalVdpTime = totalVdpTime.Add(res);
						}
						currstat = d;
					}
				}
				Console.WriteLine("Total VDP Time: " + totalVdpTime.ToString(@"hh\:mm\:ss"));
				totTime = totTime.Add(totalVdpTime);
			}
			Console.WriteLine("Total Time: " + totTime.ToString(@"hh\:mm\:ss"));
			Console.ReadKey();
		}
		static void test5()
		{
			var a = new STPClient();
			var i = DateTime.Now.AddDays(-1 * new Random().Next(10));
			Console.WriteLine(i.ToString());

			var b = a.GetListPasport(i);
			foreach (var e in b)
				Console.WriteLine(e.Item1 + '\t' + e.Item2);
			Console.WriteLine();

			var r = new Random().Next(b.Length);
			Console.WriteLine(b[r].Item1 + '\t' + b[r].Item2);

			var p = a.GetPasport(b[r].Item2);
			Console.WriteLine(p.ToString());

		}
		static void test6()
		{
			var a = new STPClient();
			var s = DateTime.Now.AddDays(-30);
			var e = DateTime.Now.AddDays(-20);
			var b = a.GetIshData(s, e, 37);
			Console.WriteLine(s.ToString(@"yyyy.MM.dd HH:mm:ss.ff") + '\t' + e.ToString(@"yyyy.MM.dd HH:mm:ss.ff"));
			foreach (var t in b)
			{
				Console.WriteLine(t.time.ToString(@"yyyy.MM.dd HH:mm:ss.ff") + '\t' + t.id.ToString("D2") + '\t' + t.value);
			}
		}
		static void test7()
		{
			var w = new STPClient();
			var s = new DateTime(2020, 06, 1);
			var e = new DateTime(2020, 07, 22);
			var r1 = new List<(string name, string dir)>();
			var r2 = new List<Pasport>();
			for (var i = s; i < e; i = i.AddDays(1))
			{
				var a = w.GetListPasport(i);
				foreach (var b in a)
				{
					var c = w.GetPasport(b.Item2);
					var d = w.GetIshData(c.time_start, c.time_end, c.numVDP);
					var flag = false;
					foreach (var f in d)
					{
						if (f.id != 0) continue;
						//Console.Write(f.time.ToString(@"yyyy.MM.dd HH:mm:ss.ff") + '\t' + b.Item1 + '\t' + f.value);
						Regex r = new Regex(@"(\w*)-(\w{3})-32-031(\w*)", RegexOptions.Compiled | RegexOptions.IgnoreCase);
						//Console.WriteLine('\t' + r.IsMatch(f.value).ToString());
						flag = flag || r.IsMatch(f.value);
					}
					if (flag)
					{
						r1.Add(b); r2.Add(c);
						Console.WriteLine(b.Item1 + '\t' +
							c.time_start.ToString(@"yyyy.MM.dd HH:mm:ss.ff") + '\t' +
							c.time_end.ToString(@"yyyy.MM.dd HH:mm:ss.ff"));
						foreach (var f in d)
						{
							if (f.id != 0) continue;
							Console.WriteLine(f.time.ToString(@"yyyy.MM.dd HH:mm:ss.ff") + '\t' + f.value);
						}
					}
				}
			}

			Console.WriteLine();
			var fs = new StreamWriter(Directory.GetCurrentDirectory() + '\\' + "result.txt");
			for (var i = 0; i < r1.Count; i++)
			{
				var a = w.GetProtectData(r2[i].time_start, r2[i].time_end, r2[i].numVDP);
				var b = new List<Protect>();
				foreach (var c in a)
					if (c.id == 72) b.Add(c);
				if (b.Count > 0)
				{
					Console.WriteLine(r1[i].Item1 + '\t' +
						r2[i].time_start.ToString(@"yyyy.MM.dd HH:mm:ss.ff") + '\t' +
						r2[i].time_end.ToString(@"yyyy.MM.dd HH:mm:ss.ff"));
					fs.WriteLine(r1[i].Item1 + '\t' +
						r2[i].time_start.ToString(@"yyyy.MM.dd HH:mm:ss.ff") + '\t' +
						r2[i].time_end.ToString(@"yyyy.MM.dd HH:mm:ss.ff"));
					foreach (var d in b)
					{
						Console.WriteLine(d.time.ToString(@"yyyy.MM.dd HH:mm:ss.ff") + '\t' + d.id.ToString("D2") + '\t' + d.value);
						fs.WriteLine(d.time.ToString(@"yyyy.MM.dd HH:mm:ss.ff") + '\t' + d.value);
					}
				}
			}
			fs.Close();
		}
		static void test8()
		{
			var w = new STPClient();
			var s = new DateTime(2020, 09, 30);
			var e = new DateTime(2020, 10, 03);
			//var vdps = new int[] { 2, 3, 4, 10, 11, 12, 16, 21, 22, 23, 27, 31, 33, 36, 45 };
			var vdps = new int[] { 3, 4, 21, 36 };
			foreach (var vdp in vdps)
			{
				var a = w.GetProtectData(s, e, (ushort)vdp);
				a = a.FindAll(x => x.id == 35 || x.id == 70 || x.id == 71);
				Console.WriteLine("VDP " + vdp.ToString("D2"));
				foreach (var b in a)
				{
					Console.Write(b.id.ToString("D2") + " ");
					Console.Write(b.time.ToString(@"yyyy.MM.dd HH:mm:ss.ff"));
					Console.WriteLine(" " + b.value.ToString("D2"));
				}

				var fs = new StreamWriter(Directory.GetCurrentDirectory() + '\\' + vdp.ToString("D2") + ".txt");
				fs.WriteLine("35 - Отсутствует связь с контроллером ГМП");
				fs.WriteLine("70 - Смещение начала архивации");
				fs.WriteLine("70 - Смещение конца архивации");
				fs.WriteLine();
				fs.WriteLine("№ защиты \t Дата возникновения записи \t Состояние");

				foreach (var b in a)
					fs.WriteLine(b.id.ToString("D2") + "\t" + b.time.ToString(@"yyyy.MM.dd HH:mm:ss.ff") + "\t" + b.value.ToString("D2"));
				fs.Close();
			}
		}
		static void test9()
		{
			var w = new STPClient();
			var s = new DateTime(2020, 10, 05);
			var e = new DateTime(2020, 10, 07);
			var vdp = 1;

			var a = w.GetProtectData(s, e, (ushort)vdp);
			/*var res = new List<int>();
			foreach (var b in a)
				if (!res.Contains(b.id))
					res.Add(b.id);
			res.Sort((x, y) => x.CompareTo(y));*/
			foreach (var b in a)
				Console.WriteLine(b.id.ToString("D3") + "  " + b.time.ToString(@"yyyy.MM.dd HH:mm:ss.ff") + "  " + b.value);
		}

		static void test10()
		{
			var a = new List<List<SZOWork>>();
			a.Add(new List<SZOWork>());

			var tc = new TechCycle()
			{
				index = TechCycle.Operation.unloading_loading,
				start = DateTime.Now.AddHours(-2),
				end = DateTime.Now
			};

			var b = new SZOWork()
			{
				tc = tc,
				tStart = DateTime.Now.AddHours(-2),
				tEnd = DateTime.Now.AddHours(-1.3)
			};
			a[0].Add(b);

			b = new SZOWork()
			{
				tc = tc,
				tStart = DateTime.Now.AddHours(-1.3),
				tEnd = DateTime.Now.AddHours(-0.7)
			};
			a[0].Add(b);

			b = new SZOWork()
			{
				tc = tc,
				tStart = DateTime.Now.AddHours(-0.7),
				tEnd = DateTime.Now.AddHours(-0.2)
			};
			a[0].Add(b);

			CreatePDF.PDFSZO(a, DateTime.Now);
		}

		static void test11()
		{
			var tStart = new DateTime(2020, 11, 17, 0, 0, 0);
			var tEnd = new DateTime(2020, 11, 17, 23, 59, 59, 999);
			var r = new List<List<SZOWork>>();
			for (var i = 1; i <= 50; i++)
				r.Add(CollectData.GetSZOWorks(tStart, tEnd, (ushort)i));
			CreatePDF.PDFSZO(r, tStart);
		}

		static void test12()
		{
			var a = new STPClient();
			var b = a.GetAnalogDiscret(DateTime.Now, DateTime.Now, 50);
			foreach (var e in b.an)
			{
				if (e.Key == 13)
					continue;
				using (StreamWriter file = new StreamWriter(Directory.GetCurrentDirectory() + "/test50.txt", true))
				{
					file.WriteLine(e.Key.ToString());
					foreach (var f in e.Value)
					{
						var tmp = f.Item2.HasValue ? f.Item2.Value.ToString() : "NULL";
						file.WriteLine(f.Item1.ToString("yyyy-MM-dd HH:mm:ss.f") + "\t" + tmp);
					}
				}
			}
		}
		static void test13()
		{
			var ts = new DateTime(2021, 03, 03);
			var te = new DateTime(2021, 03, 04);
			var a = new STPClient();
			var b = a.GetProtectData(ts, te, 32);
			foreach (var e in b)
				if (e.id == 68)
					Console.WriteLine(e.time.ToString("yyyy-MM-dd HH:mm:ss.f") + "\t" + e.id.ToString("D3") + "\t" + e.value.ToString("D2"));

		}

		static void test14()
		{
			var total = new List<(ushort, Protect)>();
			var ts = new DateTime(2021, 05, 28);
			var te = new DateTime(2021, 06, 02);
			var a = new STPClient();
			var vdp = new ushort[] { 3, 4, 5, 6, 8, 10, 17, 18, 33, 39, 40, 43, 44 };
			foreach(var v in vdp)
			{
				Console.WriteLine("Get: vdp=" + v.ToString("D2"));
				var b = a.GetProtectData(ts, te, v);
				Console.WriteLine("Write: vdp=" + v.ToString("D2"));
				foreach (var e in b)
				{
					if (e.id != 35 && e.id != 105)
						continue;
					total.Add((v, e));
					var name = e.id == 35 ? "Отс. связь ГМП" : "Отс. связь AB ";
					using (StreamWriter file = new StreamWriter(Directory.GetCurrentDirectory() + "/" + v.ToString("D2") +".txt", true))
					{
						file.WriteLine(
							e.time.ToString("yyyy-MM-dd HH:mm:ss.f") + "\t" +
							e.id.ToString("D3") + "\t" +
							name + "\t" +
							e.value.ToString("D2"));
					}
				}
				Console.WriteLine("End: vdp=" + v.ToString("D2"));
			}
			using (StreamWriter file = new StreamWriter(Directory.GetCurrentDirectory() + "/total.txt", true))
			{
				total.Sort(delegate((ushort, Protect)x, (ushort, Protect) y) {
					var r = DateTime.Compare(x.Item2.time, y.Item2.time);
					if (r < 0) return -1;
					else if (r > 0) return 1;
					else return 0;
				});
				for (var i = 0; i < total.Count; i++)
				{
					var e = total[i];
					var name = e.Item2.id == 35 ? "Отс. связь ГМП" : "Отс. связь AB ";
					file.WriteLine(
						e.Item2.time.ToString("yyyy-MM-dd HH:mm:ss.f") + "\t" +
						e.Item2.id.ToString("D3") + "\t" +
						name + "\t" +
						e.Item2.value.ToString("D2") + "\t" + 
						"<- vdp" + e.Item1.ToString("D2"));
				}
			}
			Console.WriteLine("End");
		}
	}
}
