using System.Diagnostics;
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Platform;

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) =>
        await OpenExternalUrlAsync(SourceCodeUrl);

    private async void OnOpenFfmpegLicenseClick(object? sender, RoutedEventArgs e) =>
        await OpenExternalUrlAsync(FfmpegLicenseUrl);

    private async void OnOpenFfprobeLicenseClick(object? sender, RoutedEventArgs e) =>
        await OpenExternalUrlAsync(FfprobeLicenseUrl);

    private void OnCloseClick(object? sender, RoutedEventArgs e) => Close();

    private async Task OpenExternalUrlAsync(string url)
    {
        try
        {
            ProcessStartInfo processStartInfo = new(url) { UseShellExecute = true };

            Process.Start(processStartInfo);
        }
        catch
        {
            await Task.CompletedTask;
        }
    }
}
