using ComPDFKit.PDFDocument;
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.Diagnostics;
using System.Linq;
using System.Management.Instrumentation;

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

        private CPDFDocument document;

        private bool _isAutoRotate;
        public bool IsAutoRotate
        {
            get { return _isAutoRotate; }
            set { _isAutoRotate = value; }
        }

        private int? _paperFrom;
        public int? PaperFrom
        {
            get
            {
                return _paperFrom;
            }
            set
            {
                if (PaperTo == null)
                {
                    SetProperty(ref _paperFrom, 1);
                }
                else if (value < PaperTo)
                {
                    SetProperty(ref _paperFrom, value);
                }
                else
                {
                    SetProperty(ref _paperFrom, PaperTo);
                }
            }
        }

        private int? _paperTo;
        public int? PaperTo
        {
            get
            {
                return _paperTo;
            }
            set
            {
                if (value < MaxPaperNumber)
                {
                    SetProperty(ref _paperTo, value);
                }
                else
                {
                    SetProperty(ref _paperTo, MaxPaperNumber);
                }
            }
        }

        private List<string> _subsetList;
        public List<string> SubsetList
        {
            get { return _subsetList; }
            set { _subsetList = value; }
        }

        private int _subsetIndex = 0;
        public int SubsetIndex
        {
            get { return _subsetIndex; }
            set
            {
                _subsetIndex = value;
                InitBookletInfo();
                SendBookletInfo();
            }
        }

        private List<string> _bindingList;
        public List<string> BindingList
        {
            get { return _bindingList; }
            set { _bindingList = value; }
        }

        private int _bindingIndex = 0;
        public int BindingIndex
        {
            get { return _bindingIndex; }
            set
            {
                _bindingIndex = value;
                InitBookletInfo();
                SendBookletInfo();
            }
        }

        private BookletInfo _bookletInfo;
        public BookletInfo BookletInfo
        {
            get { return _bookletInfo; }
            set { _bookletInfo = value; }
        }

        private int _maxPaperNumber;
        public int MaxPaperNumber
        {
            get { return _maxPaperNumber; }
            set { _maxPaperNumber = value; }
        }

        public HomePagePrinterModBookletContentViewModel(IEventAggregator eventAggregator)
        {
            this.eventAggregator = eventAggregator;

            SubsetList = new List<string>();
            InitSubsetList();
            BindingList = new List<string>();
            InitBindingList();

            BookletInfo = new BookletInfo();
        }

        private void InitSubsetList()
        {
            SubsetList.Clear();
            SubsetList.Add("双面");
            SubsetList.Add("仅正面");
            SubsetList.Add("仅背面");
        }

        private void InitBindingList()
        {
            BindingList.Clear();
            BindingList.Add("左");
            BindingList.Add("右");
        }

        private void InitBookletInfo()
        {
            BookletInfo.EnumBookletSubset = (EnumBookletSubset)SubsetIndex;
            BookletInfo.EnumBookletBinding = (EnumBookletBinding)BindingIndex;
            if (PaperFrom != null)
            {
                BookletInfo.BeginPaperIndex = (int)PaperFrom;
            }
            else
            {
                BookletInfo.BeginPaperIndex = 1;
            }
            if (PaperTo != null)
            {
                BookletInfo.EndPaperIndex = (int)PaperTo;
            }
            else
            {
                BookletInfo.EndPaperIndex = MaxPaperNumber;
            }
            BookletInfo.IsAutoRotate = IsAutoRotate;
        }

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

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

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {
        }

        public void OnNavigatedTo(NavigationContext navigationContext)
        {
            if (navigationContext.Parameters.ContainsKey("document"))
            {
                document = navigationContext.Parameters.GetValue<CPDFDocument>("document");
                MaxPaperNumber = (document.PageCount % 4 == 0) ? (document.PageCount / 4) : (document.PageCount / 4 + 1);
                if (PaperFrom == null)
                {
                    PaperFrom = 1;
                }
                if (PaperTo == null)
                {
                    PaperTo = MaxPaperNumber;
                }
                InitBookletInfo();
                SendBookletInfo();
            }
            else
            {
                Trace.WriteLine("No Key Named{0}", "document");
            }
        }
    }
}
