using PDF_Office.Model.Dialog.HomePageToolsDialogs.HomePagePrinter;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Management.Instrumentation;

namespace PDF_Office.ViewModels.Dialog.HomePageToolsDialogs.HomePagePrinter
{
    public class HomePagePrinterModSizeContentViewModel : BindableBase, INavigationAware
    {
        private IEventAggregator eventAggregator;

        private bool _isStatusAdaptive = true;
        public bool IsStatusAdaptive
        {
            get { return _isStatusAdaptive; }
            set
            {
                SetProperty(ref _isStatusAdaptive, value);
            }
        }

        private bool _isStatusActural = false;
        public bool IsStatusActural
        {
            get { return _isStatusActural; }
            set
            {
                SetProperty(ref _isStatusActural, value);
            }
        }
        
        private bool _isStatusCustomized = false;
        public bool IsStatusCustomized
        {
            get { return _isStatusCustomized; }
            set { SetProperty(ref _isStatusCustomized, value); }
        }

        private string _displayRatio = "100";
        public string DisplayRatio
        {
            get { return _displayRatio; }
            set { SetProperty(ref _displayRatio, value); }
        }

        private SizeInfo _sizeInfo;
        public SizeInfo SizeInfo
        {
            get { return _sizeInfo; }
            set { _sizeInfo = value; }
        }

        public DelegateCommand SetStatusAdaptiveCommand { get; set; }
        public DelegateCommand SetStatusActuralCommand { get; set; }
        public DelegateCommand SetStatusCustomizedCommand { get; set; }
        public DelegateCommand SetDisplayRatioCommand { get; set; }

        //public DelegateCommand sendSizeInfoCommand;
        //public DelegateCommand SendSizeInfoCommand => this.SendSizeInfoCommand ?? (this.sendSizeInfoCommand = new DelegateCommand(() => this.eventAggregator.GetEvent<ModInfoSendEvent>().Publish(PrintMod)));

        public DelegateCommand SendSizeInfoCommand { get; set; }

        public HomePagePrinterModSizeContentViewModel(IEventAggregator eventAggregator)
        {
            this.eventAggregator = eventAggregator;
            SizeInfo = new SizeInfo();
            SetStatusAdaptiveCommand = new DelegateCommand(SetStatusAdaptive);
            SetStatusActuralCommand = new DelegateCommand(SetStatusActural);
            SetStatusCustomizedCommand = new DelegateCommand(SetStatusCustomized);
            SetDisplayRatioCommand = new DelegateCommand(SetDisplayRatio);
        }

        public void SendSizeInfo()
        {
            if(SizeInfo!= null)
            {
                this.eventAggregator.GetEvent<ModInfoSendEvent>().Publish(SizeInfo);
            }
        }

        public void SetStatusActural()
        {
            IsStatusActural = true;
            IsStatusAdaptive = false;
            IsStatusCustomized = false;

            SizeInfo.EnumSizeType = EnumSizeType.StatusActural;

            SendSizeInfo();
        }

        public void SetStatusAdaptive()
        {
            IsStatusActural = false;
            IsStatusAdaptive = true;
            IsStatusCustomized = false;

            SizeInfo.EnumSizeType = EnumSizeType.StatusAdaptive;
            SendSizeInfo();
        }

        public void SetStatusCustomized()
        {
            IsStatusActural = false;
            IsStatusAdaptive = false;
            IsStatusCustomized = true;

            SizeInfo.EnumSizeType = EnumSizeType.StatusCustomized;
        }

        public void SetDisplayRatio()
        {
            int previousDisplayRatio = SizeInfo.DisplayRatio;
            SizeInfo.DisplayRatio = int.Parse(DisplayRatio);
            if (previousDisplayRatio != SizeInfo.DisplayRatio)
            {
                SendSizeInfo();
            }
        }

        private void InitSizeInfo()
        {
            if (IsStatusAdaptive)
            {
                SizeInfo.EnumSizeType = EnumSizeType.StatusAdaptive;
            }
            else if (IsStatusActural)
            {
                SizeInfo.EnumSizeType = EnumSizeType.StatusActural;
            }
            else
            {
                SizeInfo.EnumSizeType = EnumSizeType.StatusCustomized;
            }
            SizeInfo.DisplayRatio = int.Parse(DisplayRatio);
        }

        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return true;
        }

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {
        }

        public void OnNavigatedTo(NavigationContext navigationContext)
        {
            InitSizeInfo();
            SendSizeInfo();
        }
    }
}
