using CommunityToolkit.Mvvm.Input;
using FLY.AppHelper;
using FLY.Thick.Base.UI.OnInit;
using Misc;
using MultiLayout.UiModule;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using Unity;

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

        public UcSectionReboot()
        {
            InitializeComponent();
        }
        [InjectionMethod]
        public void Init(ParamDictionary paramDictionary,[Dependency("reboot")]IOnInit onInitReboot)
        {
            viewModel = new UcSectionRebootVm();
            viewModel.Init(paramDictionary, (OnInitReboot)onInitReboot);
            this.DataContext = viewModel;
        }
    }
    public class UcSectionRebootVm : INotifyPropertyChanged
    {

        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// 自动重启电脑
        /// </summary>
        public bool IsAutoReboot { get; set; }

        /// <summary>
        /// 开机时间
        /// </summary>
        public int BootTimeMin { get; set; }
        public bool IsReBootAtOnce { get; set; }
        ParamDictionary paramDictionary;
        OnInitReboot onInitReboot;

        public UcSectionRebootVm()
        {

        }

        public void Init(ParamDictionary paramDictionary, OnInitReboot onInitReboot)
        {
            this.paramDictionary = paramDictionary;
            this.onInitReboot = onInitReboot;
            IsAutoReboot = paramDictionary.GetValue(ParamDistItemKeys.AutoReboot, false);
            BootTimeMin = onInitReboot.BootTimeMin;
            IsReBootAtOnce = onInitReboot.IsReBootAtOnce;
            this.PropertyChanged += UcSectionRebootVm_PropertyChanged;
            onInitReboot.PropertyChanged += OnInitReboot_PropertyChanged;
        }

        private void OnInitReboot_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == nameof(onInitReboot.BootTimeMin)) 
            {
                BootTimeMin = onInitReboot.BootTimeMin;
            }
        }

        private void UcSectionRebootVm_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == nameof(IsAutoReboot))
            {
                paramDictionary.SetValue(ParamDistItemKeys.AutoReboot, IsAutoReboot);
            }
            else if (e.PropertyName == nameof(IsReBootAtOnce)) {
                onInitReboot.IsReBootAtOnce = IsReBootAtOnce;
            }
        }
    }
}
