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 BS.Shared;
using BS.Shared.DataContracts;
using BS.Shared.DataContracts.ClientPartials;

namespace BeWo.View.Controls
{
    /// <summary>
    /// Interaktionslogik für ComboBoxTreeViewControl.xaml
    /// </summary>
    public partial class ComboBoxTreeViewControl
    {
        public double ParentWindowHeight
        {
            get { return (double) GetValue(ParentWindowHeightProperty); }
            set { SetValue(ParentWindowHeightProperty, value); }
        }

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


        public List<SupportConceptGoalDC> TreeViewSourceList
        {
            get { return (List<SupportConceptGoalDC>)GetValue(ListProperty); }
            set
            {
                SetValue(ListProperty, value);
                ShowCheckedValues();
            }
        }

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

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

        private void ShowCheckedValues()
        {
            string ToolTipString;

	        GoalCount = 0;
            var selectedGoals = GetSelectedGoals();

            bool allSelected = selectedGoals.Count == GoalCount && GoalCount > 0;
            
            ToolTipString = selectedGoals.Aggregate(string.Empty, (s, dc) => s + (dc.TypeDescription + "; "));

            DeSelectAllCheckBox.IsChecked = allSelected;

            Imitationheader.Text = ToolTipString;

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

            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 List<ValueListEntryDC> GetSelectedGoals()
        {
            var goals = new List<ValueListEntryDC>();

            AddCheckedGoals(TreeViewSourceList, goals);

            return goals;
        }

        private int GoalCount;
        private void AddCheckedGoals(List<SupportConceptGoalDC> goals, List<ValueListEntryDC> selectedGoals)
        {
            foreach (SupportConceptGoalDC item in goals)
            {
                if (item.Children == null || item.Children.Count == 0)
                {
                    //if (item.GoalEntryDC.Type.Equals(ValueListEntryType.SupportConceptGoalType) || item.GoalEntryDC.Type.Equals(ValueListEntryType.SupportConceptIndividualGoalType))
                    //{
                        GoalCount++;
                        if (item.IsChecked)
                        {
                            selectedGoals.Add(item.GoalEntryDC);
                        }
                    //}
                }
                else
                {
                    AddCheckedGoals(item.Children, selectedGoals);
                }
            }
        }

        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;
			if (ParentWindowHeight == 0)
				ParentWindowHeight = 500;
            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<SupportConceptGoalDC>();
            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 SupportConceptGoal = ((TextBlock)sender).Tag as SupportConceptGoalDC;
            
            SupportConceptGoal.IsChecked = !SupportConceptGoal.IsChecked;
            ShowCheckedValues();
        }
    }
}
