using PDF_Office.Model;
using PDF_Office.Model.PageEdit;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace PDF_Office.ViewModels.Dialog.PageEditDialogs
{
    public class InsertDialogViewModel : BindableBase, IDialogAware
    {
        public string Title =>"";

        /// <summary>
        /// 数据模型
        /// </summary>
        private CustomInsertModel Model = new CustomInsertModel();

        public event Action<IDialogResult> RequestClose;

        private string currentPageSize;

        /// <summary>
        /// 当前页的尺寸大小  括号显示的形式
        /// </summary>
        public string CurrentPageSize
        {
            get { return currentPageSize; }
            set
            {
                SetProperty(ref currentPageSize, value);
            }
        }

        private int itemSelectedIndex = 0;

        /// <summary>
        /// 自定义页面的选中索引
        /// </summary>
        public int ItemSelectedIndex
        {
            get { return itemSelectedIndex; }
            set
            {
                SetProperty(ref itemSelectedIndex, value);
                if(Model!=null)
                {
                    Model.filepath = Pages[itemSelectedIndex].FilePath;
                }
            }
        }


        private string customWidth;
        /// <summary>
        /// 自定义页面宽度
        /// </summary>
        public string CustomWidth
        {
            get { return customWidth; }
            set
            {
                SetProperty(ref customWidth, value);
            }
        }

        private string customHeight;
        /// <summary>
        /// 自定义页面高度
        /// </summary>
        public string CustomHeight
        {
            get { return customHeight; }
            set
            {
                SetProperty(ref customHeight, value);
            }
        }




        /// <summary>
        /// 自定义页面的路径集合
        /// </summary>
        public ObservableCollection<CustomPageItem> Pages { get; set; }

        /// <summary>
        /// 页面单位集合
        /// </summary>
        public List<string> Units { get; set; }

        public DelegateCommand CancelCommand { get; set; }

        public DelegateCommand InsertCommnad { get; set; }

        /// <summary>
        /// 页面方向选择的事件
        /// </summary>
        public DelegateCommand<string> OrientationCheckedCommand { get; set; }

        public InsertDialogViewModel()
        {
            InitPageSource();
            InitUnits();

            CancelCommand = new DelegateCommand(cancel);
            InsertCommnad = new DelegateCommand(insert);
            OrientationCheckedCommand = new DelegateCommand<string>(OrientationChecked);
        }

        /// <summary>
        /// 初始化页面大小单位集合
        /// </summary>
        private  void InitUnits()
        {

        }

        /// <summary>
        /// 初始化自定义页面集合
        /// </summary>
        private void InitPageSource()
        {
            Pages = new ObservableCollection<CustomPageItem>();
            Pages.Add(new CustomPageItem() {Name="空白页",FilePath = ""});
            Pages.Add(new CustomPageItem() { Name="横线",FilePath= System.IO.Path.Combine(Environment.CurrentDirectory, @"Resources\PageEdit\HorizontalLine.png")});
            Pages.Add(new CustomPageItem() { Name = "五线谱", FilePath = System.IO.Path.Combine(Environment.CurrentDirectory, @"Resources\PageEdit\Staff.png") });
            Pages.Add(new CustomPageItem() { Name = "格子线", FilePath = System.IO.Path.Combine(Environment.CurrentDirectory, @"Resources\PageEdit\GridLine.png") });
        }

        /// <summary>
        /// 页面方向选中事件
        /// </summary>
        /// <param name="orientation"></param>
        private void OrientationChecked(string orientation)
        {
            switch (orientation)
            {
                //如果宽高不符合条件就对调宽高
                case "Vertical":
                    if(Model.height<=Model.width)
                    {
                        var temp = Model.height;
                        Model.height = Model.width;
                        Model.width = Model.height;
                    }
                    break;
                case "Horizontal":
                    if (Model.height> Model.width)
                    {
                        var temp = Model.height;
                        Model.height = Model.width;
                        Model.width = Model.height;
                    }
                    break;
                default:
                    break;
            }
        }

        private void cancel()
        {
            RequestClose.Invoke(new DialogResult(ButtonResult.Cancel));
        }

        private void insert()
        {
            DialogParameters valuePairs = new DialogParameters();
            valuePairs.Add(ParameterNames.DataModel, Model);
            RequestClose.Invoke(new DialogResult(ButtonResult.OK, valuePairs));
        }

        #region 弹窗接口

        public bool CanCloseDialog()
        {
            return true;
        }

        public void OnDialogClosed()
        {
        
        }

        public void OnDialogOpened(IDialogParameters parameters)
        {
            if(parameters!=null)
            {
                var size = parameters.GetValue<Size>("CurrentPageSize");
                CurrentPageSize = $"({size.Width.ToString("F2")}mm*{size.Height.ToString("F2")}mm)";
                Model.width = (int)size.Width;
                Model.height = (int)size.Height;
            }
        }
        #endregion
    }
}