using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataClient.Struct
{
  public class Pasport
  {
    public Pasport() { }
    public Pasport(byte[] arr)
		{
      this.PaspByte = arr;
		}
    //functions
    private DateTime? ByteToDate(byte[] arr) { return ByteToDate(arr, 0); }
    private DateTime? ByteToDate(byte[] arr, int index)
    {
      if (arr == null || (index + 7) > arr.Length) return null;
      return new DateTime(arr[index] | arr[index + 1] << 8, arr[index + 2], arr[index + 3], arr[index + 4], arr[index + 5], arr[index + 6]);
    }
    private byte[] DateToByte(DateTime? date)
    {
      if (!date.HasValue) return null;
      return new byte[]
      {
        BitConverter.GetBytes(date.Value.Year)[0],
        BitConverter.GetBytes(date.Value.Year)[1],
        (byte)date.Value.Month,
        (byte)date.Value.Day,
        (byte)date.Value.Hour,
        (byte)date.Value.Minute,
        (byte)date.Value.Second
      };
    }

    private string ByteToString(byte[] arr) { return ByteToString(arr, 0, arr.Length); }
    private string ByteToString(byte[] arr, int index, int length)
    {
      if (arr == null || arr.Length == 0 || index < 0 || length < 0)
        return "";
      Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
      var enc = Encoding.GetEncoding(866);
      var res = new List<byte>();
      for (var i = index; i < (index + length); i++)
      {
        if (i >= arr.Length) break;
        if (arr[i] == 0x00) continue;
        res.Add(arr[i]);
      }
      return enc.GetString(res.ToArray());
    }

    private byte[] StringToByte(uint? size = null, string str = null)
    {
      Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
      var enc = Encoding.GetEncoding(866);
      var szArr = size ?? ((!string.IsNullOrEmpty(str)) ? (uint)(str.Length + 1) : 0);
      var strArr = (string.IsNullOrEmpty(str)) ? Array.Empty<byte>() : enc.GetBytes(str);
      var res = new byte[szArr];
      for (var i = 0; i < res.Length; i++)
        res[i] = (byte)((i == res.Length - 1) ? 0x00 : (i < strArr.Length) ? strArr[i] : 0x00);
      return res;
    }



    //params
    public bool HasData { get { return numVDP.HasValue && dStart.HasValue && dEnd.HasValue; } }
    public byte? numVDP = null;
    public DateTime? dStart = null;
    public DateTime? dEnd = null;

    public bool hasPasport = false;
    public int kod_npl = 0;
    public string nplav = null; //12
    public string rm = null; //11
    public string splav = null; //101
    public string iS = null; //51
    public ushort notd = 0;
    public ushort vessl = 0;
    public ushort diam = 0;
    public ushort prpl = 0;
    public string tin = null; //9
    public string dzap = null; //9
    public short dlog = 0;
    public short last = 0;
    public short dlper = 0;
    public string nazn = null; //51
    public ushort kompl = 0;
    public ushort izl = 0;
    public float robm = 0;
    public float rizol = 0;
    public ushort dkr = 0;
    public string nkon = null; //51
    public string pos = null; //6
    public string ukaz = null; //51
    public string zakaz = null; //51
    public string kat = null; //51
    public string pril = null; //3
    public string rezerved = null; //1023

    public byte[] PaspByte
    {
      get
      {
        var res = new List<byte>();

        res.Add((byte)(HasData ? 0x01 : 0x00));
        if (!HasData) return res.ToArray();

        res.Add(numVDP.Value);
        res.AddRange(DateToByte(dStart));
        res.AddRange(DateToByte(dEnd));
        res.Add((byte)(hasPasport ? 0x01 : 0x00));
        if (!hasPasport) return res.ToArray();

        res.AddRange(BitConverter.GetBytes(kod_npl));
        res.AddRange(StringToByte(12, nplav));
        res.AddRange(StringToByte(11, rm));
        res.AddRange(StringToByte(101, splav));
        res.AddRange(StringToByte(51, iS));
        res.AddRange(BitConverter.GetBytes(notd));
        res.AddRange(BitConverter.GetBytes(vessl));
        res.AddRange(BitConverter.GetBytes(diam));
        res.AddRange(BitConverter.GetBytes(prpl));
        res.AddRange(StringToByte(9, tin));
        res.AddRange(StringToByte(9, dzap));
        res.AddRange(BitConverter.GetBytes(dlog));
        res.AddRange(BitConverter.GetBytes(last));
        res.AddRange(BitConverter.GetBytes(dlper));
        res.AddRange(StringToByte(51, nazn));
        res.AddRange(BitConverter.GetBytes(kompl));
        res.AddRange(BitConverter.GetBytes(izl));
        res.AddRange(BitConverter.GetBytes(robm));
        res.AddRange(BitConverter.GetBytes(rizol));
        res.AddRange(BitConverter.GetBytes(dkr));
        res.AddRange(StringToByte(51, nkon));
        res.AddRange(StringToByte(6, pos));
        res.AddRange(StringToByte(51, ukaz));
        res.AddRange(StringToByte(51, zakaz));
        res.AddRange(StringToByte(51, kat));
        res.AddRange(StringToByte(3, pril));
        res.AddRange(StringToByte(1023, rezerved));

        return res.ToArray();
      }
      set
      {
        var count = 0;
        if (value == null) return;
        if (value.Length < (count + 1) || value[count] != 0x01) return;
        count++;
        if (value.Length < (count + 1)) return;
        numVDP = value[count]; count++;
        if (value.Length < (count + 7)) return;
        dStart = ByteToDate(value, count); count += 7;
        if (value.Length < (count + 7)) return;
        dEnd = ByteToDate(value, count); count += 7;
        if (value.Length < (count + 1) || value[count] != 0x01) { hasPasport = false; return; }
        hasPasport = true; count++;
        if (value.Length < (count + 4)) return;
        kod_npl = BitConverter.ToInt32(value, count); count += 4;
        if (value.Length < (count + 12)) return;
        nplav = ByteToString(value, count, 12); count += 12;
        if (value.Length < (count + 11)) return;
        rm = ByteToString(value, count, 11); count += 11;
        if (value.Length < (count + 101)) return;
        splav = ByteToString(value, count, 101); count += 101;
        if (value.Length < (count + 51)) return;
        iS = ByteToString(value, count, 51); count += 51;
        if (value.Length < (count + 2)) return;
        notd = BitConverter.ToUInt16(value, count); count += 2;
        if (value.Length < (count + 2)) return;
        vessl = BitConverter.ToUInt16(value, count); count += 2;
        if (value.Length < (count + 2)) return;
        diam = BitConverter.ToUInt16(value, count); count += 2;
        if (value.Length < (count + 2)) return;
        prpl = BitConverter.ToUInt16(value, count); count += 2;
        if (value.Length < (count + 9)) return;
        tin = ByteToString(value, count, 9); count += 9;
        if (value.Length < (count + 9)) return;
        dzap = ByteToString(value, count, 9); count += 9;
        if (value.Length < (count + 2)) return;
        dlog = BitConverter.ToInt16(value, count); count += 2;
        if (value.Length < (count + 2)) return;
        last = BitConverter.ToInt16(value, count); count += 2;
        if (value.Length < (count + 2)) return;
        dlper = BitConverter.ToInt16(value, count); count += 2;
        if (value.Length < (count + 51)) return;
        nazn = ByteToString(value, count, 51); count += 51;
        if (value.Length < (count + 2)) return;
        kompl = BitConverter.ToUInt16(value, count); count += 2;
        if (value.Length < (count + 2)) return;
        izl = BitConverter.ToUInt16(value, count); count += 2;
        if (value.Length < (count + 4)) return;
        robm = BitConverter.ToSingle(value, count); count += 4;
        if (value.Length < (count + 4)) return;
        rizol = BitConverter.ToSingle(value, count); count += 4;
        if (value.Length < (count + 2)) return;
        dkr = BitConverter.ToUInt16(value, count); count += 2;
        if (value.Length < (count + 51)) return;
        nkon = ByteToString(value, count, 51); count += 51;
        if (value.Length < (count + 6)) return;
        pos = ByteToString(value, count, 6); count += 6;
        if (value.Length < (count + 51)) return;
        ukaz = ByteToString(value, count, 51); count += 51;
        if (value.Length < (count + 51)) return;
        zakaz = ByteToString(value, count, 51); count += 51;
        if (value.Length < (count + 51)) return;
        kat = ByteToString(value, count, 51); count += 51;
        if (value.Length < (count + 3)) return;
        pril = ByteToString(value, count, 3); count += 3;
        if (value.Length < (count + 1023)) return;
        rezerved = ByteToString(value, count, 1023); count += 1023;
      }
    }
  }

}
