using DataClients;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Mail;
using System.Text;

namespace Mailing
{
	class Program
	{
		static void Main(string[] args)
		{
			if (args.Length < 1)
			{
				Console.WriteLine("Need params.");
				return;
			}
			var mailList = new List<string>();
			{
				var strings = File.ReadAllLines(args.Last());
				foreach (var e in strings)
				{
					var tmp = e.Split(' ');
					foreach (var ee in tmp)
					{
						try
						{
							MailAddress m = new MailAddress(ee.ToLower());
							if (!mailList.Contains(ee.ToLower()))
								mailList.Add(ee.ToLower());
						}
						catch (Exception) { }
					}
				}
			}
			switch (args[0])
			{
				case "SZO":
					if (args.Length != 3)
					{
						Console.WriteLine("Wrong format");
						Console.WriteLine("Example: Maling SZO 2020.07.14 /mail.list");
						return;
					}

					var time = DateTime.Now;
					{
						try
						{
							var s = args[1].Split('.');
							time = new DateTime(
								Convert.ToInt32(s[0]),
								Convert.ToInt32(s[1]),
								Convert.ToInt32(s[2]));
						}
						catch
						{
							Console.WriteLine("Wrong format data");
							Console.WriteLine("Example: 2020.07.14");
							return;
						}
					}
					var ts = new DateTime(time.Year, time.Month, time.Day, 0, 0, 0, 0);
					var te = new DateTime(time.Year, time.Month, time.Day, 23, 59, 59, 999);
					var resVDP = new List<List<SZOWork>>();
					for (var i = 1; i <= 50; i++)
						resVDP.Add(CollectData.GetSZOWorks(ts, te, (ushort)i));
					CreatePDF.PDFSZO(resVDP, ts);
					var fileszo = Directory.GetCurrentDirectory() + "/SZO/" + time.ToString("yyyy-MM-dd") + ".pdf";
					foreach (var e in mailList)
						Mailing.SendMail(new MailAddress(e), fileszo);
				break;
				case "NTC1":
					if (args.Length != 4)
					{
						Console.WriteLine("Wrong format");
						Console.WriteLine("Example: Maling NTC1 2020.07.07 2020.07.14 /mail.list");
						return;
					}

					var timestart = DateTime.Now.AddDays(-7);
					var timeend = DateTime.Now;
					{
						try
						{
							var s = args[1].Split('.');
							timestart = new DateTime(
								Convert.ToInt32(s[0]),
								Convert.ToInt32(s[1]),
								Convert.ToInt32(s[2]));

							s = args[2].Split('.');
							timeend = new DateTime(
								Convert.ToInt32(s[0]),
								Convert.ToInt32(s[1]),
								Convert.ToInt32(s[2]));
						}
						catch
						{
							Console.WriteLine("Wrong format data");
							Console.WriteLine("Example: 2020.07.14");
							return;
						}
					}
					GenNTC1.GetNTC1(timestart, timeend);
					var filentc1 = Directory.GetCurrentDirectory() + "/NTC1/" + timestart.ToString(@"yyyyMMdd") + "-" + timeend.ToString(@"yyyyMMdd") + ".txt";
					foreach (var e in mailList)
						Mailing.SendMailNTC1(new MailAddress(e), filentc1);
					break;

			}
		}
	}
		public static class Mailing
	{
		private static readonly MailAddress From = new MailAddress("no-reply@vsmpo.ru", "ASCKU_32_ROBOT");
		private static SmtpClient Smtp = new SmtpClient("mail.vsmpo.ru", 25);
		private static string Subject = "Контроль работы SZO";
		private static string Body = "Ежедневная рассылка контроля работы " +
																 "водокольцевых насосов SZO на ВДП в цехе №32";
		public static void SendMail(MailAddress to, string file)
		{
			MailMessage m = new MailMessage(From, to)
			{
				Subject = Subject,
				Body = Body
			};
			m.Attachments.Add(new Attachment(file));
			Smtp.Send(m);

		}
		public static void SendMailNTC1(MailAddress to, string file)
		{
			MailMessage m = new MailMessage(From, to)
			{
				Subject = "Список плавок с низской скоростью штока",
				Body = "Еженедельная рассылка списка плавок " +
				"с низкой скоростью штока."
		};
			try
			{
				m.Attachments.Add(new Attachment(file));
			}
			catch
			{
				m.Body = m.Body + "\n В заданный период времени не было найдено подходящих плавок.";
				Console.WriteLine("Отсутствует файл для отправки.");
			}
			finally
			{
				Smtp.Send(m);
			}
		}
	}
}
