using ComPDFKitViewer;
using ComPDFKitViewer.AnnotEvent;
using ComPDFKitViewer.PdfViewer;
using PDF_Office.Helper;
using PDF_Office.Model;
using PDF_Office.Model.From;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;

namespace PDF_Office.ViewModels.Form
{
    public class TextFieldPropertyViewModel : FormBaseVM, INavigationAware
    {
        #region 属性

        #region 一般
        private FormFieldType _formPos;
        public FormFieldType FormPos
        {
            get { return _formPos; }
            set { SetProperty(ref _formPos, value); ChangeFieldValue("FormPos"); }
        }

        #endregion

        #region 选项
        //默认值
        private string _defaultValue;
        public string DefaultValue
        {
            get { return _defaultValue; }
            set { SetProperty(ref _defaultValue, value); }
        }

        //多行
        private bool _isMultiline = false;
        public bool IsMultiLine
        {
            get { return _isMultiline; }
            set { SetProperty(ref _isMultiline, value); }
        }

        //滚动显示长文本
        private bool _isScrollText = false;
        public bool IsScrollText
        {
            get { return _isScrollText; }
            set { SetProperty(ref _isScrollText, value); }
        }

        #endregion

        #endregion

        #region Command变量
        public DelegateCommand<string> NameTextChangedCommand { get; set; }
        public DelegateCommand<string> ToolTipTextChangedCommand { get; set; }

        public DelegateCommand<object> IsReadOnlyCheckedCommand { get; set; }
        public DelegateCommand<object> RequiredFieldCheckedCommand { get; set; }
        
        #endregion

        #region 变量
        private CPDFViewer PDFViewer;
        private WidgetTextBoxArgs textBoxArgs;
        public UpdateAttributeHelper AttribEvent;
        #endregion

        #region 初始化
        public TextFieldPropertyViewModel()
        {
            InitVariable();
            InitCommand();
        }

        private void InitVariable()
        {

        }

        private void InitCommand()
        {
            NameTextChangedCommand = new DelegateCommand<string>(NameTextChanged);
            ToolTipTextChangedCommand = new DelegateCommand<string>(ToolTipTextChanged);
            IsReadOnlyCheckedCommand = new DelegateCommand<object>(IsReadOnlyChecked);
            RequiredFieldCheckedCommand = new DelegateCommand<object>(RequiredFieldChecked);

            ChangeValueHandler -= ChangeValue;
            ChangeValueHandler += ChangeValue;
        }


        #endregion


        #region 一般处理
        private void NameTextChanged(string obj)
        {
            if(string.IsNullOrEmpty(obj) == false)
            {
                NameStr = obj;
            }
        }

        private void ToolTipTextChanged(string obj)
        {
            if (string.IsNullOrEmpty(obj) == false)
            {
                ToolTipStr = obj;
            }
        }

        private void IsReadOnlyChecked(object obj)
        {
            if(obj is bool)
            {
                IsReadOnly = (bool)obj;
            }
        }


        private void RequiredFieldChecked(object obj)
        {
            if (obj is bool)
            {
                IsRequiredField = (bool)obj;
            }
        }
        #endregion

        #region 外观处理

        #endregion

        #region 选项处理

        #endregion

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

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {
            textBoxArgs = null;
        }

        public void OnNavigatedTo(NavigationContext navigationContext)
        {
            navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
            navigationContext.Parameters.TryGetValue<UpdateAttributeHelper>(ParameterNames.AnnotEvent, out AttribEvent);
            navigationContext.Parameters.TryGetValue<WidgetTextBoxArgs>("WidgetArgs", out textBoxArgs);
           
            GetWidgeText();
        }

        private void GetWidgeText()
        {
            PDFViewer.SetMouseMode(MouseModes.FormEditTool);

            if (textBoxArgs == null)
            {
                WidgetTextBoxArgs textArgs = new WidgetTextBoxArgs();
                textArgs.BgColor = Colors.Transparent;
                textArgs.FontSize = 12;
                textArgs.FontFamily = "Courier New";
                textArgs.FontColor = Colors.Black;
                textArgs.FieldName = "TextBox";
                textBoxArgs = textArgs;
                PDFViewer.SetToolParam(textBoxArgs);
            }
            else
            {
                PDFViewer.SetToolParam(new AnnotHandlerEventArgs());
            }

            GetProperty();
        }
        private void GetProperty()
        {
            if(textBoxArgs != null)
            {
                NameStr = textBoxArgs.FieldName;
                IsRequiredField = textBoxArgs.IsRequired;
                IsMultiLine = textBoxArgs.IsMultiLine;
                
            }
        }

        //更改基类公共属性后,触发的事件
        private void ChangeValue(object sender, FormAttributeType e)
        {
           switch(e)
            {
                case FormAttributeType.Name:
                    textBoxArgs.FieldName = NameStr;
                    AttribEvent?.UpdateAttrib(AnnotAttrib.FieldName, NameStr);
                    break;

                case FormAttributeType.ToolTip:
                    textBoxArgs.Tooltip = ToolTipStr;
                    AttribEvent?.UpdateAttrib(AnnotAttrib.Tooltip, ToolTipStr);
                    break;

                case FormAttributeType.IsSolid:
                    textBoxArgs.BorderStyle = IsSolid;
                    AttribEvent?.UpdateAttrib(AnnotAttrib.LineStyle, IsSolid);
                    break;

                case FormAttributeType.IsLocked:
                    textBoxArgs.Locked = IsLocked;
                    AttribEvent?.UpdateAttrib(AnnotAttrib.Locked, IsLocked);
                    break;

                case FormAttributeType.HeightSize:
                    textBoxArgs.DefaultWidth = HeightSize;
                    //AttribEvent?.UpdateAttrib(, BorderThiness);
                    break;

                case FormAttributeType.BorderThiness:
                    textBoxArgs.LineWidth = BorderThiness;
                    AttribEvent?.UpdateAttrib(AnnotAttrib.Thickness, BorderThiness);
                    break;

                case FormAttributeType.BorderColor:
                    textBoxArgs.LineColor = BorderColor;
                    AttribEvent?.UpdateAttrib(AnnotAttrib.Color, BorderColor);
                    break;

                case FormAttributeType.ContentColor:
                    textBoxArgs.FontColor = ContentColor;
                    AttribEvent?.UpdateAttrib(AnnotAttrib.FontColor, ContentColor);
                    break;

                case FormAttributeType.IsReadOnly:
                    textBoxArgs.ReadOnly = IsReadOnly;
                    AttribEvent?.UpdateAttrib(AnnotAttrib.ReadOnly, IsReadOnly);
                    break;

                case FormAttributeType.WidthSize:
                    break;

                case FormAttributeType.IsRequiredField:
                    textBoxArgs.IsRequired = IsRequiredField;
                    AttribEvent?.UpdateAttrib(AnnotAttrib.IsRequired, IsRequiredField);
                    break;

            }
            AttribEvent?.UpdateAnnot();

        }

        private void ChangeFieldValue(string tag)
        {
            switch(tag)
            {
                case "FormPos":
                    AttribEvent?.UpdateAttrib(AnnotAttrib.FormField,FormPos);
                    break;
            }
            AttribEvent?.UpdateAnnot();
        }

        #endregion
    }
}