using Adaptation.Eaf.Management.ConfigurationData.CellAutomation;
using Adaptation.Ifx.Eaf.EquipmentConnector.File.Configuration;
using Adaptation.Shared;
using Adaptation.Shared.Methods;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text.Json;
using System.Text.RegularExpressions;

namespace Adaptation.FileHandlers.jpeg;

public class FileRead : Shared.FileRead, IFileRead
{

    protected long _LastChange;
    protected string _LastText;
    protected readonly int _EndX;
    protected readonly int _EndY;
    protected readonly Size _Size;
    protected readonly int _StartX;
    protected readonly int _StartY;
    protected string _TessDataDirectory;
    protected List<int> _PreviousTotalDeltaCollection;
    protected readonly Dictionary<string, string> _Reactors;
    protected readonly List<Tuple<string, Color[]>> _ColorCollections;

    public FileRead(ISMTP smtp, Dictionary<string, string> fileParameter, string cellInstanceName, string cellInstanceConnectionName, FileConnectorConfiguration fileConnectorConfiguration, string equipmentTypeName, string parameterizedModelObjectDefinitionType, IList<ModelObjectParameterDefinition> modelObjectParameters, string equipmentDictionaryName, Dictionary<string, List<long>> dummyRuns, Dictionary<long, List<string>> staticRuns, bool useCyclicalForDescription, bool isEAFHosted) :
        base(new Description(), true, smtp, fileParameter, cellInstanceName, cellInstanceConnectionName, fileConnectorConfiguration, equipmentTypeName, parameterizedModelObjectDefinitionType, modelObjectParameters, equipmentDictionaryName, dummyRuns, staticRuns, useCyclicalForDescription, isEAFHosted)
    {
        _MinFileLength = 36790;
        _NullData = string.Empty;
        _LastText = string.Empty;
        _Logistics = new(this);
        if (_FileParameter is null)
            throw new Exception(cellInstanceConnectionName);
        if (_ModelObjectParameterDefinitions is null)
            throw new Exception(cellInstanceConnectionName);
        if (_IsDuplicator)
            throw new Exception(cellInstanceConnectionName);
        _LastChange = DateTime.Now.AddDays(-1).Ticks;
        _Reactors = new Dictionary<string, string>();
        string alias = GetPropertyValue(cellInstanceConnectionName, modelObjectParameters, string.Concat("CellInstance.", cellInstanceName, ".Alias"));
        _Reactors.Add(alias, alias);
        string x86 = Path.Combine(AppContext.BaseDirectory, "x86");
        if (!Directory.Exists(x86))
            _ = Directory.CreateDirectory(x86);
        _TessDataDirectory = Path.Combine(AppContext.BaseDirectory, "tessdata");
        if (!Directory.Exists(_TessDataDirectory))
            _ = Directory.CreateDirectory(_TessDataDirectory);
        string pdfttfSource = Path.Combine(AppContext.BaseDirectory, "pdf.ttf");
        string pdfttfDestination = Path.Combine(_TessDataDirectory, Path.GetFileName(pdfttfSource));
        if (File.Exists(pdfttfSource) && !File.Exists(pdfttfDestination))
            File.Copy(pdfttfSource, pdfttfDestination);
        string tesseract41dllSource = Path.Combine(AppContext.BaseDirectory, "tesseract41.dll");
        string tesseract41dllDestination = Path.Combine(x86, Path.GetFileName(tesseract41dllSource));
        if (File.Exists(tesseract41dllSource) && !File.Exists(tesseract41dllDestination))
            File.Copy(tesseract41dllSource, tesseract41dllDestination);
        string engtraineddataSource = Path.Combine(AppContext.BaseDirectory, "eng.traineddata");
        string engtraineddataDestination = Path.Combine(_TessDataDirectory, Path.GetFileName(engtraineddataSource));
        if (File.Exists(engtraineddataSource) && !File.Exists(engtraineddataDestination))
            File.Copy(engtraineddataSource, engtraineddataDestination);
        string leptonica1800dllSource = Path.Combine(AppContext.BaseDirectory, "leptonica-1.80.0.dll");
        string leptonica1800dllDestination = Path.Combine(x86, Path.GetFileName(leptonica1800dllSource));
        if (File.Exists(leptonica1800dllSource) && !File.Exists(leptonica1800dllDestination))
            File.Copy(leptonica1800dllSource, leptonica1800dllDestination);
        ModelObjectParameterDefinition[] images = GetProperties(cellInstanceConnectionName, modelObjectParameters, "Image.");
        string startX = GetPropertyValue(cellInstanceConnectionName, images, "Image.StartX");
        string startY = GetPropertyValue(cellInstanceConnectionName, images, "Image.StartY");
        string endX = GetPropertyValue(cellInstanceConnectionName, images, "Image.EndX");
        string endY = GetPropertyValue(cellInstanceConnectionName, images, "Image.EndY");
        _StartX = int.Parse(startX);
        _StartY = int.Parse(startY);
        _EndX = int.Parse(endX);
        _EndY = int.Parse(endY);
        _ColorCollections = new();
        _PreviousTotalDeltaCollection = new();
        string masterImageDirectory = GetPropertyValue(cellInstanceConnectionName, modelObjectParameters, "Path.Memory.Master.Images");
        List<(string File, Size Size, Color[] Colors)> colorCollections = ProcessData.GetColorCollections(_StartX, _StartY, _EndX, _EndY, masterImageDirectory);
        int[] distinctSizes = (from l in colorCollections select l.Size.Width * l.Size.Height).Distinct().ToArray();
        if (distinctSizes.Length != 1)
            throw new Exception($"All Master Images must be the same size{Environment.NewLine}{string.Join(Environment.NewLine, distinctSizes)}");
        _Size = colorCollections[0].Size;
        _ColorCollections.AddRange(from l in colorCollections select new Tuple<string, Color[]>(l.File, l.Colors));
    }

    void IFileRead.Move(Tuple<string, Test[], JsonElement[], List<FileInfo>> extractResults, Exception exception) => Move(extractResults);

    void IFileRead.WaitForThread() => WaitForThread(thread: null, threadExceptions: null);

    string IFileRead.GetEventDescription()
    {
        string result = _Description.GetEventDescription();
        return result;
    }

    List<string> IFileRead.GetHeaderNames()
    {
        List<string> results = _Description.GetHeaderNames();
        return results;
    }

    string[] IFileRead.Move(Tuple<string, Test[], JsonElement[], List<FileInfo>> extractResults, string to, string from, string resolvedFileLocation, Exception exception)
    {
        string[] results = Move(extractResults, to, from, resolvedFileLocation, exception);
        return results;
    }

    JsonProperty[] IFileRead.GetDefault()
    {
        JsonProperty[] results = _Description.GetDefault(this, _Logistics);
        return results;
    }

    Dictionary<string, string> IFileRead.GetDisplayNamesJsonElement()
    {
        Dictionary<string, string> results = _Description.GetDisplayNamesJsonElement(this);
        return results;
    }

    List<IDescription> IFileRead.GetDescriptions(IFileRead fileRead, List<Test> tests, IProcessData processData)
    {
        List<IDescription> results = _Description.GetDescriptions(fileRead, _Logistics, tests, processData);
        return results;
    }

    Tuple<string, Test[], JsonElement[], List<FileInfo>> IFileRead.GetExtractResult(string reportFullPath, string eventName)
    {
        Tuple<string, Test[], JsonElement[], List<FileInfo>> results;
        if (string.IsNullOrEmpty(eventName))
            throw new Exception();
        _ReportFullPath = reportFullPath;
        DateTime dateTime = DateTime.Now;
        results = GetExtractResult(reportFullPath, dateTime);
        if (results.Item3 is null)
            results = new Tuple<string, Test[], JsonElement[], List<FileInfo>>(results.Item1, Array.Empty<Test>(), JsonSerializer.Deserialize<JsonElement[]>("[]"), results.Item4);
        if (results.Item3.Length > 0 && _IsEAFHosted)
            WritePDSF(this, results.Item3);
        UpdateLastTicksDuration(DateTime.Now.Ticks - dateTime.Ticks);
        return results;
    }

    Tuple<string, Test[], JsonElement[], List<FileInfo>> IFileRead.ReExtract()
    {
        Tuple<string, Test[], JsonElement[], List<FileInfo>> results;
        List<string> headerNames = _Description.GetHeaderNames();
        Dictionary<string, string> keyValuePairs = _Description.GetDisplayNamesJsonElement(this);
        results = ReExtract(this, headerNames, keyValuePairs);
        return results;
    }

    private Tuple<string, Test[], JsonElement[], List<FileInfo>> GetExtractResult(string reportFullPath, DateTime dateTime)
    {
        Tuple<string, Test[], JsonElement[], List<FileInfo>> results = new(string.Empty, null, null, new List<FileInfo>());
        _Logistics = new Logistics(this, reportFullPath, useSplitForMID: true);
        SetFileParameterLotIDToLogisticsMID();
        if (_Logistics.FileInfo.Length < _MinFileLength)
            results.Item4.Add(_Logistics.FileInfo);
        else
        {
            IProcessData iProcessData = new ProcessData(this, _Logistics, results.Item4, _StartX, _StartY, _EndX, _EndY, _Size, _ColorCollections);
            if (iProcessData is not ProcessData processData)
                throw new Exception(string.Concat("A) No Data - ", dateTime.Ticks));
            string lastText = Regex.Replace(_LastText, @"[\\,\/,\:,\*,\?,\"",\<,\>,\|]", "_").Split('\r')[0].Split('\n')[0];
            string text = Regex.Replace(processData.Text, @"[\\,\/,\:,\*,\?,\"",\<,\>,\|]", "_").Split('\r')[0].Split('\n')[0];
            if (string.IsNullOrEmpty(lastText))
                lastText = "_";
            string mid = string.Concat(lastText, '-', text);
            SetFileParameterLotID(mid);
            string processJobID = iProcessData.GetCurrentReactor(this, _Logistics, _Reactors);
            _Logistics.Update(mid, processJobID);
            if (!iProcessData.Details.Any())
                throw new Exception(string.Concat("B) No Data - ", dateTime.Ticks));
            if (processData.Text == _LastText && _LastChange > DateTime.Now.AddMinutes(-30).Ticks)
                File.Delete(reportFullPath);
            else
            {
                if (processData.Text != _LastText)
                {
                    if (_PreviousTotalDeltaCollection.Count > 50)
                    {
                        double average = _PreviousTotalDeltaCollection.Average();
                        double sum = _PreviousTotalDeltaCollection.Sum(l => Math.Pow(l - average, 2));
                        double standardDeviation = Math.Sqrt(sum / _PreviousTotalDeltaCollection.Count);
                        double deviation = standardDeviation * 3;
                        double upper = average + deviation;
                        double lower = average - deviation;
                        var dynamic = new { _Logistics.Sequence, average, sum, standardDeviation, upper, lower, processData.TotalDelta, processData.Text, _PreviousTotalDeltaCollection.Count };
                        string message = JsonSerializer.Serialize(dynamic, new JsonSerializerOptions { WriteIndented = true });
                        _Log.Debug(message);
                        if (processData.TotalDelta > upper || processData.TotalDelta < lower)
                            _SMTP.SendHighPriorityEmailMessage($"Exception:{_CellInstanceConnectionName}", message);
                    }
                    _PreviousTotalDeltaCollection.Add(processData.TotalDelta);
                    if (_PreviousTotalDeltaCollection.Count > short.MaxValue)
                    {
                        for (int i = 1; i < 500; i++)
                            _PreviousTotalDeltaCollection.RemoveAt(0);
                    }
                }
                _LastText = processData.Text;
                _LastChange = DateTime.Now.Ticks;
                results = iProcessData.GetResults(this, _Logistics, results.Item4);
            }
        }
        return results;
    }

}