using ComPDFKitViewer;
using ComPDFKitViewer.AnnotEvent;
using ComPDFKitViewer.PdfViewer;
using PDF_Office.Helper;
using PDF_Office.Model;
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;

namespace PDF_Office.ViewModels.Form
{
    public class FormsToolContentViewModel : BindableBase, INavigationAware
    {
        #region 属性

        /// <summary>
        /// 显示PreView按钮
        /// </summary>
        public Visibility ShowPreView
        {
            get { return showPreView; }
            set
            {
                SetProperty(ref showPreView, value);
            }
        }

        #endregion

        #region 变量
        private CPDFViewer PDFViewer;

        public ViewContentViewModel viewContentViewModel;

        private IRegionManager regions;

        private string CurrentToolBtnType;
        /// <summary>
        /// 按钮和属性面板键值对
        /// </summary>
        private Dictionary<string, string> btnToProperty = new Dictionary<string, string>();

        private Visibility showPreView;
        #endregion

        #region Command变量
        public DelegateCommand<string> CheckedCommand { get; set; }
        public DelegateCommand CancleCheckedCommand { get; set; }
        public DelegateCommand<string> ClickCommand { get; set; }

        public event EventHandler<string> UncheckedToolsBtnEvent;
        
        #endregion

        #region 初始化

        public FormsToolContentViewModel(IRegionManager regionManager)
        {
            regions = regionManager;

            CheckedCommand = new DelegateCommand<string>(CheckedEvent);
            CancleCheckedCommand = new DelegateCommand(CancleChecked);
            ClickCommand = new DelegateCommand<string>(ClickEvent);
            InitBtnToProperty();
        }


        private void InitBtnToProperty()
        {
            btnToProperty.Add("EmptyForm", "EmptyFormProperty");
            btnToProperty.Add("Text", "TextFieldProperty");
            btnToProperty.Add("CheckBox", "CheckBoxProperty");
            btnToProperty.Add("RadioButton", "RadioButtonProperty");
            btnToProperty.Add("ListBox", "ListBoxProperty");
            btnToProperty.Add("Combox", "ComboxProperty");
            btnToProperty.Add("Button", "ButtonProperty");
            btnToProperty.Add("Sign", "SignProperty");
        }

        #endregion

        #region 属性面板

        /// <summary>
        /// Form
        /// </summary>
        /// <param name="type"></param>
        private void CheckedEvent(string type)
        {
            AddToPropertyPanel(type);
        }

        //
        private void ClickEvent(string obj)
        {

            if (string.IsNullOrEmpty(obj) == false)
            {
                if (CurrentToolBtnType == obj)
                {
                    PDFViewer.SetMouseMode(MouseModes.FormEditTool);
                    PDFViewer.SetToolParam(new AnnotHandlerEventArgs());
                    UncheckedToolsBtnEvent?.Invoke(null, CurrentToolBtnType);
                    CurrentToolBtnType = "";
                }
                else
                {
                    CurrentToolBtnType = obj;
                }
               
            }
        }

        private void AddToPropertyPanel(string type, WidgetArgs widget = null, UpdateAttributeHelper e = null)
        {
            if (btnToProperty.ContainsKey(type))
            {
                NavigationParameters parameters = new NavigationParameters();
                parameters.Add(ParameterNames.PDFViewer, PDFViewer);
                parameters.Add("WidgetArgs", widget);
                parameters.Add(ParameterNames.AnnotEvent, e);
                regions.RequestNavigate(RegionNames.PropertyRegionName, btnToProperty[type], parameters);
                ShowPropertyPanel(true);
            }
        }
        private void ShowPropertyPanel(bool show = true)
        {
            viewContentViewModel.IsPropertyOpen = show;
        }
        #endregion

        #region 工具按钮
        private void CancleChecked()
        {
            PDFViewer.SetMouseMode(MouseModes.FormEditTool);
            PDFViewer.SetToolParam(new AnnotHandlerEventArgs());
        }


        #endregion

        #region 获取Form

        private void GetTextField()
        {
            
        }

        private void GetButton()
        {

        }

        private void GeCheckBox()
        {

        }

        private void GetCombox()
        {

        }

        private void GetListBox()
        {

        }

        private void GetRadioButton()
        {

        }

        private void GetSign()
        {

        }


        #endregion

        #region 表单内部触发的事件（比如选中表单行为等）
        private void PDFViewer_WidgetClickHander(object sender, WidgetArgs e)
        {
            //点击表单，签名表单，触发事件
            if (e != null)
            {

            }
        }
        private void PDFViewer_AnnotActiveHandler(object sender, AnnotAttribEvent e)
        {
            if (e != null)
            {
               // 选中时
                var annotHandlerEventArgs = e.AnnotItemsList[0];
                if (annotHandlerEventArgs as WidgetArgs != null)
                {
                    var widgetArgs = annotHandlerEventArgs as WidgetArgs;

                    //新建的表单之后；
                    if (CurrentWidgetArgs != null &&
                        CurrentWidgetArgs.AnnotIndex == widgetArgs.AnnotIndex  &&
                        CurrentWidgetArgs.PageIndex == widgetArgs.PageIndex)
                    {
                        GetSelectWidget(widgetArgs, e);
                        return;
                    }

                    CurrentWidgetArgs = widgetArgs;

                    GetSelectWidget(widgetArgs, e);
                    UncheckedToolsBtnEvent?.Invoke(null, CurrentToolBtnType);
                    CurrentToolBtnType = "";
                }

            }

        }

        public WidgetArgs CurrentWidgetArgs;
        private void PDFViewer_AnnotEditHandler(object sender, List<AnnotEditEvent> e)
        {
           if(e != null && e.Count > 0)
            {
            
                var widgeArgs = e[e.Count - 1].EditAnnotArgs as WidgetArgs;
                if (widgeArgs != null)
                {
                    CurrentWidgetArgs = widgeArgs;
                }
                AnnotEditEvent editEvent = e[e.Count - 1];
                //新增表单后，选中
                if (editEvent.EditAction == ActionType.Add)
                {
                    PDFViewer.SelectAnnotation(editEvent.PageIndex, editEvent.AnnotIndex);
                }
               

            }
        }

        //获取View选中的表单
        private void GetSelectWidget(WidgetArgs widgetArgs, AnnotAttribEvent e)
        {
            if (widgetArgs == null) return;

            switch (widgetArgs.WidgeType)
            {
                case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_TEXTFIELD:
                    AddToPropertyPanel("Text", widgetArgs, new UpdateAttributeHelper(e));
                    break;
                case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_CHECKBOX:
                    AddToPropertyPanel("CheckBox", widgetArgs, new UpdateAttributeHelper(e));
                    break;
                case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_RADIOBUTTON:
                    AddToPropertyPanel("RadioButton", widgetArgs, new UpdateAttributeHelper(e));
                    break;
                case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_LISTBOX:
                    AddToPropertyPanel("ListBox", widgetArgs, new UpdateAttributeHelper(e));
                    break;
                case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_COMBOBOX:
                    AddToPropertyPanel("Combox", widgetArgs, new UpdateAttributeHelper(e));
                    break;
                case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_PUSHBUTTON:
                    AddToPropertyPanel("Button", widgetArgs, new UpdateAttributeHelper(e));
                    break;
                case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_SIGNATUREFIELDS:
                    AddToPropertyPanel("Sign", widgetArgs, new UpdateAttributeHelper(e));
                    break;

            }
        }
      
        #endregion

        #region Navegation
        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return true;
        }
        
        public void OnNavigatedFrom(NavigationContext navigationContext)
        {
            CurrentToolBtnType = "";
        }

        public void OnNavigatedTo(NavigationContext navigationContext)
        {
            navigationContext.Parameters.TryGetValue<ViewContentViewModel>(ParameterNames.ViewContentViewModel,out viewContentViewModel);
            navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);

            BindingFormHandler();
        }

        private void BindingFormHandler()
        {
            if (PDFViewer != null)
            {
                PDFViewer.WidgetClickHander -= PDFViewer_WidgetClickHander;
                PDFViewer.WidgetClickHander += PDFViewer_WidgetClickHander;
                PDFViewer.AnnotActiveHandler -= PDFViewer_AnnotActiveHandler;
                PDFViewer.AnnotActiveHandler += PDFViewer_AnnotActiveHandler;
                PDFViewer.AnnotEditHandler -= PDFViewer_AnnotEditHandler;
                PDFViewer.AnnotEditHandler += PDFViewer_AnnotEditHandler;
            }
        }

        #endregion
    }
}
