using PDF_Office.Model.Dialog.HomePageToolsDialogs.HomePagePrinter;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows;

namespace PDF_Office.ViewModels.Dialog.HomePageToolsDialogs.HomePagePrinter
{
    public class HomePagePrinterPageSetDialogViewModel : BindableBase, IDialogAware
    {
        private int _paperSizeIndex;
        public int PaperSizeIndex
        {
            get { return _paperSizeIndex; }
            set { _paperSizeIndex = value; }
        }

        private int _paperSizeWidth;
        public int PaperSizeWidth
        {
            get { return _paperSizeWidth; }
            set
            {
                SetProperty(ref _paperSizeWidth, value);
            }
        }

        private int _paperSizeHeight;
        public int PaperSizeHeight
        {
            get { return _paperSizeHeight; }
            set
            {
                SetProperty(ref _paperSizeHeight, value);
            }
        }

        private List<string> _paperSizeNameList;
        public List<string> PaperSizeNameList
        {
            get { return _paperSizeNameList; }
            set { _paperSizeNameList = value; }
        }

        public Dictionary<EnumPaperSize, string> GetPaperSizeNameFromEnumPaperSize;

        private int _leftMargin;
        public int LeftMargin
        {
            get { return _leftMargin; }
            set { _leftMargin = value; }
        }

        private int _rightMargin;
        public int RightMargin
        {
            get { return _rightMargin; }
            set { _rightMargin = value; }
        }

        private int _bottomMargin;
        public int BottomMargin
        {
            get { return _bottomMargin; }
            set { _bottomMargin = value; }
        }

        private int _topMargin;
        public int TopMargin
        {
            get { return _topMargin; }
            set { _topMargin = value; }
        }

        private int[] _paperMargin;
        public int[] PaperMargin
        {
            get { return _paperMargin; }
            set { _paperMargin = value; }
        }

        private Visibility _isPageSizeStackPanelsVisible = Visibility.Hidden;
        public Visibility IsPageSizeStackPanelsVisible
        {
            get { return _isPageSizeStackPanelsVisible; }
            set { SetProperty(ref _isPageSizeStackPanelsVisible, value); }
        }

        public DelegateCommand SetPaperSizeCommand { get; set; }
        public DelegateCommand ConfirmSetPageSizeCommand { get; set; }
        public DelegateCommand CancelSetPageSizeCommand { get; set; }

        public HomePagePrinterPageSetDialogViewModel()
        {
            HomePagePrinterDialogModel homePagePrinterDialogModel = new HomePagePrinterDialogModel();
            SetPaperSizeCommand = new DelegateCommand(SetPaperSize);
            ConfirmSetPageSizeCommand = new DelegateCommand(ConfirmSetPageSize);
            CancelSetPageSizeCommand = new DelegateCommand(CancelSetPageSize);

            GetPaperSizeNameFromEnumPaperSize = new Dictionary<EnumPaperSize, string> { };
            InitGetPaperSizeNameFromEnumPaperSize(ref GetPaperSizeNameFromEnumPaperSize);
            InitPaperSizeList();
        }

        public void InitGetPaperSizeNameFromEnumPaperSize(ref Dictionary<EnumPaperSize, string> Dictionary)
        {
            GetPaperSizeNameFromEnumPaperSize.Clear();
            GetPaperSizeNameFromEnumPaperSize.Add(EnumPaperSize.StatusA3, "A3");
            GetPaperSizeNameFromEnumPaperSize.Add(EnumPaperSize.StatusA4, "A4");
            GetPaperSizeNameFromEnumPaperSize.Add(EnumPaperSize.StatusA5, "A5");
            GetPaperSizeNameFromEnumPaperSize.Add(EnumPaperSize.StatusB5, "B5");
            GetPaperSizeNameFromEnumPaperSize.Add(EnumPaperSize.StatusNo10Envelope, "10号信封");
            GetPaperSizeNameFromEnumPaperSize.Add(EnumPaperSize.StatusChoukei3Envelope, "Choukei 3信封");
            GetPaperSizeNameFromEnumPaperSize.Add(EnumPaperSize.StatusDLEnvelope, "DL信封");
            GetPaperSizeNameFromEnumPaperSize.Add(EnumPaperSize.StatusJISB5, "JIS B5");
            GetPaperSizeNameFromEnumPaperSize.Add(EnumPaperSize.StatusROC16K, "ROC 16K");
            GetPaperSizeNameFromEnumPaperSize.Add(EnumPaperSize.StatusExtraLargeBA3, "超大型B/A3型");
            GetPaperSizeNameFromEnumPaperSize.Add(EnumPaperSize.StatusTabloid, "小报用纸");
            GetPaperSizeNameFromEnumPaperSize.Add(EnumPaperSize.StatusExraLargeTabloid, "小报用纸（特大）");
            GetPaperSizeNameFromEnumPaperSize.Add(EnumPaperSize.StatusAmericanLegal, "美国法定用纸");
            GetPaperSizeNameFromEnumPaperSize.Add(EnumPaperSize.StatusAmericanLetter, "美国信纸");
            GetPaperSizeNameFromEnumPaperSize.Add(EnumPaperSize.StatusCustomized, "自定义");
        }

        public void InitPaperSizeList()
        {
            PaperSizeNameList = new List<string>();
            for (int temp = 0; temp < GetPaperSizeNameFromEnumPaperSize.Count; temp++)
            {
                PaperSizeNameList.Add(GetPaperSizeNameFromEnumPaperSize[(EnumPaperSize)temp]);
            }
        }

        public void SetPaperSize()
        {
            IsPageSizeStackPanelsVisible = Visibility.Hidden;
            switch (PaperSizeIndex)
            {
                case (int)EnumPaperSize.StatusA3:
                    PaperSizeWidth = 297;
                    PaperSizeHeight = 420;
                    break;
                case (int)EnumPaperSize.StatusA4:
                    PaperSizeWidth = 210;
                    PaperSizeHeight = 297;
                    break;
                case (int)EnumPaperSize.StatusA5:
                    PaperSizeWidth = 148;
                    PaperSizeHeight = 210;
                    break;
                case (int)EnumPaperSize.StatusB5:
                    PaperSizeWidth = 176;
                    PaperSizeHeight = 250;
                    break;
                case (int)EnumPaperSize.StatusNo10Envelope:
                    PaperSizeWidth = 458;
                    PaperSizeHeight = 324;
                    break;
                case (int)EnumPaperSize.StatusChoukei3Envelope:
                    PaperSizeWidth = 120;
                    PaperSizeHeight = 235;
                    break;
                case (int)EnumPaperSize.StatusDLEnvelope:
                    PaperSizeWidth = 220;
                    PaperSizeHeight = 110;
                    break;
                case (int)EnumPaperSize.StatusJISB5:
                    PaperSizeWidth = 176;
                    PaperSizeHeight = 276;
                    break;
                case (int)EnumPaperSize.StatusROC16K:
                    PaperSizeWidth = 197;
                    PaperSizeHeight = 273;
                    break;
                case (int)EnumPaperSize.StatusExtraLargeBA3:
                    PaperSizeWidth = 330;
                    PaperSizeHeight = 483;
                    break;
                case (int)EnumPaperSize.StatusTabloid:
                    PaperSizeWidth = 431;
                    PaperSizeHeight = 279;
                    break;
                case (int)EnumPaperSize.StatusExraLargeTabloid:
                    PaperSizeWidth = 305;
                    PaperSizeHeight = 457;
                    break;
                case (int)EnumPaperSize.StatusAmericanLegal:
                    PaperSizeWidth = 216;
                    PaperSizeHeight = 356;
                    break;
                case (int)EnumPaperSize.StatusAmericanLetter:
                    PaperSizeWidth = 216;
                    PaperSizeHeight = 279;
                    break;
                case (int)EnumPaperSize.StatusCustomized:
                    IsPageSizeStackPanelsVisible = Visibility.Visible;
                    break;
                default:
                    break;
            }
        }

        public void ConfirmSetPageSize()
        {
            var dialogResult = new DialogResult(ButtonResult.OK);
            dialogResult.Parameters.Add("LeftMargin", LeftMargin);
            dialogResult.Parameters.Add("TopMargin", TopMargin);
            dialogResult.Parameters.Add("RightMargin", RightMargin);
            dialogResult.Parameters.Add("BottomMargin", BottomMargin);
            dialogResult.Parameters.Add("PaperWidth", PaperSizeWidth);
            dialogResult.Parameters.Add("PaperHeight", PaperSizeHeight);
            dialogResult.Parameters.Add("PaperSizeIndex", PaperSizeIndex);
            RequestClose.Invoke(dialogResult);
        }

        public void CancelSetPageSize()
        {
            var dialogResult = new DialogResult(ButtonResult.Cancel);
            RequestClose.Invoke(dialogResult);
        }

        public event Action<IDialogResult> RequestClose;

        public string Title => "";


        public bool CanCloseDialog()
        {
            return true;
        }

        public void OnDialogClosed()
        {

        }

        public void OnDialogOpened(IDialogParameters parameters)
        {
        }
    }
}
