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

namespace WPFBeginner.ViewModels;

public partial class MainViewModel : ObservableObject
{
    private readonly ISettingsService _settingsService;
    private readonly IMemberService _memberService;

    [ObservableProperty]
    private string _searchKeyword = string.Empty;

    [ObservableProperty]
    private Member? _selectedMember;

    [ObservableProperty]
    private ObservableCollection<Member> _filteredMembers = new();

    [ObservableProperty]
    private bool _isKorean = true;

    [ObservableProperty]
    private bool _isEnglish;

    [ObservableProperty]
    private bool _isDefaultTheme = true;

    [ObservableProperty]
    private bool _isLightTheme;

    [ObservableProperty]
    private string _searchResultMessage = string.Empty;

    [ObservableProperty]
    private bool _hasSearchResult = true;

    public MainViewModel(ISettingsService settingsService, IMemberService memberService)
    {
        _settingsService = settingsService;
        _memberService = memberService;

        // Load default members
        _memberService.LoadDefaultMembers(_settingsService.Settings.DefaultMembers);

        // Set initial language/theme based on settings
        IsKorean = _settingsService.CurrentLanguage == "ko-KR";
        IsEnglish = _settingsService.CurrentLanguage == "en-US";
        IsDefaultTheme = _settingsService.CurrentTheme == "DefaultTheme";
        IsLightTheme = _settingsService.CurrentTheme == "LightTheme";

        // Initialize filtered members
        RefreshFilteredMembers();
    }

    private void RefreshFilteredMembers()
    {
        FilteredMembers = new ObservableCollection<Member>(_memberService.Members);
    }

    [RelayCommand]
    private void Search()
    {
        var results = _memberService.Search(SearchKeyword).ToList();
        FilteredMembers = new ObservableCollection<Member>(results);

        if (results.Count == 0)
        {
            HasSearchResult = false;
            SearchResultMessage = _settingsService.CurrentLanguage == "en-US"
                ? "No results found"
                : "검색 결과가 없습니다";

            MessageBox.Show(SearchResultMessage,
                _settingsService.CurrentLanguage == "en-US" ? "Search" : "검색",
                MessageBoxButton.OK,
                MessageBoxImage.Information);
        }
        else
        {
            HasSearchResult = true;
            SearchResultMessage = string.Empty;
        }
    }

    [RelayCommand]
    private void AddMember()
    {
        var registWindow = new RegistWindow();
        registWindow.MemberAdded += OnMemberAdded;
        registWindow.ShowDialog();
    }

    private void OnMemberAdded(object? sender, Member member)
    {
        _memberService.Add(member);
        RefreshFilteredMembers();
    }

    [RelayCommand]
    private void DeleteMember()
    {
        if (SelectedMember != null)
        {
            _memberService.Remove(SelectedMember);
            RefreshFilteredMembers();
            SelectedMember = null;
        }
    }

    [RelayCommand]
    private void Reset()
    {
        SearchKeyword = string.Empty;
        _memberService.Reset();
        RefreshFilteredMembers();
        HasSearchResult = true;
        SearchResultMessage = string.Empty;
    }

    [RelayCommand]
    private void SetKorean()
    {
        IsKorean = true;
        IsEnglish = false;
        _settingsService.SetLanguage("ko-KR");
        OnPropertyChanged(nameof(IsKorean));
        OnPropertyChanged(nameof(IsEnglish));
    }

    [RelayCommand]
    private void SetEnglish()
    {
        IsKorean = false;
        IsEnglish = true;
        _settingsService.SetLanguage("en-US");
        OnPropertyChanged(nameof(IsKorean));
        OnPropertyChanged(nameof(IsEnglish));
    }

    [RelayCommand]
    private void SetDefaultTheme()
    {
        IsDefaultTheme = true;
        IsLightTheme = false;
        _settingsService.SetTheme("DefaultTheme");
        OnPropertyChanged(nameof(IsDefaultTheme));
        OnPropertyChanged(nameof(IsLightTheme));
    }

    [RelayCommand]
    private void SetLightTheme()
    {
        IsDefaultTheme = false;
        IsLightTheme = true;
        _settingsService.SetTheme("LightTheme");
        OnPropertyChanged(nameof(IsDefaultTheme));
        OnPropertyChanged(nameof(IsLightTheme));
    }
}
