using System.Windows;
using PSLauncher.Models;

namespace PSLauncher.App.Views;

public partial class LauncherUpdateDialog : Window
{
    public bool UpdateRequested { get; private set; }

    public LauncherUpdateDialog(LauncherInfo info, string currentVersion)
    {
        InitializeComponent();
        VersionText.Text = $"v{currentVersion} → v{info.Version}";
        SizeText.Text = info.Download.SizeBytes > 0
            ? $"Taille : {FormatSize(info.Download.SizeBytes)}"
            : "";
    }

    private void OnUpdate(object sender, RoutedEventArgs e)
    {
        UpdateRequested = true;
        DialogResult = true;
        Close();
    }

    private void OnLater(object sender, RoutedEventArgs e)
    {
        UpdateRequested = false;
        DialogResult = false;
        Close();
    }

    private static string FormatSize(long bytes)
    {
        if (bytes <= 0) return "—";
        string[] units = { "o", "Ko", "Mo", "Go" };
        double v = bytes; int u = 0;
        while (v >= 1024 && u < units.Length - 1) { v /= 1024; u++; }
        return $"{v:0.#} {units[u]}";
    }
}
