using PDF_Office.ViewModels.PropertyPanel;
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.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.PDFEdit
{
    /// <summary>
    /// TextEditProperty.xaml 的交互逻辑
    /// </summary>
    public partial class TextEditProperty : UserControl
    {
        public TextEditPropertyViewModel ViewModel => DataContext as TextEditPropertyViewModel;
        public TextEditProperty()
        {
            InitializeComponent();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            InitEventHandler();
            InitVariable();
        }

        private void InitEventHandler()
        {
            FontFamilyBox.SelectionChanged -= FontFamilyBox_SelectionChanged;
            FontStyleBox.SelectionChanged -= FontStyleBox_SelectionChanged;
            FontSizeBox.SelectionChanged -= FontSizeBox_SelectionChanged;

            FontColorBox.SelectedColorHandler -= FontColorBox_SelectedColorHandler;

            FontFamilyBox.SelectionChanged += FontFamilyBox_SelectionChanged;
            FontStyleBox.SelectionChanged += FontStyleBox_SelectionChanged;
            FontSizeBox.SelectionChanged += FontSizeBox_SelectionChanged;

            FontColorBox.SelectedColorHandler += FontColorBox_SelectedColorHandler;

            if(ViewModel != null)
            {
                ViewModel.ClearCheckedAglin -= ViewModel_ClearCheckedAglin;
                ViewModel.ClearCheckedAglin += ViewModel_ClearCheckedAglin;
            }
        }

        private void InitVariable()
        {
            FontTitleBox.SelectedIndex = 0;
            FontFamilyBox.SelectedIndex = 0;
            FontStyleBox.SelectedIndex = 0;
            FontSizeBox.SelectedIndex = 0;
        }

        private void ViewModel_ClearCheckedAglin(object sender, EventArgs e)
        {
            foreach (var item in LayoutAlignGrid.Children)
            {
                var radioBtn = item as RadioButton;
                if (radioBtn != null)
                {
                    radioBtn.IsChecked = false;
                }
            }
        }

        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 FontColorBox_SelectedColorHandler(object sender, Color e)
        {
            var data = this.DataContext as TextEditPropertyViewModel;
            if (data != null)
            {
                data.SelectedColorCommand?.Execute(e);
            }

        }
        
        private void BtnTextAlign_Click(object sender, RoutedEventArgs e)
        {

        }

       
    }
}
