using Adaptation.Shared.Methods;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace Adaptation.Shared;

public class Logistics : ILogistics
{

    protected readonly DateTime _DateTimeFromSequence;
    protected readonly FileInfo _FileInfo;
    protected readonly string _JobID;
    protected readonly List<string> _Logistics1;
    protected readonly List<Logistics2> _Logistics2;
    protected string _MID;
    protected readonly string _MesEntity;
    protected readonly object _NullData;
    protected string _ProcessJobID;
    protected readonly string _ReportFullPath;
    protected readonly long _Sequence;
    protected readonly double _TotalSecondsSinceLastWriteTimeFromSequence;

    public DateTime DateTimeFromSequence => _DateTimeFromSequence;
    public FileInfo FileInfo => _FileInfo;
    public string JobID => _JobID;
    public List<string> Logistics1 => _Logistics1;
    public List<Logistics2> Logistics2 => _Logistics2;
    public string MID => _MID;
    public string MesEntity => _MesEntity;
    public object NullData => _NullData;
    public string ProcessJobID => _ProcessJobID;
    public string ReportFullPath => _ReportFullPath;
    public long Sequence => _Sequence;
    public double TotalSecondsSinceLastWriteTimeFromSequence => _TotalSecondsSinceLastWriteTimeFromSequence;

    public Logistics(IFileRead fileRead)
    {
        DateTime dateTime = DateTime.Now;
        _NullData = null;
        _Sequence = dateTime.Ticks;
        _DateTimeFromSequence = dateTime;
        _JobID = fileRead.CellInstanceName;
        _TotalSecondsSinceLastWriteTimeFromSequence = (DateTime.Now - dateTime).TotalSeconds;
        _MesEntity = DefaultMesEntity(dateTime);
        _ReportFullPath = string.Empty;
        _ProcessJobID = nameof(ProcessJobID);
        _MID = nameof(MID);
        _Logistics1 = new string[] { string.Concat("LOGISTICS_1", '\t', "A_JOBID=", JobID, ";A_MES_ENTITY=", MesEntity, ";") }.ToList();
        _Logistics2 = new List<Logistics2>();
    }

    public Logistics(IFileRead fileRead, string reportFullPath, bool useSplitForMID, int? fileInfoLength = null)
    {
        if (string.IsNullOrEmpty(fileRead.CellInstanceName))
            throw new Exception();
        if (string.IsNullOrEmpty(fileRead.MesEntity))
            throw new Exception();
        _NullData = fileRead.NullData;
        _FileInfo = new(reportFullPath);
        DateTime dateTime = _FileInfo.LastWriteTime;
        if (fileInfoLength.HasValue && _FileInfo.Length < fileInfoLength.Value)
            dateTime = dateTime.AddTicks(-1);
        _JobID = fileRead.CellInstanceName;
        _Sequence = dateTime.Ticks;
        _DateTimeFromSequence = dateTime;
        _TotalSecondsSinceLastWriteTimeFromSequence = (DateTime.Now - dateTime).TotalSeconds;
        _MesEntity = fileRead.MesEntity;
        _ReportFullPath = _FileInfo.FullName;
        _ProcessJobID = nameof(ProcessJobID);
        string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(_FileInfo.FullName);
        if (useSplitForMID)
        {
            if (fileNameWithoutExtension.IndexOf(".") > -1)
                fileNameWithoutExtension = fileNameWithoutExtension.Split('.')[0].Trim();
            if (fileNameWithoutExtension.IndexOf("_") > -1)
                fileNameWithoutExtension = fileNameWithoutExtension.Split('_')[0].Trim();
            if (fileNameWithoutExtension.IndexOf("-") > -1)
                fileNameWithoutExtension = fileNameWithoutExtension.Split('-')[0].Trim();
        }
        _MID = string.Concat(fileNameWithoutExtension.Substring(0, 1).ToUpper(), fileNameWithoutExtension.Substring(1).ToLower());
        _Logistics1 = new string[] { string.Concat("LOGISTICS_1", '\t', "A_JOBID=", JobID, ";A_MES_ENTITY=", MesEntity, ";") }.ToList();
        _Logistics2 = new List<Logistics2>();
    }

    public Logistics(string reportFullPath, string logistics)
    {
        string key;
        DateTime dateTime;
        string[] segments;
        _FileInfo = new(reportFullPath);
        _Logistics1 = logistics.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).ToList();
        if (!Logistics1.Any() || !Logistics1[0].StartsWith("LOGISTICS_1"))
        {
            _NullData = null;
            _JobID = "null";
            dateTime = _FileInfo.LastWriteTime;
            _Sequence = dateTime.Ticks;
            _DateTimeFromSequence = dateTime;
            _TotalSecondsSinceLastWriteTimeFromSequence = (DateTime.Now - dateTime).TotalSeconds;
            _MesEntity = DefaultMesEntity(dateTime);
            _ReportFullPath = reportFullPath;
            _ProcessJobID = "R##";
            _MID = "null";
            _Logistics1 = new string[] { string.Concat("LOGISTICS_1", '\t', "A_JOBID=", JobID, ";A_MES_ENTITY=", MesEntity, ";") }.ToList();
            _Logistics2 = new List<Logistics2>();
        }
        else
        {
            string logistics1Line1 = Logistics1[0];
            key = "NULL_DATA=";
            if (!logistics1Line1.Contains(key))
                _NullData = null;
            else
            {
                segments = logistics1Line1.Split(new string[] { key }, StringSplitOptions.RemoveEmptyEntries);
                _NullData = segments[1].Split(';')[0];
            }
            key = "JOBID=";
            if (!logistics1Line1.Contains(key))
                _JobID = "null";
            else
            {
                segments = logistics1Line1.Split(new string[] { key }, StringSplitOptions.RemoveEmptyEntries);
                _JobID = segments[1].Split(';')[0];
            }
            key = "SEQUENCE=";
            if (!logistics1Line1.Contains(key))
                dateTime = _FileInfo.LastWriteTime;
            else
            {
                segments = logistics1Line1.Split(new string[] { key }, StringSplitOptions.RemoveEmptyEntries);
                if (!long.TryParse(segments[1].Split(';')[0].Split('.')[0], out long sequence) || sequence < new DateTime(1999, 1, 1).Ticks)
                    dateTime = _FileInfo.LastWriteTime;
                else
                    dateTime = new DateTime(sequence);
            }
            _Sequence = dateTime.Ticks;
            _DateTimeFromSequence = dateTime;
            _TotalSecondsSinceLastWriteTimeFromSequence = (DateTime.Now - dateTime).TotalSeconds;
            DateTime lastWriteTime = _FileInfo.LastWriteTime;
            if (TotalSecondsSinceLastWriteTimeFromSequence > 600)
            {
                if (lastWriteTime != dateTime)
                    try
                    { File.SetLastWriteTime(reportFullPath, dateTime); }
                    catch (Exception) { }
            }
            key = "MES_ENTITY=";
            if (!logistics1Line1.Contains(key))
                _MesEntity = DefaultMesEntity(dateTime);
            else
            {
                segments = logistics1Line1.Split(new string[] { key }, StringSplitOptions.RemoveEmptyEntries);
                _MesEntity = segments[1].Split(';')[0];
            }
            _ReportFullPath = reportFullPath;
            key = "PROCESS_JOBID=";
            if (!logistics1Line1.Contains(key))
                _ProcessJobID = "R##";
            else
            {
                segments = logistics1Line1.Split(new string[] { key }, StringSplitOptions.RemoveEmptyEntries);
                _ProcessJobID = segments[1].Split(';')[0];
            }
            key = "MID=";
            if (!logistics1Line1.Contains(key))
                _MID = "null";
            else
            {
                segments = logistics1Line1.Split(new string[] { key }, StringSplitOptions.RemoveEmptyEntries);
                _MID = segments[1].Split(';')[0];
            }
        }
        Logistics2 logistics2;
        _Logistics2 = new List<Logistics2>();
        for (int i = 1; i < Logistics1.Count; i++)
        {
            if (Logistics1[i].StartsWith("LOGISTICS_2"))
            {
                logistics2 = new Logistics2(Logistics1[i]);
                Logistics2.Add(logistics2);
            }
        }
        for (int i = Logistics1.Count - 1; i > -1; i--)
        {
            if (Logistics1[i].StartsWith("LOGISTICS_2"))
                Logistics1.RemoveAt(i);
        }
    }

    private static string DefaultMesEntity(DateTime dateTime) => string.Concat(dateTime.Ticks, "_MES_ENTITY");

    internal void Update(string mid, string processJobID)
    {
        _MID = mid;
        _ProcessJobID = processJobID;
    }

}