using CommunityToolkit.Mvvm.Input;
using FLY.AppHelper;
using Misc;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;

namespace FLY.Thick.Base.UI.CustomSection
{
    /// <summary>
    /// UcSectionOsk.xaml 的交互逻辑
    /// </summary>
    public partial class UcSectionLanguage : UserControl
    {
        UcSectionLanguageVm viewModel;

        public UcSectionLanguage()
        {
            InitializeComponent();
        }
        [Unity.InjectionMethod]
        public void Init(Culture culture)
        {
            viewModel = new UcSectionLanguageVm();
            viewModel.Init(culture);
            this.DataContext = viewModel;
        }
    }
    public class UcSectionLanguageVm : INotifyPropertyChanged
    {

        public event PropertyChangedEventHandler PropertyChanged;


        public LanguageInfo CurrentLanguage { get; set; }
        public ObservableCollection<LanguageInfo> LanguageList { get; } = new ObservableCollection<LanguageInfo>();
        public RelayCommand OkCmd { get; private set; }

        Culture culture;
        public UcSectionLanguageVm()
        {
            OkCmd = new RelayCommand(Ok);

            LanguageList.Add(new LanguageInfo() { Info = "中文", Language = "ch" });
            LanguageList.Add(new LanguageInfo() { Info = "English", Language = "en" });
        }

        public void Init(Culture culture)
        {
            this.culture = culture;
            try
            {
                CurrentLanguage = LanguageList.First(l => l.Language == culture.Language);
            }
            catch
            {
                CurrentLanguage = LanguageList[0];
            }
        }
        private void Ok()
        {
            string msg = (string)Application.Current.FindResource("str.PgCustomSections.IsSureToReboot");

            if (MessageBox.Show(msg, null, MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.No)
            {
                return;
            }

            if (culture.Language != CurrentLanguage.Language)
            {
                culture.Language = CurrentLanguage.Language;
                culture.Save();
            }

            AppJustOne.Restart();
        }
    }

    public class LanguageInfo : INotifyPropertyChanged
    {
        public string Language { get; set; }
        public string Info { get; set; }

        public event PropertyChangedEventHandler PropertyChanged;
    }
}
