using PDF_Office.CustomControl;
using PDF_Office.ViewModels.PropertyPanel.AnnotPanel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace PDF_Office.Views.PropertyPanel.AnnotPanel
{
    /// <summary>
    /// FreetextAnnotProperty.xaml 的交互逻辑
    /// </summary>
    public partial class FreetextAnnotProperty : UserControl
    {
      
        public FreetextAnnotProperty()
        {
            InitializeComponent();
            FontFamilyBox.SelectionChanged += FontFamilyBox_SelectionChanged;
            FontStyleBox.SelectionChanged += FontStyleBox_SelectionChanged;
            FontSizeBox.SelectionChanged += FontSizeBox_SelectionChanged;
            ThicknessBox.SelectionChanged += ThicknessBox_SelectionChanged; 
            this.Loaded += usercontrol_Loaded;
            cusColor.SelectedColorHandler += cusColor_SelectedColorHandler;
            layerThick.SelectedValueChanged += layerThick_SelectedValue;
        }

        private void usercontrol_Loaded(object sender, RoutedEventArgs e)
        {
            FontFamilyBox.SelectedIndex = 0;
            FontStyleBox.SelectedIndex = 0;
            FontSizeBox.SelectedIndex = 0;
            ThicknessBox.SelectedIndex = 0;
        }

        private void ThicknessBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var listItem = ThicknessBox.ItemContainerGenerator.ContainerFromItem(ThicknessBox.SelectedItem) as ComboBoxItem;
            if (listItem != null)
                ThicknessText.Text = listItem.Content.ToString();
            else
                ThicknessText.Text = "1";
           
        }

        private void layerThick_SelectedValue(object sender, double e)
        {
            var data = this.DataContext as FreetextAnnotPropertyViewModel;
            if (data != null)
            {
                data.SelectedFillOpacityCommand?.Execute(e);
            }
        }

        private void FontSizeBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var listItem = FontSizeBox.ItemContainerGenerator.ContainerFromItem(FontSizeBox.SelectedItem) as ComboBoxItem;
            if (listItem != null)
                FontSizeText.Text = listItem.Content.ToString();
            else
                FontSizeText.Text = "6";
           
        }

        private void FontStyleBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var listItem = FontStyleBox.ItemContainerGenerator.ContainerFromItem(FontStyleBox.SelectedItem) as ComboBoxItem;
            if (listItem != null)
                FontStyleText.Text = listItem.Content.ToString();
            else
                FontStyleText.Text = "Regular";
          
        }

        private void FontFamilyBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var listItem = FontFamilyBox.ItemContainerGenerator.ContainerFromItem(FontFamilyBox.SelectedItem) as ComboBoxItem;
            if (listItem != null)
                FontFamilyText.Text = (listItem.Content as TextBlock).Text;
            else
                FontFamilyText.Text = "Courier New";
        }

        private void BtnTextAlign_Click(object sender, RoutedEventArgs e)
        {

            var btn = sender as ToggleButton;
            foreach(var item in ToolGrid.Children)
            {
                var btnItem = item as ToggleButton;
                if(btnItem != null)
                {
                    if(btn!= btnItem)
                        btnItem.IsChecked = false;
                    else
                        btnItem.IsChecked = true;
                }
            }
           
        }

        private void BtnLineStyle_Click(object sender, RoutedEventArgs e)
        {
            var btn = sender as CustomIconToggleBtn;
            foreach (var item in PnlLineStyle.Children)
            {
                var btnItem = item as CustomIconToggleBtn;
                if (btnItem != null)
                {
                    if (btn != btnItem)
                        btnItem.IsChecked = false;
                    else
                        btnItem.IsChecked = true;
                }
            }
        }

        private void FontColorBox_SelectedColorChanged(object sender, Color? e)
        {
            var data = this.DataContext as FreetextAnnotPropertyViewModel;
            data.SelectedColorCommand?.Execute(e);
        }

        private void cusColor_SelectedColorHandler(object sender, Color e)
        {
            var data = this.DataContext as FreetextAnnotPropertyViewModel; 
            data.SelectedFillColorCommand?.Execute(e);
        }


    }
}
