using Adaptation.Eaf.Management.ConfigurationData.CellAutomation;
using Adaptation.Ifx.Eaf.EquipmentConnector.File.Configuration;
using Adaptation.Shared;
using Adaptation.Shared.Deposition;
using Adaptation.Shared.Duplicator;
using Adaptation.Shared.Methods;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading;

namespace Adaptation.FileHandlers.DEP08SIASM;

public class FileRead : Shared.FileRead, IFileRead
{

    private readonly bool _IsXToAPC;
    private readonly bool _IsXToIQSSi;
    private readonly string _MemoryPath;
    private readonly bool _IsXToOpenInsight;
    private readonly bool _IsXToOpenInsightMetrologyViewer;
    private readonly Dictionary<string, string> _CellNames;
    private readonly bool _IsXToOpenInsightMetrologyViewerAttachments;
    private readonly string _OpenInsightCommonGatewayInterfaceContacts;

    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, bool useCyclicalForDescription, bool isEAFHosted) :
        base(new Description(), false, smtp, fileParameter, cellInstanceName, cellInstanceConnectionName, fileConnectorConfiguration, equipmentTypeName, parameterizedModelObjectDefinitionType, modelObjectParameters, equipmentDictionaryName, dummyRuns, useCyclicalForDescription, isEAFHosted)
    {
        _MinFileLength = 10;
        _NullData = string.Empty;
        _Logistics = new Logistics(this);
        if (_FileParameter is null)
            throw new Exception(cellInstanceConnectionName);
        if (_ModelObjectParameterDefinitions is null)
            throw new Exception(cellInstanceConnectionName);
        if (!_IsDuplicator)
            throw new Exception(cellInstanceConnectionName);
        _IsXToAPC = _Hyphens == (int)Hyphen.IsXToAPC;
        _CellNames = new Dictionary<string, string>();
        _IsXToIQSSi = _Hyphens == (int)Hyphen.IsXToIQSSi;
        _IsXToOpenInsight = _Hyphens == (int)Hyphen.IsXToOpenInsight;
        _IsXToOpenInsightMetrologyViewer = _Hyphens == (int)Hyphen.IsXToOpenInsightMetrologyViewer;
        _MemoryPath = GetPropertyValue(cellInstanceConnectionName, modelObjectParameters, "Path.Memory");
        _IsXToOpenInsightMetrologyViewerAttachments = _Hyphens == (int)Hyphen.IsXToOpenInsightMetrologyViewerAttachments;
        ModelObjectParameterDefinition[] cellInstanceCollection = GetProperties(cellInstanceConnectionName, modelObjectParameters, "CellInstance.", ".Path");
        _OpenInsightCommonGatewayInterfaceContacts = GetPropertyValue(cellInstanceConnectionName, modelObjectParameters, "OpenInsight.CommonGatewayInterface.Contacts");
        foreach (ModelObjectParameterDefinition modelObjectParameterDefinition in cellInstanceCollection)
            _CellNames.Add(modelObjectParameterDefinition.Name.Split('.')[1], modelObjectParameterDefinition.Value);
    }

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

    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;
    }

    void IFileRead.CheckTests(Test[] tests, bool extra)
    {
        if (_Description is not Description)
            throw new Exception();
    }

    protected static List<jpeg.Description> GetDescriptions(JsonElement[] jsonElements)
    {
        List<jpeg.Description> results = new();
        jpeg.Description description;
        JsonSerializerOptions jsonSerializerOptions = new() { NumberHandling = JsonNumberHandling.AllowReadingFromString | JsonNumberHandling.WriteAsString };
        foreach (JsonElement jsonElement in jsonElements)
        {
            if (jsonElement.ValueKind != JsonValueKind.Object)
                throw new Exception();
            description = JsonSerializer.Deserialize<jpeg.Description>(jsonElement.ToString(), jsonSerializerOptions);
            results.Add(description);
        }
        return results;
    }

    private Tuple<string, Test[], JsonElement[], List<FileInfo>> GetExtractResult(string reportFullPath, DateTime dateTime)
    {
        Tuple<string, Test[], JsonElement[], List<FileInfo>> results;
        string duplicateDirectory;
        Tuple<string, string[], string[]> pdsf = ProcessDataStandardFormat.GetLogisticsColumnsAndBody(reportFullPath);
        _Logistics = new Logistics(reportFullPath, pdsf.Item1);
        SetFileParameterLotIDToLogisticsMID();
        JsonElement[] jsonElements = ProcessDataStandardFormat.GetArray(pdsf);
        List<jpeg.Description> descriptions = GetDescriptions(jsonElements);
        Tuple<Test[], Dictionary<Test, List<Shared.Properties.IDescription>>> tuple = GetTuple(this, from l in descriptions select (Shared.Properties.IDescription)l, extra: false);
        results = new Tuple<string, Test[], JsonElement[], List<FileInfo>>(pdsf.Item1, tuple.Item1, jsonElements, new List<FileInfo>());
        bool isDummyRun = _DummyRuns.Any() && _DummyRuns.ContainsKey(_Logistics.JobID) && _DummyRuns[_Logistics.JobID].Any() && (from l in _DummyRuns[_Logistics.JobID] where l == _Logistics.Sequence select 1).Any();
        if (isDummyRun)
        {
            try
            { File.SetLastWriteTime(reportFullPath, dateTime); }
            catch (Exception) { }
        }
        string[] segments = Path.GetFileNameWithoutExtension(reportFullPath).Split('_');
        if (_IsXToIQSSi)
            duplicateDirectory = string.Concat(_FileConnectorConfiguration.TargetFileLocation, @"\All");
        else if (!_IsXToOpenInsight)
            duplicateDirectory = string.Concat(_FileConnectorConfiguration.TargetFileLocation, @"\", segments[0]);
        else
            duplicateDirectory = string.Concat(Path.GetDirectoryName(Path.GetDirectoryName(_FileConnectorConfiguration.TargetFileLocation)), @"\Data");
        if (segments.Length > 2)
            duplicateDirectory = string.Concat(duplicateDirectory, @"-", segments[2]);
        if (!Directory.Exists(duplicateDirectory))
            _ = Directory.CreateDirectory(duplicateDirectory);
        if (isDummyRun || _FileConnectorConfiguration.FileScanningIntervalInSeconds > 0)
        {
            if (!Directory.Exists(duplicateDirectory))
                _ = Directory.CreateDirectory(duplicateDirectory);
            string successDirectory;
            if (!_IsXToAPC)
                successDirectory = string.Empty;
            else
            {
                successDirectory = string.Concat(Path.GetDirectoryName(_FileConnectorConfiguration.TargetFileLocation), @"\ViewerPath");
                if (!Directory.Exists(successDirectory))
                    _ = Directory.CreateDirectory(successDirectory);
            }
            List<Tuple<Shared.Properties.IScopeInfo, string>> tuples = new();
            string duplicateFile = string.Concat(duplicateDirectory, @"\", Path.GetFileName(reportFullPath));
            string weekOfYear = _Calendar.GetWeekOfYear(_Logistics.DateTimeFromSequence, CalendarWeekRule.FirstDay, DayOfWeek.Sunday).ToString("00");
            string weekDirectory = string.Concat(_Logistics.DateTimeFromSequence.ToString("yyyy"), "_Week_", weekOfYear, @"\", _Logistics.DateTimeFromSequence.ToString("yyyy-MM-dd"));
            string logisticsSequenceMemoryDirectory = string.Concat(_MemoryPath, @"\", _EquipmentType, @"\Source\", weekDirectory, @"\", _Logistics.Sequence);
            if (!Directory.Exists(logisticsSequenceMemoryDirectory))
                _ = Directory.CreateDirectory(logisticsSequenceMemoryDirectory);
            if (_IsXToAPC)
            {
                if (!isDummyRun && _IsEAFHosted)
                    File.Copy(reportFullPath, duplicateFile, overwrite: true);
            }
            else
            {
                if (_IsXToOpenInsightMetrologyViewer)
                    _Log.Debug("Not sure if this data should post to OI Metrology Viewer?");
                else if (!_IsXToOpenInsight)
                    throw new Exception($"Only {nameof(_IsXToOpenInsight)} is codded!");
                else if (_IsXToOpenInsight)
                {
                    if (!isDummyRun && _IsEAFHosted)
                        ProcessData.PostOpenInsightCommonGatewayInterfaceContacts(this, _Logistics, _OpenInsightCommonGatewayInterfaceContacts, dateTime, logisticsSequenceMemoryDirectory, descriptions);
                }
                else
                    throw new Exception();
            }
        }
        if (_IsXToOpenInsightMetrologyViewerAttachments)
        {
            string destinationDirectory;
            //string destinationDirectory = WriteScopeInfo(_ProgressPath, _Logistics, dateTime, duplicateDirectory, tuples);
            FileInfo fileInfo = new(reportFullPath);
            string logisticsSequence = _Logistics.Sequence.ToString();
            if (fileInfo.Exists && fileInfo.LastWriteTime < fileInfo.CreationTime)
                File.SetLastWriteTime(reportFullPath, fileInfo.CreationTime);
            string jobIdDirectory = string.Concat(Path.GetDirectoryName(Path.GetDirectoryName(_FileConnectorConfiguration.TargetFileLocation)), @"\", _Logistics.JobID);
            if (!Directory.Exists(jobIdDirectory))
                _ = Directory.CreateDirectory(jobIdDirectory);
            string[] matchDirectories;
            if (!_IsEAFHosted)
                matchDirectories = new string[] { Path.GetDirectoryName(Path.GetDirectoryName(reportFullPath)) };
            else
                matchDirectories = Directory.GetDirectories(jobIdDirectory, string.Concat(_Logistics.MID, '*', logisticsSequence, '*'), SearchOption.TopDirectoryOnly);
            if ((matchDirectories is null) || matchDirectories.Length != 1)
                throw new Exception("Didn't find directory by logistics sequence");
            destinationDirectory = matchDirectories[0];
            if (isDummyRun)
                Shared0607(reportFullPath, duplicateDirectory, logisticsSequence, destinationDirectory);
            else
            {
                var wsRequest = new { _Logistics, descriptions };
                JsonSerializerOptions jsonSerializerOptions = new() { WriteIndented = true };
                string json = JsonSerializer.Serialize(wsRequest, wsRequest.GetType(), jsonSerializerOptions);
                if (_IsEAFHosted)
                    Shared1277(reportFullPath, destinationDirectory, logisticsSequence, jobIdDirectory, json);
                else
                {
                    string jsonFileName = Path.ChangeExtension(reportFullPath, ".json");
                    string historicalText = File.ReadAllText(jsonFileName);
                    if (json != historicalText)
                        throw new Exception("File doesn't match historical!");
                }
            }
        }
        return results;
    }

}