using System.Windows;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using WPFBeginner.Services;
using WPFBeginner.Views;

namespace WPFBeginner.ViewModels;

public partial class LoginViewModel : ObservableObject
{
    private readonly ISettingsService _settingsService;

    [ObservableProperty]
    private string _userId = string.Empty;

    [ObservableProperty]
    private string _password = string.Empty;

    [ObservableProperty]
    private string _errorMessage = string.Empty;

    [ObservableProperty]
    private bool _hasError;

    public string WelcomeMessage => _settingsService.CurrentLanguage == "en-US"
        ? "Welcome"
        : "환영합니다";

    public LoginViewModel(ISettingsService settingsService)
    {
        _settingsService = settingsService;
    }

    [RelayCommand]
    private void Login(Window window)
    {
        var settings = _settingsService.Settings.Login;

        if (UserId == settings.User && Password == settings.Password)
        {
            HasError = false;
            ErrorMessage = string.Empty;

            var mainWindow = new MainWindow();
            mainWindow.Show();
            window.Close();
        }
        else
        {
            HasError = true;
            ErrorMessage = _settingsService.CurrentLanguage == "en-US"
                ? "Invalid ID or Password"
                : "아이디 또는 비밀번호가 틀렸습니다";
        }
    }

    [RelayCommand]
    private void Close(Window window)
    {
        Application.Current.Shutdown();
    }
}
