using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using BeWo.Core;

namespace BeWo.View.Controls
{
    /// <summary>
    /// Interaktionslogik für ComboBoxTreeViewControlV2.xaml
    /// </summary>
    public partial class ComboBoxTreeViewControlV2 : UserControl
    {
        public List<ServiceCategoryTreeItem> TreeViewSourceList
        {
            get { return (List<ServiceCategoryTreeItem>)GetValue(ListProperty); }
            set
            {
                SetValue(ListProperty, value);

                ShowCheckedValues();
            }
        }

        public double ParentWindowHeight
        {
            get { return (double)GetValue(ParentWindowHeightProperty); }
            set { SetValue(ParentWindowHeightProperty, value); }
        }

        public static readonly DependencyProperty ParentWindowHeightProperty =
            DependencyProperty.Register("ParentWindowHeight",
                                        typeof(double),
                                        typeof(ComboBoxTreeViewControlV2),
                                        new FrameworkPropertyMetadata(500.0));

        public static readonly DependencyProperty ListProperty =
            DependencyProperty.Register("TreeViewSourceList",
                                        typeof(List<ServiceCategoryTreeItem>),
                                        typeof(ComboBoxTreeViewControlV2),
                                        new FrameworkPropertyMetadata(new List<ServiceCategoryTreeItem>()));

        public ComboBoxTreeViewControlV2()
        {
            InitializeComponent();
            DataContext = this;
        }

        private void ShowCheckedValues()
        {
            string ToolTipString = string.Empty;
            TreeViewSourceList.Aggregate(string.Empty,(current1, item) =>item.Children.Where(item2 => item2.IsChecked).Aggregate(current1,(current, item2) =>current + (item2.Name + "; ")));

            bool allSelected = true;
            bool noneSelected = true;

            foreach (var item in TreeViewSourceList)
            {
                if (item.IsChecked)
                    noneSelected = false;
                else
                    allSelected = false;

                bool allSubselected = true;
                bool noneSubselected = true;

                string children = string.Empty;
                foreach (var child in item.Children)
                {
                    if (child.IsChecked)
                    {
                        noneSelected = false;
                        noneSubselected = false;
                        children += string.Format("{0}; ", child.Name);
                    }
                    else
                    {
                        allSelected = false;
                        allSubselected = false;
                    }
                }

                if (allSubselected && item.Children.Count > 0)
                    item.IsChecked = true;

                if (noneSubselected && item.Children.Count > 0)
                    item.IsChecked = false;

                if (!allSubselected)
                    ToolTipString += children;
                else if(item.IsChecked)
                    ToolTipString += string.Format("{0}; ", item.Name);
            }

            if (allSelected)
                ToolTipString = "Alle";
            else if (noneSelected)
                ToolTipString = string.Empty;

            DeSelectAllCheckBox.IsChecked = allSelected;

            Imitationheader.Text = ToolTipString;

            if (string.IsNullOrWhiteSpace(ToolTipString))
            {
                Imitationheader.ToolTip = null;
                return;
            }

            // Formatierung des Tooltips
            string[] splittedToolTip = ToolTipString.Split(';');
            ToolTipString = string.Empty;

            foreach (var item in splittedToolTip.Where(x => x != " "))
            {
                ToolTipString += item.Trim(' ');
                if (splittedToolTip.Where(x => x != " ").ToList().IndexOf(item) <
                    (splittedToolTip.Count(x => x != " ") - 1))
                    ToolTipString += "\n";
            }

            Imitationheader.ToolTip = ToolTipString;
        }

        private void OpenPopUpBtnClick(object sender, EventArgs e)
        {
            openPopup();
        }

        private void openPopup()
        {
            TreeViewPopup.Placement = PlacementMode.RelativePoint;
			TreeViewPopup.PlacementTarget = Imitationheader;
            TreeViewPopup.VerticalOffset = Imitationheader.Height;
            TreeViewPopup.HorizontalOffset = 0;
            TreeViewPopup.MinWidth = Imitationheader.Width;
            TreeViewPopup.IsOpen = true;

            OpenPopUpBtn.IsEnabled = false;
        }

        private void PopUpClosedEvent(object sender, EventArgs eventArgs)
        {
            ShowCheckedValues();
            cbtv_search.Text = string.Empty;
            OpenPopUpBtn.IsEnabled = true;
        }

        private void ApplyBtnClose(object sender, EventArgs e)
        {
            TreeViewPopup.IsOpen = false;
        }

        public void ResetControl()
        {
            TreeViewSourceList = new List<ServiceCategoryTreeItem>();
            Imitationheader.Text = string.Empty;
            Imitationheader.ToolTip = null;
            DeSelectAllCheckBox.IsChecked = false;
        }

        private void Tree1_OnLoaded(object sender, RoutedEventArgs e)
        {
            if (ParentWindowHeight >= TreeViewPopup.VerticalOffset)
                TreeViewPopup.MaxHeight = ParentWindowHeight - TreeViewPopup.VerticalOffset - ParentWindowHeight / 4;
        }

        private void Bd_OnClick(object sender, RoutedEventArgs e)
        {
            ShowCheckedValues();
        }

        private void DeSelectAllOnClick(object sender, RoutedEventArgs e)
        {
            var cb = (CheckBox) sender;

            if (!cb.IsChecked.HasValue)
                return;
            
            foreach (var item in TreeViewSourceList)
            {
                item.IsChecked = cb.IsChecked.Value;
                foreach(var item2 in item.Children)
                    item2.IsChecked = cb.IsChecked.Value;
            }

            ShowCheckedValues();
        }

        private void BdText_OnMouseDown(object sender, MouseButtonEventArgs e)
        {
            var TreeItem = ((TextBlock)sender).Tag as ServiceCategoryTreeItem;

            TreeItem.IsChecked = !TreeItem.IsChecked;
            ShowCheckedValues();
        }
    }
}
