using ComPDFKitViewer;
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Collections.ObjectModel;
using ComPDFKit.PDFDocument.Action;
using ComPDFKit.PDFAnnotation;
using ComPDFKit.PDFDocument;
using ComPDFKit.Tool;
using ComPDFKit.Tool.Help;
using System.Windows.Input;
using ComPDFKit.PDFAnnotation.Form;
using static ComPDFKit.PDFAnnotation.CTextAttribute.CFontNameHelper;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Button;

namespace Compdfkit_Tools.PDFControl
{
    public partial class PushButtonProperty : UserControl
    {
        private PushButtonParam widgetParam = null;
        private CPDFPushButtonWidget cPDFAnnotation = null;
        private PDFViewControl pdfViewerControl = null;
        private CPDFDocument cPDFDocument = null;
        public ObservableCollection<int> SizeList { get; set; } = new ObservableCollection<int>
        {
            6,8,9,10,12,14,18,20,24,26,28,32,30,32,48,72
        };

        bool IsLoadedData = false;
        public PushButtonProperty()
        {
            InitializeComponent();
        }

        #region Loaded

        public void SetProperty(AnnotParam annotParam, CPDFAnnotation annotation, CPDFDocument doc, PDFViewControl cPDFViewer)
        {
            widgetParam = (PushButtonParam)annotParam;
            cPDFAnnotation = (CPDFPushButtonWidget)annotation;
            pdfViewerControl = cPDFViewer;
            cPDFDocument = doc;
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            Binding SizeListbinding = new Binding();
            SizeListbinding.Source = this;
            SizeListbinding.Path = new System.Windows.PropertyPath("SizeList");
            FontSizeCombox.SetBinding(ComboBox.ItemsSourceProperty, SizeListbinding);

            TopTabControl.SelectedIndex = 2;
            FieldNameText.Text = widgetParam.FieldName;
            FormFieldCombox.SelectedIndex = (int)ParamConverter.ConverterWidgetFormFlags(widgetParam.Flags, widgetParam.IsHidden);
            BorderColorPickerControl.SetCheckedForColor(ParamConverter.ConverterByteForColor(widgetParam.LineColor));
            BackgroundColorPickerControl.SetCheckedForColor(ParamConverter.ConverterByteForColor(widgetParam.BgColor));
            TextColorPickerControl.SetCheckedForColor(ParamConverter.ConverterByteForColor(widgetParam.FontColor));
            SetFontName(widgetParam.FontName);
            SetFontStyle(widgetParam.IsItalic, widgetParam.IsBold);
            SetFontSize(widgetParam.FontSize);
            ItemText.Text = widgetParam.Text;
            SetActionContext();
            IsLoadedData = true;
        }

        private void UserControl_Unloaded(object sender, RoutedEventArgs e)
        {
            IsLoadedData = false;
        }

        private void SetActionContext()
        {
            switch (widgetParam.Action)
            {
                case C_ACTION_TYPE.ACTION_TYPE_GOTO:
                    TextAlignmentCombox.SelectedIndex = 1;
                    ActionContentText.Visibility = Visibility.Visible;
                    ActionContentText.Text = (widgetParam.DestinationPageIndex + 1).ToString();
                    break;
                case C_ACTION_TYPE.ACTION_TYPE_URI:
                    TextAlignmentCombox.SelectedIndex = 2;
                    ActionContentText.Visibility = Visibility.Visible;
                    ActionContentText.Text = widgetParam.Uri;
                    break;
                default:
                    TextAlignmentCombox.SelectedIndex = 0;
                    ActionContentText.Text = "";
                    break;
            }
        }
        private void SetFontSize(double size)
        {
            int index = SizeList.IndexOf((int)size);
            FontSizeCombox.SelectedIndex = index;
        }

        private void SetFontStyle(bool IsItalic, bool IsBold)
        {
            int index = 0;
            if (IsItalic && IsBold)
            {
                index = 3;
            }
            else if (IsItalic)
            {
                index = 2;
            }
            else if (IsBold)
            {
                index = 1;
            }
            FontStyleCombox.SelectedIndex = index;
        }

        private void SetFontName(string fontName)
        {
            int index = -1;
            List<string> fontFamilyList = new List<string>() { "Helvetica", "Courier", "Times" };
            for (int i = 0; i < fontFamilyList.Count; i++)
            {
                if (fontFamilyList[i].ToLower().Contains(fontName.ToLower())
                    || fontName.ToLower().Contains(fontFamilyList[i].ToLower()))
                {
                    index = i;
                }
            }
            FontCombox.SelectedIndex = index;
        }

        #endregion

        #region Updata

        private void FieldNameText_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (IsLoadedData)
            {
                cPDFAnnotation.SetFieldName((sender as TextBox).Text);
                pdfViewerControl.PDFViewTool.GetCPDFViewer().UpDataAnnotFrame();
            }
        }


        private void FormFieldCombox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (IsLoadedData)
            {
                ParamConverter.SetFormField((ParamConverter.FormField)(sender as ComboBox).SelectedIndex, cPDFAnnotation);
                pdfViewerControl.PDFViewTool.GetCPDFViewer().UpDataAnnotFrame();
            }
        }


        private void BorderColorPickerControl_ColorChanged(object sender, EventArgs e)
        {
            if (IsLoadedData)
            {
                byte[] Color = new byte[3];
                Color[0] = ((SolidColorBrush)BorderColorPickerControl.Brush).Color.R;
                Color[1] = ((SolidColorBrush)BorderColorPickerControl.Brush).Color.G;
                Color[2] = ((SolidColorBrush)BorderColorPickerControl.Brush).Color.B;
                cPDFAnnotation.SetWidgetBorderRGBColor(Color);
                pdfViewerControl.PDFViewTool.GetCPDFViewer().UpDataAnnotFrame();
            }
        }


        private void BackgroundColorPickerControl_ColorChanged(object sender, EventArgs e)
        {
            if (IsLoadedData)
            {
                byte[] Color = new byte[3];
                Color[0] = ((SolidColorBrush)BackgroundColorPickerControl.Brush).Color.R;
                Color[1] = ((SolidColorBrush)BackgroundColorPickerControl.Brush).Color.G;
                Color[2] = ((SolidColorBrush)BackgroundColorPickerControl.Brush).Color.B;
                cPDFAnnotation.SetWidgetBgRGBColor(Color);
                pdfViewerControl.PDFViewTool.GetCPDFViewer().UpDataAnnotFrame();
            }
        }

        private void TextColorPickerControl_ColorChanged(object sender, EventArgs e)
        {
            if (IsLoadedData)
            {
                byte[] Color = new byte[3];
                Color[0] = ((SolidColorBrush)TextColorPickerControl.Brush).Color.R;
                Color[1] = ((SolidColorBrush)TextColorPickerControl.Brush).Color.G;
                Color[2] = ((SolidColorBrush)TextColorPickerControl.Brush).Color.B;
                CTextAttribute cTextAttribute = cPDFAnnotation.GetTextAttribute();
                cTextAttribute.FontColor = Color;
                cPDFAnnotation.SetTextAttribute(cTextAttribute);
                cPDFAnnotation.UpdateFormAp();
                pdfViewerControl.PDFViewTool.GetCPDFViewer().UpDataAnnotFrame();
            }
        }

        private void FontCombox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (IsLoadedData)
            {
                CTextAttribute cTextAttribute = cPDFAnnotation.GetTextAttribute();
                bool isBold = IsBold(cTextAttribute.FontName);
                bool isItalic = IsItalic(cTextAttribute.FontName);
                FontType fontType = GetFontType(cTextAttribute.FontName);
                cTextAttribute.FontName = ObtainFontName(fontType, isBold, isItalic);
                cPDFAnnotation.SetTextAttribute(cTextAttribute);
                cPDFAnnotation.UpdateFormAp();
                pdfViewerControl.PDFViewTool.GetCPDFViewer().UpDataAnnotFrame();
            }
        }

        private void FontStyleCombox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (IsLoadedData)
            {
                CTextAttribute cTextAttribute = cPDFAnnotation.GetTextAttribute();
                bool isItalic = false;
                bool isBold = false;
                switch ((sender as ComboBox).SelectedIndex)
                {
                    case 0:
                        break;
                    case 1:
                        isItalic = IsItalic(cTextAttribute.FontName);
                        isBold = true;
                        break;
                    case 2:
                        isItalic = true;
                        isBold = IsBold(cTextAttribute.FontName);
                        break;
                    case 3:
                        isItalic = true;
                        isBold = true;
                        break;
                    default:
                        break;
                }

                FontType fontType = GetFontType(cTextAttribute.FontName);
                cTextAttribute.FontName = ObtainFontName(fontType, isBold, isItalic);
                cPDFAnnotation.SetTextAttribute(cTextAttribute);
                cPDFAnnotation.UpdateFormAp();
                pdfViewerControl.PDFViewTool.GetCPDFViewer().UpDataAnnotFrame();
            }
        }

        private void FontSizeCombox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (IsLoadedData)
            {
                CTextAttribute cTextAttribute = cPDFAnnotation.GetTextAttribute();
                cTextAttribute.FontSize = Convert.ToSingle((sender as ComboBox).SelectedItem);
                cPDFAnnotation.SetTextAttribute(cTextAttribute);
                cPDFAnnotation.UpdateFormAp();
                pdfViewerControl.PDFViewTool.GetCPDFViewer().UpDataAnnotFrame();
            }
        }
        #endregion

        private void TextAlignmentCombox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (IsLoadedData)
            {
                ActionContentText.Style = null;
                switch (TextAlignmentCombox.SelectedIndex)
                {
                    case 0:
                        ActionContentText.Visibility = Visibility.Collapsed;
                        break;
                    case 1:
                        ActionContentText.Visibility = Visibility.Visible;
                        ActionContentText.Text = "";
                        break;
                    case 2:
                        ActionContentText.Visibility = Visibility.Visible;
                        ActionContentText.Style = this.FindResource("txtboxStyle") as Style;
                        ActionContentText.Text = "";
                        break;
                    default:
                        break;
                }
                if (ActionContentText.Visibility != Visibility.Collapsed && ActionContentText != null)
                    AddAction();
            }
        }
        private void AddAction()
        {
            Dictionary<C_ACTION_TYPE, string> ActionDict = new Dictionary<C_ACTION_TYPE, string>();
            if (TextAlignmentCombox.SelectedIndex == 1 && !string.IsNullOrEmpty(TextAlignmentCombox.Text))
            {
                int page = 0;
                int.TryParse(ActionContentText.Text.Trim(), out page);
                if (page <= 0 || page > pdfViewerControl.PDFViewTool.GetCPDFViewer().GetDocument().PageCount)
                {
                    page = 1;
                }
                if (page - 1 >= 0)
                {
                    CPDFGoToAction gotoAction = new CPDFGoToAction();
                    CPDFDestination destination = new CPDFDestination();
                    destination.Position_X = 0;
                    destination.Position_Y = 0;
                    destination.PageIndex = page;
                    gotoAction.SetDestination(cPDFDocument, destination);
                    cPDFAnnotation.SetButtonAction(gotoAction);
                }
            }
            if (TextAlignmentCombox.SelectedIndex == 2)
            {
                CPDFUriAction uriAction = new CPDFUriAction();
                if (string.IsNullOrEmpty(ActionContentText.Text.Trim()))
                {
                    uriAction.SetUri(@"https://www.compdf.com");
                }
                else
                {
                    uriAction.SetUri(ActionContentText.Text.Trim());
                }
                cPDFAnnotation.SetButtonAction(uriAction);
            }

            pdfViewerControl.PDFViewTool.GetCPDFViewer().UpDataAnnotFrame();
        }
        private void ItemText_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (IsLoadedData)
            {
                cPDFAnnotation.SetButtonTitle((sender as TextBox).Text);
                pdfViewerControl.PDFViewTool.GetCPDFViewer().UpDataAnnotFrame();
            }
        }

        private void ActionContentText_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (IsLoadedData)
            {
                AddAction();
            }
        }
    }
}
