using System;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;
using CommunityToolkit.Mvvm.Messaging;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Media.Animation;
using Microsoft.UI.Xaml.Navigation;
using Microsoft.Web.WebView2.Core;
using Windows.System;
using Wino.Core.Domain;
using Wino.Core.Domain.Enums;
using Wino.Core.Domain.Interfaces;
using Wino.Core.Domain.Models.Printing;
using Wino.Mail.ViewModels.Data;
using Wino.Mail.WinUI;
using Wino.Mail.WinUI.Extensions;
using Wino.Messaging.Client.Mails;
using Wino.Messaging.Client.Shell;
using Wino.Views.Abstract;

namespace Wino.Views.Mail;

public sealed partial class MailRenderingPage : MailRenderingPageAbstract,
    IRecipient<HtmlRenderingRequested>,
    IRecipient<CancelRenderingContentRequested>,
    IRecipient<ApplicationThemeChanged>
{
    private readonly IPreferencesService _preferencesService = App.Current.Services.GetService<IPreferencesService>()!;
    private readonly IMailDialogService _dialogService = App.Current.Services.GetService<IMailDialogService>()!;

    private bool isRenderingInProgress = false;
    private bool? _lastAppliedDarkTheme;
    private TaskCompletionSource<bool> DOMLoadedTask = new TaskCompletionSource<bool>();

    public WebView2 GetWebView() => Chromium;

    public MailRenderingPage()
    {
        InitializeComponent();

        WebViewExtensions.EnsureWebView2Environment();

        ViewModel.DirectPrintFuncAsync = DirectPrintAsync;

        ViewModel.SaveHTMLasPDFFunc = new Func<string, Task<bool>>((path) =>
        {
            return Chromium.CoreWebView2.PrintToPdfAsync(path, null).AsTask();
        });
    }

    private async Task<PrintingResult> DirectPrintAsync(WebView2PrintSettingsModel settings)
    {
        if (Chromium.CoreWebView2 == null) return PrintingResult.Failed;

        try
        {
            var nativeSettings = settings.ToCoreWebView2PrintSettings(Chromium.CoreWebView2.Environment);
            var res = await Chromium.CoreWebView2.PrintAsync(nativeSettings);

            return res switch
            {
                CoreWebView2PrintStatus.Succeeded => PrintingResult.Submitted,
                _ => PrintingResult.Failed,
            };
        }
        catch (Exception)
        {
            return PrintingResult.Failed;
        }
    }

    public override async void OnEditorThemeChanged()
    {
        base.OnEditorThemeChanged();

        await UpdateEditorThemeAsync();
    }

    private async Task RenderInternalAsync(string htmlBody)
    {
        isRenderingInProgress = true;

        await DOMLoadedTask.Task;

        await UpdateEditorThemeAsync();
        await UpdateReaderFontPropertiesAsync();

        if (string.IsNullOrEmpty(htmlBody))
        {
            await Chromium.ExecuteScriptFunctionAsync("RenderHTML", JsonSerializer.Serialize(" ", BasicTypesJsonContext.Default.String));
        }
        else
        {
            var shouldLinkifyText = ViewModel.CurrentRenderModel?.MailRenderingOptions?.RenderPlaintextLinks ?? true;
            await Chromium.ExecuteScriptFunctionAsync("RenderHTML",
                JsonSerializer.Serialize(htmlBody, BasicTypesJsonContext.Default.String),
                JsonSerializer.Serialize(shouldLinkifyText, BasicTypesJsonContext.Default.Boolean));
        }

        isRenderingInProgress = false;
    }

    private async void WindowRequested(CoreWebView2 sender, CoreWebView2NewWindowRequestedEventArgs args)
    {
        args.Handled = true;

        try
        {
            await Launcher.LaunchUriAsync(new Uri(args.Uri));
        }
        catch (Exception) { }
    }

    private void DOMContentLoaded(CoreWebView2 sender, CoreWebView2DOMContentLoadedEventArgs args) => DOMLoadedTask.TrySetResult(true);

    async void IRecipient<HtmlRenderingRequested>.Receive(HtmlRenderingRequested message)
    {
        // Ensure WebView2 is fully initialized before first render.
        // OnNavigatedTo starts initialization fire-and-forget; this await
        // guarantees the core is ready before we invoke any script.
        await Chromium.EnsureCoreWebView2Async();

        if (message == null || string.IsNullOrEmpty(message.HtmlBody))
        {
            await RenderInternalAsync(string.Empty);
            return;
        }

        await RenderInternalAsync(message.HtmlBody);
    }

    protected override void OnNavigatedFrom(NavigationEventArgs e)
    {
        base.OnNavigatedFrom(e);

        // Disposing the page.
        // Make sure the WebView2 is disposed properly.

        ViewModel.SaveHTMLasPDFFunc = null;
        ViewModel.DirectPrintFuncAsync = null;

        DisposeWebView2();
    }

    private void DisposeWebView2()
    {
        if (Chromium == null) return;

        Chromium.CoreWebView2Initialized -= CoreWebViewInitialized;
        Chromium.NavigationStarting -= WebViewNavigationStarting;

        if (Chromium.CoreWebView2 != null)
        {
            Chromium.CoreWebView2.DOMContentLoaded -= DOMContentLoaded;
            Chromium.CoreWebView2.NewWindowRequested -= WindowRequested;
        }

        Chromium.Close();
        GC.Collect();
    }

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        // Initialize WebView2 wiring before base navigation invokes ViewModel rendering.
        // Base.OnNavigatedTo triggers VM.OnNavigatedTo, which can send HtmlRenderingRequested.
        DOMLoadedTask = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
        Chromium.CoreWebView2Initialized -= CoreWebViewInitialized;
        Chromium.CoreWebView2Initialized += CoreWebViewInitialized;
        _ = Chromium.EnsureCoreWebView2Async();

        base.OnNavigatedTo(e);

        var anim = ConnectedAnimationService.GetForCurrentView().GetAnimation("WebViewConnectedAnimation");
        anim?.TryStart(Chromium);

        // We don't have shell initialized here. It's only standalone EML viewing.
        // Shift command bar from top to adjust the design.

        if (ViewModel.StatePersistenceService.ShouldShiftMailRenderingDesign)
            RendererGridFrame.Margin = new Thickness(0, 24, 0, 0);
        else
            RendererGridFrame.Margin = new Thickness(0, 0, 0, 0);
    }

    private async void CoreWebViewInitialized(WebView2 sender, CoreWebView2InitializedEventArgs args)
    {
        if (Chromium.CoreWebView2 == null) return;

        var editorBundlePath = (await ViewModel.NativeAppService.GetEditorBundlePathAsync()).Replace("editor.html", string.Empty);

        Chromium.CoreWebView2.SetVirtualHostNameToFolderMapping("wino.mail", editorBundlePath, CoreWebView2HostResourceAccessKind.Allow);

        Chromium.CoreWebView2.DOMContentLoaded -= DOMContentLoaded;
        Chromium.CoreWebView2.DOMContentLoaded += DOMContentLoaded;

        Chromium.CoreWebView2.NewWindowRequested -= WindowRequested;
        Chromium.CoreWebView2.NewWindowRequested += WindowRequested;

        Chromium.Source = new Uri("https://wino.mail/reader.html");
    }


    async void IRecipient<CancelRenderingContentRequested>.Receive(CancelRenderingContentRequested message)
    {
        await Chromium.EnsureCoreWebView2Async();

        if (!isRenderingInProgress)
        {
            await RenderInternalAsync(string.Empty);
        }
    }

    private async void WebViewNavigationStarting(WebView2 sender, CoreWebView2NavigationStartingEventArgs args)
    {
        // This is our reader.
        if (args.Uri == "https://wino.mail/reader.html")
            return;

        // Cancel all external navigations since it's navigating to different address inside the WebView2.
        args.Cancel = !args.Uri.StartsWith("data:text/html");

        // TODO: Check external link navigation setting is enabled.
        // Open all external urls in launcher.

        if (args.Cancel && Uri.TryCreate(args.Uri, UriKind.Absolute, out Uri? newUri) && newUri != null)
        {
            await Launcher.LaunchUriAsync(newUri);
        }
    }

    private void AttachmentClicked(object sender, ItemClickEventArgs e)
    {
        if (e.ClickedItem is MailAttachmentViewModel attachmentViewModel)
        {
            ViewModel?.OpenAttachmentCommand.Execute(attachmentViewModel);
        }
    }

    private void BarDynamicOverflowChanging(CommandBar sender, DynamicOverflowItemsChangingEventArgs args)
    {
        if (args.Action == CommandBarDynamicOverflowAction.AddingToOverflow || sender.SecondaryCommands.Any())
            sender.OverflowButtonVisibility = CommandBarOverflowButtonVisibility.Visible;
        else
            sender.OverflowButtonVisibility = CommandBarOverflowButtonVisibility.Collapsed;
    }

    private async Task UpdateEditorThemeAsync()
    {
        await DOMLoadedTask.Task;

        var isDark = ViewModel.IsDarkWebviewRenderer;

        if (_lastAppliedDarkTheme == isDark) return;

        _lastAppliedDarkTheme = isDark;

        if (isDark)
        {
            Chromium.CoreWebView2.Profile.PreferredColorScheme = CoreWebView2PreferredColorScheme.Dark;

            await Chromium.ExecuteScriptSafeAsync("SetDarkEditor();");
        }
        else
        {
            Chromium.CoreWebView2.Profile.PreferredColorScheme = CoreWebView2PreferredColorScheme.Light;

            await Chromium.ExecuteScriptSafeAsync("SetLightEditor();");
        }
    }

    private async Task UpdateReaderFontPropertiesAsync()
    {
        await Chromium.ExecuteScriptFunctionAsync("ChangeFontSize", JsonSerializer.Serialize(_preferencesService.ReaderFontSize, BasicTypesJsonContext.Default.Int32));

        // Prepare font family name with fallback to sans-serif by default.
        var fontName = _preferencesService.ReaderFont;

        // If font family name is not supported by the browser, fallback to sans-serif.
        fontName += ", sans-serif";

        await Chromium.ExecuteScriptFunctionAsync("ChangeFontFamily", JsonSerializer.Serialize(fontName, BasicTypesJsonContext.Default.String));
    }

    void IRecipient<ApplicationThemeChanged>.Receive(ApplicationThemeChanged message)
    {
        ViewModel.IsDarkWebviewRenderer = message.IsUnderlyingThemeDark;
    }

    private void InternetAddressClicked(object sender, RoutedEventArgs e)
    {
        if (sender is HyperlinkButton hyperlinkButton)
        {
            hyperlinkButton.ContextFlyout.ShowAt(hyperlinkButton);
        }
    }

    private void CopyAddress_Click(object sender, RoutedEventArgs e)
    {
        if (sender is HyperlinkButton button && button.CommandParameter is string address)
        {
            ViewModel.CopyClipboardCommand.Execute(address);
        }
    }

    private void OpenAttachment_Click(object sender, RoutedEventArgs e)
    {
        if (sender is MenuFlyoutItem item && item.CommandParameter is MailAttachmentViewModel attachment)
        {
            ViewModel.OpenAttachmentCommand.Execute(attachment);
        }
    }

    private void SaveAttachment_Click(object sender, RoutedEventArgs e)
    {
        if (sender is MenuFlyoutItem item && item.CommandParameter is MailAttachmentViewModel attachment)
        {
            ViewModel.SaveAttachmentCommand.Execute(attachment);
        }
    }

    protected override void RegisterRecipients()
    {
        base.RegisterRecipients();

        WeakReferenceMessenger.Default.Register<HtmlRenderingRequested>(this);
        WeakReferenceMessenger.Default.Register<CancelRenderingContentRequested>(this);
        WeakReferenceMessenger.Default.Register<ApplicationThemeChanged>(this);
    }

    protected override void UnregisterRecipients()
    {
        base.UnregisterRecipients();

        WeakReferenceMessenger.Default.Unregister<HtmlRenderingRequested>(this);
        WeakReferenceMessenger.Default.Unregister<CancelRenderingContentRequested>(this);
        WeakReferenceMessenger.Default.Unregister<ApplicationThemeChanged>(this);
    }

    private void EscapeInvoked(Microsoft.UI.Xaml.Input.KeyboardAccelerator sender, Microsoft.UI.Xaml.Input.KeyboardAcceleratorInvokedEventArgs args)
    {
        WeakReferenceMessenger.Default.Send(new ClearMailSelectionsRequested());
    }
}
