using System.Reflection;

using AIStudio.Components;
using AIStudio.Dialogs;
using AIStudio.Settings.DataModel;
using AIStudio.Tools.Databases;
using AIStudio.Tools.Databases.VectorStore;
using AIStudio.Tools.Metadata;
using AIStudio.Tools.PluginSystem;
using AIStudio.Tools.Rust;
using AIStudio.Tools.Services;

using Microsoft.AspNetCore.Components;

using SharedTools;

using DialogOptions = AIStudio.Dialogs.DialogOptions;

namespace AIStudio.Pages;

public partial class Information : MSGComponentBase
{
    [Inject]
    private RustService RustService { get; init; } = null!;

    [Inject]
    private IDialogService DialogService { get; init; } = null!;

    [Inject]
    private UpdatePolicy UpdatePolicy { get; init; } = null!;

    [Inject]
    private RuntimeInfoResponse RuntimeInfo { get; init; }
    
    [Inject]
    private DatabaseClientProvider DatabaseClientProvider { get; init; } = null!;
    
    private static readonly Assembly ASSEMBLY = Assembly.GetExecutingAssembly();
    private static readonly MetaDataAttribute META_DATA = ASSEMBLY.GetCustomAttribute<MetaDataAttribute>()!;
    private static readonly MetaDataArchitectureAttribute META_DATA_ARCH = ASSEMBLY.GetCustomAttribute<MetaDataArchitectureAttribute>()!;
    private static readonly MetaDataLibrariesAttribute META_DATA_LIBRARIES = ASSEMBLY.GetCustomAttribute<MetaDataLibrariesAttribute>()!;
    private static readonly MetaDataVectorStoreAttribute META_DATA_VECTOR_STORE = ASSEMBLY.GetCustomAttribute<MetaDataVectorStoreAttribute>()!;
    
    private static string TB(string fallbackEN) => I18N.I.T(fallbackEN, typeof(Information).Namespace, nameof(Information));

    private string osLanguage = string.Empty;
    private string osUserName = string.Empty;
    private UpdatePolicyMode updatePolicyMode;
    
    private static string VersionApp => $"MindWork AI Studio: v{META_DATA.Version} (commit {META_DATA.AppCommitHash}, build {META_DATA.BuildNum}, {META_DATA_ARCH.Architecture.ToRID().ToUserFriendlyName()})";
    
    private static string MudBlazorVersion => $"MudBlazor: v{META_DATA.MudBlazorVersion}";
    
    private static string TauriVersion => $"Tauri: v{META_DATA.TauriVersion}";
    
    private string OSLanguage => $"{T("User-language provided by the OS")}: '{this.osLanguage}'";
    
    private string OSUserName => $"{T("Username provided by the OS")}: '{this.osUserName}'";

    private string WorkingDirectory => $"{T("Working directory")}: {this.RuntimeInfo.WorkingDirectory}";

    private string ExecutablePath => $"{T("Executable path")}: {this.RuntimeInfo.ExecutablePath}";

    private string LinuxPackageType => $"{T("Linux package")}: {this.LinuxPackageTypeDisplayName}";

    private string LinuxPackageTypeDisplayName => this.RuntimeInfo.LinuxPackageType switch
    {
        "appimage" => "AppImage",
        "flatpak" => "Flatpak",
        "unknown" => T("unknown"),
        _ => T("not applicable")
    };

    private string VersionRust => $"{T("Used Rust compiler")}: v{META_DATA.RustVersion}";
    
    private string VersionDotnetRuntime => $"{T("Used .NET runtime")}: v{META_DATA.DotnetVersion}";
    
    private string VersionDotnetSdk => $"{T("Used .NET SDK")}: v{META_DATA.DotnetSdkVersion}";
    
    private string BuildTime => $"{T("Build time")}: {META_DATA.BuildTime}";
    
    private string VersionPdfium => $"{T("Used PDFium version")}: v{META_DATA_LIBRARIES.PdfiumVersion}";
    
    private string VersionVectorStore
    {
        get
        {
            if (this.vectorStore is null)
                return $"{T("Vector store")}: {T("checking availability")}";

            return this.vectorStore.Status switch
            {
                DatabaseClientStatus.AVAILABLE => $"{T("Vector store version")}: {this.vectorStore.Name} v{META_DATA_VECTOR_STORE.VectorStoreVersion}",
                DatabaseClientStatus.STARTING => $"{T("Vector store")}: {this.vectorStore.Name} - {T("starting")}",
                _ => $"{T("Vector store")}: {this.vectorStore.Name} - {T("not available")}"
            };
        }
    }
    
    private string versionPandoc = TB("Determine Pandoc version, please wait...");
    private PandocInstallation pandocInstallation;

    private GetLogPathsResponse logPaths;
    
    private bool showEnterpriseConfigDetails;

    private bool showVectorStoreDetails;
    private bool showExternalHttpCustomRootCertificateDetails;

    private List<IAvailablePlugin> configPlugins = PluginFactory.AvailablePlugins
        .Where(x => x.Type is PluginType.CONFIGURATION)
        .OfType<IAvailablePlugin>()
        .ToList();

    private List<EnterpriseEnvironment> enterpriseEnvironments = EnterpriseEnvironmentService.CURRENT_ENVIRONMENTS.ToList();

    private List<MandatoryInfoPanelData> mandatoryInfoPanels = [];

    private sealed record MandatoryInfoPanelData(string HeaderText, string PluginName, DataMandatoryInfo Info, DataMandatoryInfoAcceptance? Acceptance);
    
    private sealed record VectorStoreDisplayInfo(string Label, string Value);
    private readonly List<VectorStoreDisplayInfo> vectorStoreDisplayInfo = new();
    private DatabaseClient? vectorStore;
    private CancellationTokenSource? vectorStoreRefreshCancellationTokenSource;

    private bool HasAnyActiveEnvironment => this.enterpriseEnvironments.Any(e => e.IsActive);
    
    private bool HasAnyLoadedEnterpriseConfigurationPlugin => this.enterpriseEnvironments
        .Where(e => e.IsActive)
        .Any(env => this.FindManagedConfigurationPlugin(env.ConfigurationId) is not null);

    /// <summary>
    /// Determines whether the enterprise configuration has details that can be shown/hidden.
    /// Returns true if there are details available, false otherwise.
    /// </summary>
    private bool HasEnterpriseConfigurationDetails
    {
        get
        {
            return this.HasAnyActiveEnvironment switch
            {
                // Case 1: No enterprise config and no plugin - no details available
                false when this.configPlugins.Count == 0 => false,

                // Case 2: Enterprise config with plugin but no central management - has details
                false => true,

                // Case 3: Enterprise config active but no plugin - has details
                true when this.configPlugins.Count == 0 => true,

                // Case 4: Enterprise config active with plugin - has details
                true => true
            };
        }
    }
    
    #region Overrides of ComponentBase
    
    protected override async Task OnInitializedAsync()
    {
        this.ApplyFilters([], [ Event.ENTERPRISE_ENVIRONMENTS_CHANGED, Event.CONFIGURATION_CHANGED ]);
        await base.OnInitializedAsync();

        this.RefreshEnterpriseConfigurationState();
        
        this.osLanguage = await this.RustService.ReadUserLanguage();
        this.osUserName = await this.RustService.ReadUserName();
        this.updatePolicyMode = this.UpdatePolicy.CurrentMode;
        this.logPaths = await this.RustService.GetLogPaths();
        
        await this.RefreshVectorStoreInfo(CancellationToken.None);
        if (this.vectorStore?.Status is DatabaseClientStatus.STARTING)
            this.StartShortVectorStoreRefreshLoop();
        
        // Determine the Pandoc version may take some time, so we start it here
        // without waiting for the result:
        _ = this.DeterminePandocVersion();
    }

    #endregion

    #region Overrides of MSGComponentBase

    protected override async Task ProcessIncomingMessage<T>(ComponentBase? sendingComponent, Event triggeredEvent, T? data) where T : default
    {
        switch (triggeredEvent)
        {
            case Event.PLUGINS_RELOADED:
            case Event.ENTERPRISE_ENVIRONMENTS_CHANGED:
            case Event.CONFIGURATION_CHANGED:
                this.updatePolicyMode = this.UpdatePolicy.CurrentMode;
                this.RefreshEnterpriseConfigurationState();
                await this.InvokeAsync(this.StateHasChanged);
                break;
        }
        
        await base.ProcessIncomingMessage(sendingComponent, triggeredEvent, data);
    }

    #endregion

    private void RefreshEnterpriseConfigurationState()
    {
        this.configPlugins = PluginFactory.AvailablePlugins
            .Where(x => x.Type is PluginType.CONFIGURATION)
            .OfType<IAvailablePlugin>()
            .ToList();

        this.enterpriseEnvironments = EnterpriseEnvironmentService.CURRENT_ENVIRONMENTS.ToList();
        this.mandatoryInfoPanels = PluginFactory.GetMandatoryInfos()
            .Select(info =>
            {
                var plugin = this.configPlugins.FirstOrDefault(item => item.Id == info.EnterpriseConfigurationPluginId);
                var pluginName = plugin?.Name ?? T("Unknown configuration plugin");
                var acceptance = this.SettingsManager.ConfigurationData.MandatoryInformation.FindAcceptance(info.Id);
                var headerText = $"{T("Consent:")} {info.Title}";
                return new MandatoryInfoPanelData(headerText, pluginName, info, acceptance);
            })
            .ToList();
    }

    private async Task DeterminePandocVersion()
    {
        this.pandocInstallation = await Pandoc.CheckAvailabilityAsync(this.RustService, false);
        var pandocInstallationType = this.pandocInstallation.IsLocalInstallation 
            ? T("installed by AI Studio")
            : T("installation provided by the system");
        
        switch (this.pandocInstallation)
        {
            case { CheckWasSuccessful: true, IsAvailable: true }:
                this.versionPandoc = $"{this.T("Installed Pandoc version")}: v{this.pandocInstallation.Version} ({pandocInstallationType}) - {T("OK")}";
                break;
            
            case { CheckWasSuccessful: true, IsAvailable: false }:
                this.versionPandoc = $"{this.T("Installed Pandoc version")}: v{this.pandocInstallation.Version} ({pandocInstallationType}) - {T("this version does not met the requirements")}";
                break;
            
            default:
                this.versionPandoc = TB("Installed Pandoc version: Pandoc is not installed or not available.");
                break;
        }
        
        await this.InvokeAsync(this.StateHasChanged);
    }
    
    private string PandocButtonText
    {
        get
        {
            return this.pandocInstallation switch
            {
                { IsAvailable: true, CheckWasSuccessful: true } => this.T("Check Pandoc Installation"),
                { IsAvailable: false, CheckWasSuccessful: true } => this.T("Update Pandoc"),
                
                _ => this.T("Install Pandoc")
            };
        }
    }

    private async Task ShowPandocDialog()
    {
        var dialogReference = await this.DialogService.ShowAsync<PandocDialog>(T("Pandoc Installation"), DialogOptions.FULLSCREEN);
        await dialogReference.Result;
        await this.DeterminePandocVersion();
    }
    
    private void ToggleEnterpriseConfigDetails()
    {
        this.showEnterpriseConfigDetails = !this.showEnterpriseConfigDetails;
    }

    private void ToggleExternalHttpCustomRootCertificateDetails()
    {
        this.showExternalHttpCustomRootCertificateDetails = !this.showExternalHttpCustomRootCertificateDetails;
    }
    
    private void ToggleVectorStoreDetails()
    {
        this.showVectorStoreDetails = !this.showVectorStoreDetails;
    }

    private async Task RefreshVectorStoreInfo(CancellationToken cancellationToken)
    {
        var refreshedClient = await this.DatabaseClientProvider.RefreshClientAsync(DatabaseRole.VECTOR_STORE, cancellationToken);
        this.vectorStore = refreshedClient;
        this.vectorStoreDisplayInfo.Clear();

        try
        {
            await foreach (var (label, value) in refreshedClient.GetDisplayInfo().WithCancellation(cancellationToken))
            {
                this.vectorStoreDisplayInfo.Add(new VectorStoreDisplayInfo(label, value));
            }
        }
        catch (OperationCanceledException)
        {
            throw;
        }
        catch (Exception e)
        {
            this.vectorStore = new NoVectorStoreClient(refreshedClient.Name, e.Message, DatabaseClientStatus.STARTING);
            await foreach (var (label, value) in this.vectorStore.GetDisplayInfo().WithCancellation(cancellationToken))
            {
                this.vectorStoreDisplayInfo.Add(new VectorStoreDisplayInfo(label, value));
            }
        }
    }

    private void StartShortVectorStoreRefreshLoop()
    {
        this.vectorStoreRefreshCancellationTokenSource?.Cancel();
        this.vectorStoreRefreshCancellationTokenSource?.Dispose();
        this.vectorStoreRefreshCancellationTokenSource = new CancellationTokenSource();
        var cancellationToken = this.vectorStoreRefreshCancellationTokenSource.Token;

        _ = Task.Run(async () =>
        {
            const int MAX_TRIES = 12;
            for (var attempt = 0; attempt < MAX_TRIES; attempt++)
            {
                try
                {
                    await Task.Delay(TimeSpan.FromSeconds(1), cancellationToken);
                    await this.InvokeAsync(async () =>
                    {
                        await this.RefreshVectorStoreInfo(cancellationToken);
                        this.StateHasChanged();
                    });

                    if (this.vectorStore?.Status is not DatabaseClientStatus.STARTING)
                        return;
                }
                catch (OperationCanceledException)
                {
                    return;
                }
                catch
                {
                    return;
                }
            }
        }, cancellationToken);
    }

    private IAvailablePlugin? FindManagedConfigurationPlugin(Guid configurationId)
    {
        return this.configPlugins.FirstOrDefault(plugin => plugin.ManagedConfigurationId == configurationId)
               // Backward compatibility for already downloaded plugins without ManagedConfigurationId.
               ?? this.configPlugins.FirstOrDefault(plugin => plugin.ManagedConfigurationId is null && plugin.Id == configurationId);
    }

    private IReadOnlyList<ConfigInfoRowItem> BuildEnterpriseConfigurationItems(EnterpriseEnvironment environment, IAvailablePlugin? plugin = null)
    {
        var items = new List<ConfigInfoRowItem>
        {
            new(Icons.Material.Filled.ArrowRightAlt,
                $"{T("Enterprise configuration ID:")} {environment.ConfigurationId}",
                environment.ConfigurationId.ToString(),
                T("Copies the config ID to the clipboard")),

            new(Icons.Material.Filled.ArrowRightAlt,
                $"{T("Configuration server:")} {environment.ConfigurationServerUrl}",
                environment.ConfigurationServerUrl,
                T("Copies the server URL to the clipboard"),
                "margin-top: 4px;"),

            new(Icons.Material.Filled.ArrowRightAlt,
                $"{T("Configuration source:")} {environment.Source}",
                environment.Source,
                T("Copies the configuration source to the clipboard"),
                "margin-top: 4px;"),
        };

        if (!string.IsNullOrWhiteSpace(environment.SourceDetail))
        {
            items.Add(new ConfigInfoRowItem(Icons.Material.Filled.ArrowRightAlt,
                $"{T("Configuration origin:")} {environment.SourceDetail}",
                environment.SourceDetail,
                T("Copies the configuration origin to the clipboard"),
                "margin-top: 4px;"));
        }

        items.Add(new ConfigInfoRowItem(Icons.Material.Filled.ArrowRightAlt,
            $"{T("Configuration slot:")} {environment.Slot}",
            environment.Slot,
            T("Copies the configuration slot to the clipboard"),
            "margin-top: 4px;"));

        if (plugin is not null)
        {
            items.Add(new ConfigInfoRowItem(Icons.Material.Filled.ArrowRightAlt,
                $"{T("Configuration plugin ID:")} {plugin.Id}",
                plugin.Id.ToString(),
                T("Copies the configuration plugin ID to the clipboard"),
                "margin-top: 4px;"));
        }

        return items;
    }

    private bool IsManagedConfigurationIdMismatch(IAvailablePlugin plugin, Guid configurationId)
    {
        return plugin.ManagedConfigurationId == configurationId && plugin.Id != configurationId;
    }

    private string ExternalHttpCustomRootCertificateWarningText
    {
        get
        {
            var state = ExternalHttpClientTimeout.CustomRootCertificateState;
            return string.IsNullOrWhiteSpace(state.Issue)
                ? T("The configured root certificates could not be used.")
                : state.Issue;
        }
    }

    private IReadOnlyList<ConfigInfoRowItem> BuildExternalHttpCustomRootCertificateItems()
    {
        var state = ExternalHttpClientTimeout.CustomRootCertificateState;
        var items = new List<ConfigInfoRowItem>
        {
            new(Icons.Material.Filled.ArrowRightAlt,
                $"{T("Status:")} {(state.IsUsable ? T("active") : T("not active"))}",
                state.IsUsable ? T("active") : T("not active"),
                T("Copies the status to the clipboard")),

            new(Icons.Material.Filled.ArrowRightAlt,
                $"{T("Configuration source:")} {state.Source}",
                state.Source,
                T("Copies the configuration source to the clipboard"),
                "margin-top: 4px;"),

            new(Icons.Material.Filled.ArrowRightAlt,
                $"{T("Certificate bundle:")} {state.BundlePath}",
                state.BundlePath,
                T("Copies the certificate bundle path to the clipboard"),
                "margin-top: 4px;"),

            new(Icons.Material.Filled.ArrowRightAlt,
                $"{T("Loaded root certificates:")} {state.CertificateCount}",
                state.CertificateCount.ToString(),
                T("Copies the number of loaded root certificates to the clipboard"),
                "margin-top: 4px;")
        };

        if (state.AllowedHostPatterns.Count == 0)
        {
            items.Add(new ConfigInfoRowItem(Icons.Material.Filled.ArrowRightAlt,
                T("Allowed hosts: none configured"),
                string.Empty,
                T("Copies the allowed host configuration to the clipboard"),
                "margin-top: 4px;"));
        }
        else
        {
            foreach (var allowedHostPattern in state.AllowedHostPatterns)
            {
                items.Add(new ConfigInfoRowItem(Icons.Material.Filled.Dns,
                    $"{T("Allowed host:")} {allowedHostPattern}",
                    allowedHostPattern,
                    T("Copies the allowed host pattern to the clipboard"),
                    "margin-top: 4px;"));
            }
        }

        foreach (var fingerprint in state.CertificateFingerprints)
        {
            items.Add(new ConfigInfoRowItem(Icons.Material.Filled.Fingerprint,
                $"{T("Root certificate fingerprint:")} {fingerprint}",
                fingerprint,
                T("Copies the root certificate fingerprint to the clipboard"),
                "margin-top: 4px;"));
        }

        return items;
    }

    protected override void DisposeResources()
    {
        this.vectorStoreRefreshCancellationTokenSource?.Cancel();
        this.vectorStoreRefreshCancellationTokenSource?.Dispose();
        base.DisposeResources();
    }

    private async Task CopyStartupLogPath()
    {
        await this.RustService.CopyText2Clipboard(this.logPaths.LogStartupPath);
    }
    
    private async Task CopyAppLogPath()
    {
        await this.RustService.CopyText2Clipboard(this.logPaths.LogAppPath);
    }
    
    private const string LICENSE = """
        # Functional Source License, Version 1.1, MIT Future License

        ## Abbreviation

        FSL-1.1-MIT

        ## Notice

        Copyright 2026 Thorsten Sommer

        ## Terms and Conditions

        ### Licensor ("We")

        The party offering the Software under these Terms and Conditions.

        ### The Software

        The "Software" is each version of the software that we make available under
        these Terms and Conditions, as indicated by our inclusion of these Terms and
        Conditions with the Software.

        ### License Grant

        Subject to your compliance with this License Grant and the Patents,
        Redistribution and Trademark clauses below, we hereby grant you the right to
        use, copy, modify, create derivative works, publicly perform, publicly display
        and redistribute the Software for any Permitted Purpose identified below.

        ### Permitted Purpose

        A Permitted Purpose is any purpose other than a Competing Use. A Competing Use
        means making the Software available to others in a commercial product or
        service that:

        1. substitutes for the Software;

        2. substitutes for any other product or service we offer using the Software
           that exists as of the date we make the Software available; or

        3. offers the same or substantially similar functionality as the Software.

        Permitted Purposes specifically include using the Software:

        1. for your internal use and access;

        2. for non-commercial education;

        3. for non-commercial research; and

        4. in connection with professional services that you provide to a licensee
           using the Software in accordance with these Terms and Conditions.

        ### Patents

        To the extent your use for a Permitted Purpose would necessarily infringe our
        patents, the license grant above includes a license under our patents. If you
        make a claim against any party that the Software infringes or contributes to
        the infringement of any patent, then your patent license to the Software ends
        immediately.

        ### Redistribution

        The Terms and Conditions apply to all copies, modifications and derivatives of
        the Software.

        If you redistribute any copies, modifications or derivatives of the Software,
        you must include a copy of or a link to these Terms and Conditions and not
        remove any copyright notices provided in or with the Software.

        ### Disclaimer

        THE SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTIES OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR
        PURPOSE, MERCHANTABILITY, TITLE OR NON-INFRINGEMENT.

        IN NO EVENT WILL WE HAVE ANY LIABILITY TO YOU ARISING OUT OF OR RELATED TO THE
        SOFTWARE, INCLUDING INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES,
        EVEN IF WE HAVE BEEN INFORMED OF THEIR POSSIBILITY IN ADVANCE.

        ### Trademarks

        Except for displaying the License Details and identifying us as the origin of
        the Software, you have no right under these Terms and Conditions to use our
        trademarks, trade names, service marks or product names.

        ## Grant of Future License

        We hereby irrevocably grant you an additional license to use the Software under
        the MIT license that is effective on the second anniversary of the date we make
        the Software available. On or after that date, you may use the Software under
        the MIT license, in which case the following will apply:

        Permission is hereby granted, free of charge, to any person obtaining a copy of
        this software and associated documentation files (the "Software"), to deal in
        the Software without restriction, including without limitation the rights to
        use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
        of the Software, and to permit persons to whom the Software is furnished to do
        so, subject to the following conditions:

        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.

        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
        """;
    
    private async Task CheckForUpdate()
    {
        this.updatePolicyMode = this.UpdatePolicy.CurrentMode;
        if (this.updatePolicyMode is UpdatePolicyMode.SELF_UPDATE)
        {
            await this.MessageBus.SendMessage<bool>(this, Event.USER_SEARCH_FOR_UPDATE);
            return;
        }

        var parameters = new DialogParameters<UpdateInstructionsDialog>();
        if (this.updatePolicyMode is UpdatePolicyMode.ENTERPRISE_DISABLED)
        {
            parameters.Add(x => x.Message, T("Updates are managed by your organization. Contact your IT department if you have questions about updating AI Studio."));
        }
        else if (this.updatePolicyMode is UpdatePolicyMode.FLATPAK)
        {
            parameters.Add(x => x.Message, T("AI Studio cannot update itself when installed as a Flatpak. A Flathub listing is planned. Until then, you can find the latest release on GitHub."));
            parameters.Add(x => x.ReleaseUrl, "https://github.com/MindWorkAI/AI-Studio/releases/latest");
        }
        else
            return;

        await this.DialogService.ShowAsync<UpdateInstructionsDialog>(T("How to update"), parameters, DialogOptions.FULLSCREEN);
    }
}
