using System.Collections.Generic;
using CommunityToolkit.Mvvm.ComponentModel;
using Metin2Launcher.Config;
using Metin2Launcher.Localization;

namespace Metin2Launcher.UI.ViewModels;

public sealed class LanguageOption
{
    public string Code { get; }
    public string Display { get; }
    public LanguageOption(string code, string display) { Code = code; Display = display; }
    public override string ToString() => Display;
}

public sealed partial class SettingsDialogViewModel : ObservableObject
{
    [ObservableProperty] private LanguageOption _selectedLanguage;
    [ObservableProperty] private string _manifestUrl;
    [ObservableProperty] private bool _devMode;

    public List<LanguageOption> Languages { get; }

    public string Title => Loc.Get("settings.title");
    public string LanguageLabel => Loc.Get("settings.language");
    public string ManifestUrlLabel => Loc.Get("settings.manifestUrl");
    public string DevModeLabel => Loc.Get("settings.devMode");
    public string NoteLabel => Loc.Get("settings.note");
    public string OkLabel => Loc.Get("button.ok");
    public string CancelLabel => Loc.Get("button.cancel");

    public SettingsDialogViewModel(LauncherSettings settings)
    {
        Languages = new List<LanguageOption>
        {
            new("cs", Loc.Get("settings.language.cs")),
            new("en", Loc.Get("settings.language.en")),
        };
        _selectedLanguage = Languages.Find(l => l.Code == settings.Locale) ?? Languages[0];
        _manifestUrl = settings.ManifestUrlOverride ?? LauncherConfig.ManifestUrl;
        _devMode = settings.DevMode;
    }

    public void ApplyTo(LauncherSettings settings)
    {
        settings.Locale = SelectedLanguage.Code;
        settings.DevMode = DevMode;
        settings.ManifestUrlOverride = ManifestUrl == LauncherConfig.ManifestUrl ? null : ManifestUrl;
    }
}
