using PDF_Office.CustomControl;
using PDF_Office.Helper;
using PDF_Office.Model.PDFTool;
using PDF_Office.ViewModels.HomePanel;
using PDF_Office.ViewModels.HomePanel.PDFTools;
using PDF_Office.Views.HomePanel.PDFTools;
using PDFSettings;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace PDF_Office.Views.HomePanel.PDFTools
{
    /// <summary>
    /// HomeToolIControl.xaml 的交互逻辑0
    /// 
    /// </summary>
    public partial class QuickToolsContent : UserControl
    {
        PDFToolsHelper PDFtools;
        private QuickToolsContentViewModel ViewModel => DataContext as QuickToolsContentViewModel;
        public QuickToolsContent()
        {
            InitializeComponent();
            PDFtools = PDFToolsHelper.GetInstance();
            ListBoxToolBars.ItemsSource = PDFtools.QuickTools;
            ListBoxExpendToolBars.ItemsSource = PDFtools.QuickTools;
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            if (ViewModel != null)
            {
                ViewModel.ExpendToolsHanlder -= ViewModel_ExpendToolsHanlder;
                ViewModel.ExpendToolsHanlder += ViewModel_ExpendToolsHanlder;
            }
            if (ListBoxToolBars.Items != null)
            {
                UpdateExptendUI(ViewModel.IsExpendTools);
                ListBoxToolBars.Items.Refresh();
                ListBoxToolBarsWidthChanged();
                ListBoxExpendToolBarsWidthChanged();
            }
           
        }

        private void ViewModel_ExpendToolsHanlder(object sender, bool e)
        {
            UpdateToolBarsWidth();
            UpdateExptendUI(e);

        }

        private void UpdateExptendUI(bool e)
        {
            if (e)
            {
                GridAllTools.Height = 270;
            }
            else
            {
                GridAllTools.Height = 174;
            }
        }


        private void GridAllTools_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            UpdateToolBarsWidth();
        }

        private void UpdateToolBarsWidth()
        {
            ListBoxToolBarsWidthChanged();
            ListBoxExpendToolBarsWidthChanged();
        }

        private void ListBoxToolBarsWidthChanged()
        {
            if (ListBoxToolBars.Visibility == Visibility.Collapsed)
                return;

            double containerWidth = ListBoxToolBars.ActualWidth - 20;
            int widthItem = (int)containerWidth / 5;
            double margin = widthItem / 3;

            int i = 0;
            foreach (var item in ListBoxToolBars.Items)
            {
                i++;
                var listBoxItem = ListBoxToolBars.ItemContainerGenerator.ContainerFromItem(item) as ListBoxItem;
                if (listBoxItem != null)
                {
                    if (i % 4 != 0)
                    {
                        listBoxItem.Margin = new Thickness(0, 0, margin, 0);

                    }
                    else
                    {
                        listBoxItem.Margin = new Thickness(0, 0, 0, 0);
                    }
                    listBoxItem.Width = widthItem;
                }
            }


        }

        private void ListBoxExpendToolBarsWidthChanged()
        {
            if (ListBoxExpendToolBars.Visibility == Visibility.Collapsed)
                return;

            double containerWidth = ListBoxExpendToolBars.ActualWidth - 20;
            int widthItem = (int)containerWidth / 5;
            double margin = widthItem / 3;

            int i = 0;
            foreach (var item in ListBoxExpendToolBars.Items)
            {
                i++;
                var listBoxItem = ListBoxExpendToolBars.ItemContainerGenerator.ContainerFromItem(item) as ListBoxItem;
                if (listBoxItem != null)
                {
                    if (i % 4 != 0)
                    {
                        listBoxItem.Margin = new Thickness(0, 0, margin, 0);

                    }
                    else
                    {
                        listBoxItem.Margin = new Thickness(0, 0, 0, 0);
                    }
                    listBoxItem.Width = widthItem;
                }
            }


        }

        private void QuickTools_Click(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            var toolItem = (sender as FrameworkElement).DataContext as ToolItem;
            var veiwModel = this.DataContext as QuickToolsContentViewModel;
            if (veiwModel != null && toolItem != null)
            {
                veiwModel.QuickToolsCommand.Execute(toolItem);
            }
        }

       
    }

}
