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;

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

        private int? _verticalPageNumber;
        public int? VerticalPageNumber
        {
            get { return _verticalPageNumber; }
            set { SetProperty(ref _verticalPageNumber, value); }
        }

        private int? _horizontalPageNumber;
        public int? HorizontalPageNumber
        {
            get { return _horizontalPageNumber; }
            set { SetProperty(ref _horizontalPageNumber, value); }
        }

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

        private List<string> _pageMatrixList;
        public List<string> PageMatrixList
        {
            get { return _pageMatrixList; }
            set { _pageMatrixList = value; }
        }

        private int _pageMatrixIndex;
        /// <summary>
        /// <para>0: 1x2</para>
        /// <para>1: 2x2</para>
        /// <para>2: 3x2</para>
        /// <para>3: 3x3</para>
        /// <para>4: 4x4</para>
        /// <para>5: 自定义</para>
        /// </summary>
        public int PageMatrixIndex
        {
            get { return _pageMatrixIndex; }
            set
            {
                _pageMatrixIndex = value;
                if (_pageMatrixIndex < 5)
                {
                    EnableCustomiseMatrix = false;
                }
                else
                {
                    EnableCustomiseMatrix = true;
                }
                SetPageMatrixValue(value);
            }
        }

        private List<string> _pageOrderList;
        public List<string> PageOrderList
        {
            get { return _pageOrderList; }
            set { _pageOrderList = value; }
        }

        private int _pageOrderIndex;
        /// <summary>
        /// <para>0: Horizontal</para>
        /// <para>1: HorizontalReversed</para>
        /// <para>0: Vertical</para>
        /// <para>0: VerticalReversed</para>
        /// </summary>
        public int PageOrderIndex
        {
            get { return _pageOrderIndex; }
            set
            {
                _pageOrderIndex = value; 
                InitMultipleInfo();
                SendMultipleInfo();
            }
        }

        private bool _enableCustomiseMatrix;
        public bool EnableCustomiseMatrix
        {
            get { return _enableCustomiseMatrix; }
            set
            {
                SetProperty(ref _enableCustomiseMatrix, value);
            }
        }

        private MultipleInfo _multipleInfo;
        /// <summary>
        /// 多页参数
        /// </summary>
        public MultipleInfo MultipleInfo
        {
            get { return _multipleInfo; }
            set { _multipleInfo = value; }
        }

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

            PageMatrixIndex = 0;
            PageOrderIndex = 0;
            PageMatrixList = new List<string>();
            InitPageMatrixList();
            PageOrderList = new List<string>();
            InitPageOrderList();

            MultipleInfo = new MultipleInfo();
        }

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

        private void InitPageMatrixList()
        {
            PageMatrixList.Clear();
            PageMatrixList.Add("2");
            PageMatrixList.Add("4");
            PageMatrixList.Add("6");
            PageMatrixList.Add("9");
            PageMatrixList.Add("16");
            PageMatrixList.Add("自定义");
        }

        private void InitPageOrderList()
        {
            PageOrderList.Clear();
            PageOrderList.Add("横向");
            PageOrderList.Add("横向倒序");
            PageOrderList.Add("纵向");
            PageOrderList.Add("纵向倒序");
        }

        private void SetPageMatrixValue(int pageMatrixIndex)
        {
            switch (pageMatrixIndex)
            {
                case 0:
                    HorizontalPageNumber = 1;
                    VerticalPageNumber = 2;
                    InitMultipleInfo();
                    SendMultipleInfo();

                    break;

                case 1:
                    HorizontalPageNumber = 2;
                    VerticalPageNumber = 2;
                    InitMultipleInfo();

                    SendMultipleInfo();

                    break;

                case 2:
                    HorizontalPageNumber = 3;
                    VerticalPageNumber = 2;
                    InitMultipleInfo();

                    SendMultipleInfo();

                    break;

                case 3:
                    HorizontalPageNumber = 3;
                    VerticalPageNumber = 3;
                    InitMultipleInfo();

                    SendMultipleInfo();

                    break;
                case 4:
                    HorizontalPageNumber = 4;
                    VerticalPageNumber = 4;
                    InitMultipleInfo();

                    SendMultipleInfo();

                    break;
                case 5:
                    HorizontalPageNumber = null;
                    VerticalPageNumber = null;

                    break;
            }
        }

        private void InitMultipleInfo()
        {
            if (HorizontalPageNumber == null)
            {
                HorizontalPageNumber = 1;
            }

            if (VerticalPageNumber == null)
            {
                VerticalPageNumber = 1;
            }
            if (MultipleInfo != null)
            {
                MultipleInfo.VerticalPageNumber = (int)VerticalPageNumber;
                MultipleInfo.HorizontalPageNumber = (int)HorizontalPageNumber;
                MultipleInfo.EnumDisplayPageNumber = EnumDisplayPageNumber.StatusTwo;
                MultipleInfo.IsAutoRotate = IsAutoRotate;
                MultipleInfo.EnumPageOrder = (EnumPageOrder)PageOrderIndex;
            }

        }

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

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {
        }

        public void OnNavigatedTo(NavigationContext navigationContext)
        {
            InitMultipleInfo();
            SendMultipleInfo();
        }
    }
}
