using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Platform;
using OF_DL.Gui.Helpers;
using Serilog;

namespace OF_DL.Gui.Views;

public partial class AboutWindow : Window
{
    private const string UnknownValue = "Unknown";
    private const string UnknownToolVersion = "Not detected";

    public string ProgramVersion { get; }
    public string FfmpegVersion { get; }
    public string FfprobeVersion { get; }

    public string SourceCodeUrl { get; } = "https://git.ofdl.tools/sim0n00ps/OF-DL";
    public string FfmpegLicenseUrl { get; } = "https://ffmpeg.org/legal.html";
    public string FfprobeLicenseUrl { get; } = "https://ffmpeg.org/legal.html";

    public AboutWindow()
        : this(UnknownValue, UnknownToolVersion, UnknownToolVersion)
    {
    }

    public AboutWindow(
        string programVersion,
        string ffmpegVersion,
        string ffprobeVersion)
    {
        ProgramVersion = programVersion;
        FfmpegVersion = ffmpegVersion;
        FfprobeVersion = ffprobeVersion;

        InitializeComponent();
        Icon = new WindowIcon(AssetLoader.Open(new Uri("avares://OF DL.Gui/Assets/icon.ico")));
        DataContext = this;
    }

    private async void OnOpenSourceCodeClick(object? sender, RoutedEventArgs e)
    {
        try
        {
            await WebLinkHelper.OpenOrCopyAsync(this, SourceCodeUrl, sender as Control);
        }
        catch (Exception exception)
        {
            Log.Error(exception, "Failed to handle link click event. {ErrorMessage}", exception.Message);
        }
    }

    private async void OnOpenFfmpegLicenseClick(object? sender, RoutedEventArgs e)
    {
        try
        {
            await WebLinkHelper.OpenOrCopyAsync(this, FfmpegLicenseUrl, sender as Control);
        }
        catch (Exception exception)
        {
            Log.Error(exception, "Failed to handle link click event. {ErrorMessage}", exception.Message);
        }
    }

    private async void OnOpenFfprobeLicenseClick(object? sender, RoutedEventArgs e)
    {
        try
        {
            await WebLinkHelper.OpenOrCopyAsync(this, FfprobeLicenseUrl, sender as Control);
        }
        catch (Exception exception)
        {
            Log.Error(exception, "Failed to handle link click event. {ErrorMessage}", exception.Message);
        }
    }
}
