using DataClients;
using MigraDoc.DocumentObjectModel;
using MigraDoc.DocumentObjectModel.Tables;
using MigraDoc.Rendering;
using PdfSharp.Drawing;
using PdfSharp.Fonts;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Mailing
{
	public static class GenNTC1
	{
    public static void GetNTC1(DateTime datestart, DateTime dateend)
		{
      var w = new STPClient();
      var s = datestart;
      var e = dateend;
      var r1 = new List<(string name, string dir)>();
      var r2 = new List<Pasport>();

      for (var i = s; i < e; i = i.AddDays(1))
      {
        Console.WriteLine(i.ToString(@"yyyy.MM.dd"));
        var a = w.GetListPasport(i);
        foreach (var b in a)
        {
          Console.WriteLine(b.Item1.ToString() + " | " + b.Item2.ToString());

          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("Write To File.");
      Directory.CreateDirectory(Directory.GetCurrentDirectory() + '/' + "NTC1");
      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)
        {
          var fs = new StreamWriter(Directory.GetCurrentDirectory() + "/NTC1/" + s.ToString(@"yyyyMMdd") + "-" + e.ToString(@"yyyyMMdd") + ".txt", true);
          /*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();
        }
      }
    }
  }
}
