using Adaptation.Eaf.Management.ConfigurationData.CellAutomation;
using Adaptation.Ifx.Eaf.EquipmentConnector.File.Configuration;
using Adaptation.Shared;
using Adaptation.Shared.Duplicator;
using Adaptation.Shared.Methods;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;

namespace Adaptation.FileHandlers.DownloadWorkItems;

public class FileRead : Shared.FileRead, IFileRead
{

    internal static string Json { get; private set; }

    private readonly string _API;
    private readonly Timer _Timer;
    private readonly string _Query;
    private readonly string _BasePage;
    private readonly HttpClient _HttpClient;

    public FileRead(ISMTP smtp, Dictionary<string, string> fileParameter, string cellInstanceName, int? connectionCount, 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(), false, smtp, fileParameter, cellInstanceName, connectionCount, cellInstanceConnectionName, fileConnectorConfiguration, equipmentTypeName, parameterizedModelObjectDefinitionType, modelObjectParameters, equipmentDictionaryName, dummyRuns, staticRuns, useCyclicalForDescription, isEAFHosted: connectionCount is null)
    {
        _MinFileLength = 10;
        _NullData = 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);
        Dictionary<string, string> requestorNameToUser = new();
        Dictionary<string, string> assignedToNameToUser = new();
        Dictionary<string, string> assignedToNameToEncodedPAT = new();
        string cellInstanceNamed = string.Concat("CellInstance.", _EquipmentType);
        MediaTypeWithQualityHeaderValue mediaTypeWithQualityHeaderValue = new("application/json");
        _API = GetPropertyValue(cellInstanceConnectionName, modelObjectParameters, $"{cellInstanceNamed}.HttpClient.API");
        _Query = GetPropertyValue(cellInstanceConnectionName, modelObjectParameters, $"{cellInstanceNamed}.HttpClient.Query");
        string pat = GetPropertyValue(cellInstanceConnectionName, modelObjectParameters, $"{cellInstanceNamed}.HttpClient.PAT");
        string basePage = GetPropertyValue(cellInstanceConnectionName, modelObjectParameters, $"{cellInstanceNamed}.HttpClient.BasePage");
        string baseAddress = GetPropertyValue(cellInstanceConnectionName, modelObjectParameters, $"{cellInstanceNamed}.HttpClient.BaseAddress");
        byte[] bytes = Encoding.ASCII.GetBytes($":{pat}");
        string base64 = Convert.ToBase64String(bytes);
        _HttpClient = new(new HttpClientHandler() { UseDefaultCredentials = true }) { BaseAddress = new(baseAddress) };
        _HttpClient.DefaultRequestHeaders.Authorization = new("Basic", base64);
        _HttpClient.DefaultRequestHeaders.Accept.Add(mediaTypeWithQualityHeaderValue);
        _BasePage = basePage;
        if (_IsEAFHosted)
            NestExistingFiles(_FileConnectorConfiguration);
        if (!Debugger.IsAttached && fileConnectorConfiguration.PreProcessingMode != FileConnectorConfiguration.PreProcessingModeEnum.Process)
            _Timer = new Timer(Callback, null, (int)(fileConnectorConfiguration.FileScanningIntervalInSeconds * 1000), Timeout.Infinite);
        else
        {
            _Timer = new Timer(Callback, null, Timeout.Infinite, Timeout.Infinite);
            Callback(null);
        }
    }

    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) => throw new Exception(string.Concat("See ", nameof(Callback)));

#nullable enable

    private static string GetIds(HttpClient httpClient, string basePage, string api, string query)
    {
        List<int> results = new();
        StringBuilder result = new();
        Task<HttpResponseMessage> httpResponseMessageTask = httpClient.GetAsync(string.Concat(basePage, api, query));
        httpResponseMessageTask.Wait();
        if (!httpResponseMessageTask.Result.IsSuccessStatusCode)
            throw new Exception(httpResponseMessageTask.Result.StatusCode.ToString());
        Task<Stream> streamTask = httpResponseMessageTask.Result.Content.ReadAsStreamAsync();
        streamTask.Wait();
        if (!streamTask.Result.CanRead)
            throw new NullReferenceException(nameof(streamTask));
        JsonSerializerOptions jsonSerializerOptions = new() { PropertyNameCaseInsensitive = true };
        json.WIQL.Root? root = JsonSerializer.Deserialize<json.WIQL.Root>(streamTask.Result, jsonSerializerOptions);
        streamTask.Result.Dispose();
        if (root is null || root.WorkItems is null)
            throw new NullReferenceException(nameof(root));
        foreach (json.WIQL.WorkItem workItem in root.WorkItems)
        {
            results.Add(workItem.Id);
            if (results.Count > 199)
                break;
        }
        foreach (int id in results)
            _ = result.Append(id).Append(',');
        if (result.Length > 0)
            _ = result.Remove(result.Length - 1, 1);
        return result.ToString();
    }

    private static void DownloadWorkItemsAsync(HttpClient httpClient, string basePage, string api, string targetFileLocation, string ids, long ticks)
    {
        string json;
        string file;
        int i = 100000;
        JsonElement[] jsonElements;
        Task<HttpResponseMessage> httpResponseMessageTask = httpClient.GetAsync(string.Concat(basePage, api, $"/workitems?ids={ids}&$expand=Relations"));
        httpResponseMessageTask.Wait();
        if (!httpResponseMessageTask.Result.IsSuccessStatusCode)
            throw new Exception(httpResponseMessageTask.Result.StatusCode.ToString());
        Task<Stream> streamTask = httpResponseMessageTask.Result.Content.ReadAsStreamAsync();
        streamTask.Wait();
        if (!streamTask.Result.CanRead)
            throw new NullReferenceException(nameof(streamTask));
        JsonElement? result = JsonSerializer.Deserialize<JsonElement>(streamTask.Result);
        if (result is null || result.Value.ValueKind != JsonValueKind.Object)
            throw new NullReferenceException(nameof(result));
        JsonProperty[] jsonProperties = result.Value.EnumerateObject().ToArray();
        foreach (JsonProperty jsonProperty in jsonProperties)
        {
            if (jsonProperty.Value.ValueKind != JsonValueKind.Array)
                continue;
            jsonElements = jsonProperty.Value.EnumerateArray().ToArray();
            foreach (JsonElement jsonElement in jsonElements)
            {
                json = jsonElement.GetRawText();
                file = Path.Combine(targetFileLocation, $"{ticks}-{i}.json");
                File.WriteAllText(file, json);
                i += 1;
            }
        }
    }

    private static void DownloadWorkItemsAsync(HttpClient httpClient,
                                               string targetFileLocation,
                                               string basePage,
                                               string api,
                                               string query)
    {
        long tick = DateTime.Now.Ticks;
        string ids = GetIds(httpClient, basePage, api, query);
        if (!string.IsNullOrEmpty(ids))
            DownloadWorkItemsAsync(httpClient, basePage, api, targetFileLocation, ids, tick);
    }

    private void Callback(object state)
    {
        try
        {
            if (_IsEAFHosted)
                DownloadWorkItemsAsync(_HttpClient, _FileConnectorConfiguration.TargetFileLocation, _BasePage, _API, _Query);
        }
        catch (Exception exception)
        {
            string subject = string.Concat("Exception:", _CellInstanceConnectionName);
            string body = string.Concat(exception.Message, Environment.NewLine, Environment.NewLine, exception.StackTrace);
            try
            { _SMTP.SendHighPriorityEmailMessage(subject, body); }
            catch (Exception) { }
        }
        try
        {
            if (_FileConnectorConfiguration?.FileScanningIntervalInSeconds is null)
                throw new Exception();
            TimeSpan timeSpan = new(DateTime.Now.AddSeconds(_FileConnectorConfiguration.FileScanningIntervalInSeconds.Value).Ticks - DateTime.Now.Ticks);
            _ = _Timer.Change((long)timeSpan.TotalMilliseconds, Timeout.Infinite);
        }
        catch (Exception exception)
        {
            string subject = string.Concat("Exception:", _CellInstanceConnectionName);
            string body = string.Concat(exception.Message, Environment.NewLine, Environment.NewLine, exception.StackTrace);
            try
            { _SMTP.SendHighPriorityEmailMessage(subject, body); }
            catch (Exception) { }
        }
    }

}