using System;
using System.Collections.Generic;
using System.Text;

namespace DataClients
{
	public class IshData
	{
		public DateTime time;
		public ushort id;
		public string value;
	}

	public class TechCycle
	{
		public enum Operation : ushort
		{
			end_tech_cycle = 0,
			unloading_loading = 1,
			vacuum_welding = 2,
			welding = 5,
			cooling_welding = 6,
			looking_welding = 7,
			vacuum_melting = 8,
			breeding_bathtub = 9,
			melting = 10,
			VUR = 11,
			cooling_ingot = 12,
			unloading_kit = 13,
			vacuum_reflow = 14,
			reflow = 15,
			cooling_reflow = 16,
			overflow_metal = 17,
			check_protection = 25
		}
		public DateTime start;
		public DateTime end;
		public Operation index;
	}
	public class ByteMatrix
	{
		public DateTime time;
		public byte?[] matrix;
	}
	public static class Converter
	{
		public static char ConvertByteToChar(byte b)
		{
			char temp;
			switch (b)
			{
				case 0x00: temp = ' '; break;
				case 0x09: temp = '\t'; break;
				case 0x0a: temp = '\n'; break;
				case 0x80: temp = 'А'; break;
				case 0x81: temp = 'Б'; break;
				case 0x82: temp = 'В'; break;
				case 0x83: temp = 'Г'; break;
				case 0x84: temp = 'Д'; break;
				case 0x85: temp = 'Е'; break;
				case 0x86: temp = 'Ж'; break;
				case 0x87: temp = 'З'; break;
				case 0x88: temp = 'И'; break;
				case 0x89: temp = 'Й'; break;
				case 0x8a: temp = 'К'; break;
				case 0x8b: temp = 'Л'; break;
				case 0x8c: temp = 'М'; break;
				case 0x8d: temp = 'Н'; break;
				case 0x8e: temp = 'О'; break;
				case 0x8f: temp = 'П'; break;
				case 0x90: temp = 'Р'; break;
				case 0x91: temp = 'С'; break;
				case 0x92: temp = 'Т'; break;
				case 0x93: temp = 'У'; break;
				case 0x94: temp = 'Ф'; break;
				case 0x95: temp = 'Х'; break;
				case 0x96: temp = 'Ц'; break;
				case 0x97: temp = 'Ч'; break;
				case 0x98: temp = 'Ш'; break;
				case 0x99: temp = 'Щ'; break;
				case 0x9a: temp = 'Ъ'; break;
				case 0x9b: temp = 'Ы'; break;
				case 0x9c: temp = 'Ь'; break;
				case 0x9d: temp = 'Э'; break;
				case 0x9e: temp = 'Ю'; break;
				case 0x9f: temp = 'Я'; break;
				case 0xa0: temp = 'а'; break;
				case 0xa1: temp = 'б'; break;
				case 0xa2: temp = 'в'; break;
				case 0xa3: temp = 'г'; break;
				case 0xa4: temp = 'д'; break;
				case 0xa5: temp = 'е'; break;
				case 0xa6: temp = 'ж'; break;
				case 0xa7: temp = 'з'; break;
				case 0xa8: temp = 'и'; break;
				case 0xa9: temp = 'й'; break;
				case 0xaa: temp = 'к'; break;
				case 0xab: temp = 'л'; break;
				case 0xac: temp = 'м'; break;
				case 0xad: temp = 'н'; break;
				case 0xae: temp = 'о'; break;
				case 0xaf: temp = 'п'; break;
				case 0xe0: temp = 'р'; break;
				case 0xe1: temp = 'c'; break;
				case 0xe2: temp = 'т'; break;
				case 0xe3: temp = 'у'; break;
				case 0xe4: temp = 'ф'; break;
				case 0xe5: temp = 'х'; break;
				case 0xe6: temp = 'ц'; break;
				case 0xe7: temp = 'ч'; break;
				case 0xe8: temp = 'ш'; break;
				case 0xe9: temp = 'щ'; break;
				case 0xea: temp = 'ъ'; break;
				case 0xeb: temp = 'ы'; break;
				case 0xec: temp = 'ь'; break;
				case 0xed: temp = 'э'; break;
				case 0xee: temp = 'ю'; break;
				case 0xef: temp = 'я'; break;
				case 0xf0: temp = 'Ё'; break;
				case 0xf1: temp = 'ё'; break;
				default: temp = Convert.ToChar(b); break;
			}
			return temp;
		}
		public static DateTime ConvertUnixTimeToDateTime(int unixTimeStamp)
		{
			DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0);
			dtDateTime = dtDateTime.AddSeconds(unixTimeStamp).AddHours(5);
			return dtDateTime;
		}
	}
	public static class Names
	{
		public static List<string> techCycle = new List<string>()
		{
			"Конец технологического цикла", //00
			"Выгрузка-загрузка", //01
			"Вакуумирование на приварку", //02
			"Цикл 3", //03
			"Цикл 4", //04
			"Приварка", //05
			"Охлаждение приварки", //06
			"Осмотр приварки", //07
			"Вакуумирование на плавку", //08
			"Разведение ванны", //09
			"Плавка (основной режим)", //10
			"ВУР", //11
			"Охлаждение слитка", //12
			"Выгрузка комплекта", //13
			"Вакуумирование на оплавление", //14
			"Оплавление", //15
			"Охлаждение оплавыша", //16
			"Слив металла", //17
			"Цикл 18", //18
			"Цикл 19", //19
			"Цикл 20", //20
			"Цикл 21", //21
			"Цикл 22", //22
			"Цикл 23", //23
			"Цикл 24", //24
			"Проверка защит" //25
		};
		public static List<string> analogFull = new List<string>()
		{
			"", //000
			"", //001
			"", //002
			"", //003
			"", //004
			"", //005
			"", //006
			"", //007
			"", //008
			"", //009
			"", //010
			"", //011
			"", //012
			"Вакуум" //013
		};
		public static List<string> analogShort = new List<string>()
		{
			"", //000
			"", //001
			"", //002
			"", //003
			"", //004
			"", //005
			"", //006
			"", //007
			"", //008
			"", //009
			"", //010
			"", //011
			"", //012
			"Вакуум" //013
		};
		public static List<string> analogMeasure = new List<string>()
		{
			"", //000
			"", //001
			"", //002
			"", //003
			"", //004
			"", //005
			"", //006
			"", //007
			"", //008
			"", //009
			"", //010
			"", //011
			"", //012
			"мкм.рт.ст" //013
		};
		public static List<string> discretFull = new List<string>()
		{
			"", //000
			"", //001
			"", //002
			"", //003
			"", //004
			"", //005
			"", //006
			"", //007
			"", //008
			"", //009
			"", //010
			"", //011
			"", //012
			"", //013
			"", //014
			"", //015
			"", //016
			"", //017
			"", //018
			"", //019
			"", //020
			"", //021
			"SZO включен" //022
		};
		public static List<string> discretShort = new List<string>()
		{
			"", //000
			"", //001
			"", //002
			"", //003
			"", //004
			"", //005
			"", //006
			"", //007
			"", //008
			"", //009
			"", //010
			"", //011
			"", //012
			"", //013
			"", //014
			"", //015
			"", //016
			"", //017
			"", //018
			"", //019
			"", //020
			"", //021
			"" //022
		};
		public static List<string[]> dscretMods = new List<string[]>()
		{
			new string[] { "","" }, //000
			new string[] { "","" }, //001
			new string[] { "","" }, //002
			new string[] { "","" }, //003
			new string[] { "","" }, //004
			new string[] { "","" }, //005
			new string[] { "","" }, //006
			new string[] { "","" }, //007
			new string[] { "","" }, //008
			new string[] { "","" }, //009
			new string[] { "","" }, //010
			new string[] { "","" }, //011
			new string[] { "","" }, //012
			new string[] { "","" }, //013
			new string[] { "","" }, //014
			new string[] { "","" }, //015
			new string[] { "","" }, //016
			new string[] { "","" }, //017
			new string[] { "","" }, //018
			new string[] { "","" }, //019
			new string[] { "","" }, //020
			new string[] { "","" }, //021
			new string[] { "Нет","Да" } //022
		};
		public static List<string> protect = new List<string>()
		{
			"", //00
			"", //01
			"", //02
			"", //03
			"", //04
			"", //05
			"", //06
			"", //07
			"", //08
			"", //09
			"", //10
			"", //11
			"", //12
			"", //13
			"", //14
			"", //15
			"", //16
			"", //17
			"", //18
			"", //19
			"", //20
			"", //21
			"", //22
			"", //23
			"", //24
			"", //25
			"", //26
			"", //27
			"", //28
			"", //29
			"", //30
			"", //31
			"", //32
			"", //33
			"", //34
			"", //35
			"", //36
			"", //37
			"", //38
			"", //39
			"", //40
			"", //41
			"", //42
			"", //43
			"", //44
			"", //45
			"", //46
			"", //47
			"", //48
			"", //49
			"", //50
			"", //51
			"", //52
			"", //53
			"", //54
			"", //55
			"", //56
			"", //57
			"", //58
			"", //59
			"", //60
			"", //61
			"", //62
			"", //63
			"", //64
			"", //65
			"", //66
			"", //67
			"", //68
			"", //69
			"", //70
			"", //71
			"Алгоритм 31: Низкая скорость плавки!", //72
			"", //73
			"", //74
			"", //75
			"", //76
			"", //77
			"", //78
			"", //79
			"" //80
		};
	}
	public class Pasport
	{
		public byte numVDP = 0x00;
		public DateTime time_start = DateTime.Now;
		public DateTime time_end = DateTime.Now;
		public bool have_pasport = false;
		public int kod_npl = 0;
		public string nplav = "";
		public string rm = "";
		public string splav = "";
		public string _is = "";
		public ushort notd = 0;
		public ushort vessl = 0;
		public ushort diam = 0;
		public ushort prpl = 0;
		public string tin = "";
		public string dzap = "";
		public short dlog = 0;
		public short last = 0;
		public short dlper = 0;
		public string nazn = "";
		public ushort kompl = 0;
		public ushort izl = 0;
		public float robm = 0.0f;
		public float rizol = 0.0f;
		public ushort dkr = 0;
		public string nkon = "";
		public string pos = "";
		public string ukaz = "";
		public string zakaz = "";
		public string kat = "";
		public string pril = "";
		public string rezerved = "";

		public byte[] time_start_byte { set { time_start = byte_to_date(value); } }
		public byte[] time_end_byte   { set { time_end = byte_to_date(value);   } }
		private DateTime byte_to_date(byte[] a) { return (a.Length < 7) ? DateTime.Now : new DateTime(BitConverter.ToUInt16(a, 0), (int)a[2], (int)a[3], (int)a[4], (int)a[5], (int)a[6]); }

		public byte[] byteArr
		{
			set
			{
				var i = 0;
				if (value.Length < i + 1) return;
				numVDP = value[i]; i += 1;

				if (value.Length < i + 7) return;
				var arr = new byte[7];
				Array.Copy(value, i, arr, 0, 7);
				time_start_byte = arr; i += 7;

				if (value.Length < i + 7) return;
				arr = new byte[7];
				Array.Copy(value, i, arr, 0, 7);
				time_end_byte = arr; i += 7;

				if (value.Length < i + 1) return;
				have_pasport = value[i] == 0x01; i += 1;

				if (!have_pasport) return;

				if (value.Length < i + 4) return;
				kod_npl = BitConverter.ToInt32(value, i); i += 4;

				Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
				var enc = Encoding.GetEncoding(866);

				if (value.Length < i + 12) return;
				nplav = enc.GetString(value, i, 12); i += 12;

				if (value.Length < i + 11) return;
				rm = enc.GetString(value, i, 11); i += 11;

				if (value.Length < i + 101) return;
				splav = enc.GetString(value, i, 101); i += 101;

				if (value.Length < i + 51) return;
				_is = enc.GetString(value, i, 51); i += 51;

				if (value.Length < i + 2) return;
				notd = BitConverter.ToUInt16(value, i); i += 2;

				if (value.Length < i + 2) return;
				vessl = BitConverter.ToUInt16(value, i); i += 2;

				if (value.Length < i + 2) return;
				diam = BitConverter.ToUInt16(value, i); i += 2;

				if (value.Length < i + 2) return;
				prpl = BitConverter.ToUInt16(value, i); i += 2;

				if (value.Length < i + 9) return;
				tin = enc.GetString(value, i, 9); i += 9;

				if (value.Length < i + 9) return;
				dzap = enc.GetString(value, i, 9); i += 9;

				if (value.Length < i + 2) return;
				dlog = BitConverter.ToInt16(value, i); i += 2;

				if (value.Length < i + 2) return;
				last = BitConverter.ToInt16(value, i); i += 2;

				if (value.Length < i + 2) return;
				dlper = BitConverter.ToInt16(value, i); i += 2;

				if (value.Length < i + 51) return;
				nazn = enc.GetString(value, i, 51); i += 51;

				if (value.Length < i + 2) return;
				kompl = BitConverter.ToUInt16(value, i); i += 2;

				if (value.Length < i + 2) return;
				izl = BitConverter.ToUInt16(value, i); i += 2;

				if (value.Length < i + 4) return;
				robm = BitConverter.ToSingle(value, i); i += 4;

				if (value.Length < i + 4) return;
				rizol = BitConverter.ToSingle(value, i); i += 4;

				if (value.Length < i + 2) return;
				dkr = BitConverter.ToUInt16(value, i); i += 2;

				if (value.Length < i + 51) return;
				nkon = enc.GetString(value, i, 51); i += 51;

				if (value.Length < i + 6) return;
				pos = enc.GetString(value, i, 6); i += 6;

				if (value.Length < i + 51) return;
				ukaz = enc.GetString(value, i, 51); i += 51;

				if (value.Length < i + 51) return;
				zakaz = enc.GetString(value, i, 51); i += 51;

				if (value.Length < i + 51) return;
				kat = enc.GetString(value, i, 51); i += 51;

				if (value.Length < i + 3) return;
				pril = enc.GetString(value, i, 3); i += 3;

				if (value.Length < i + 1023) return;
				rezerved = enc.GetString(value, i, 1023); i += 1023;
			}
		}

		override public string ToString()
		{
			var r = new StringBuilder();
			r.Append("numVDP:\t"); r.Append(numVDP); r.Append('\n');
			r.Append("tStart:\t"); r.Append(time_start.ToString()); r.Append('\n');
			r.Append("tEnd:\t"); r.Append(time_end.ToString()); r.Append('\n');
			r.Append("Check:\t"); r.Append(have_pasport.ToString()); r.Append('\n');
			if (!have_pasport) return r.ToString();
			r.Append("knpl:\t"); r.Append(kod_npl); r.Append('\n');
			r.Append("nplav:\t"); r.Append(nplav); r.Append('\n');
			r.Append("rm:\t"); r.Append(rm); r.Append('\n');
			r.Append("splav:\t"); r.Append(splav); r.Append('\n');
			r.Append("_is:\t"); r.Append(_is); r.Append('\n');
			r.Append("notd:\t"); r.Append(notd); r.Append('\n');
			r.Append("vessl:\t"); r.Append(vessl); r.Append('\n');
			r.Append("diam:\t"); r.Append(diam); r.Append('\n');
			r.Append("prpl:\t"); r.Append(prpl); r.Append('\n');
			r.Append("tin:\t"); r.Append(tin); r.Append('\n');
			r.Append("dzap:\t"); r.Append(dzap); r.Append('\n');
			r.Append("dlog:\t"); r.Append(dlog); r.Append('\n');
			r.Append("last:\t"); r.Append(last); r.Append('\n');
			r.Append("dlper:\t"); r.Append(dlper); r.Append('\n');
			r.Append("nazn:\t"); r.Append(nazn); r.Append('\n');
			r.Append("kompl:\t"); r.Append(kompl); r.Append('\n');
			r.Append("izl:\t"); r.Append(izl); r.Append('\n');
			r.Append("robm:\t"); r.Append(robm); r.Append('\n');
			r.Append("rizol:\t"); r.Append(rizol); r.Append('\n');
			r.Append("dkr:\t"); r.Append(dkr); r.Append('\n');
			r.Append("nkon:\t"); r.Append(nkon); r.Append('\n');
			r.Append("pos:\t"); r.Append(pos); r.Append('\n');
			r.Append("ukaz:\t"); r.Append(ukaz); r.Append('\n');
			r.Append("zakaz:\t"); r.Append(zakaz); r.Append('\n');
			r.Append("kat:\t"); r.Append(kat); r.Append('\n');
			r.Append("pril:\t"); r.Append(pril); r.Append('\n');
			r.Append("rezerv:\t"); r.Append(rezerved); r.Append('\n');
			return r.ToString();
		}
}
	public class ADresult
	{
		public Dictionary<int, Tuple<DateTime, double?>[]> an = new Dictionary<int, Tuple<DateTime, double?>[]>();
		public Dictionary<int, Tuple<DateTime, bool?>[]> di = new Dictionary<int, Tuple<DateTime, bool?>[]>();
	}
	public class AnalogsMatrix
	{
		public Dictionary<int, Analog> matrix = new Dictionary<int, Analog>();

		public AnalogsMatrix(int vdp = 0)
		{
			matrix.Add(13, new Analog() { min = 0, max = 1000, mul = 0.1,  byteId = new ushort[] { 26, 27 } });
			if (vdp == 50)
			{
				matrix.Add(0, new Analog() { min = -10000, max = 10000, mul = 1, byteId = new ushort[] { 0, 1 } });
				matrix.Add(1, new Analog() { min = -10000, max = 10000, mul = 1, byteId = new ushort[] { 2, 3 } });
			}
		}
	}
	public class Analog
	{
		public double min = double.MinValue;
		public double max = double.MaxValue;
		public double mul = 1;
		public ushort[] byteId = new ushort[0];
	}
	public class DiscretsMatrix
	{
		public Dictionary<int, Discret> matrix = new Dictionary<int, Discret>();
		public DiscretsMatrix(int vdp = 0)
		{
			matrix.Add(22, new Discret() { byteId = 0x30, bitId = 6, invert = false });
		}
	}
	public class Discret
	{
		public ushort byteId = 0x00;
		public ushort bitId = 0x00;
		public bool invert = false;
	}
	public class Protect
	{
		public DateTime time;
		public int id;
		public int value;
	}
}
