using System;
using System.Collections.Generic;
using System.ComponentModel;
using Windows.UI.Xaml.Controls;
using System.Linq;
using Windows.UI.Xaml;
using Microsoft.Xaml.Interactivity;
using C1.Xaml;

namespace ControlExplorer.Controls.TreeView
{
    public sealed partial class CheckBox : UserControl
    {
        public CheckBox()
        {
            this.InitializeComponent();

        }

       
    }
    public class C1CheckBoxTreeViewBehavior : DependencyObject, IBehavior
    {
        public bool AutoCheck
        {
            get { return (bool)GetValue(AutoCheckProperty); }
            set { SetValue(AutoCheckProperty, value); }
        }

        // Using a DependencyProperty as the backing store for AutoCheck.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty AutoCheckProperty =
            DependencyProperty.Register("AutoCheck", typeof(bool), typeof(C1CheckBoxTreeViewBehavior), new PropertyMetadata(false));


        public DependencyObject AssociatedObject

        { get; private set; }

        public void Attach(DependencyObject associatedObject)
        {
            if (associatedObject != AssociatedObject)
            {
                AssociatedObject = associatedObject;
                var treeView = AssociatedObject as C1.Xaml.C1TreeView;
                if (treeView != null)
                {
                    treeView.ItemClick += treeView_ItemClick;
                }
            }
        }

        void treeView_ItemClick(object sender, C1.Xaml.SourcedEventArgs e)
        {
            if (AutoCheck)
            {
                var treeView = sender as C1.Xaml.C1TreeView;
                if (treeView != null)
                {
                    var treeViewItem = treeView.SelectedItem;
                    if (treeViewItem != null)
                    {
                        ChangeChildNodes(treeViewItem);
                        ChangedParentNodes(treeViewItem);
                    }
                }
            }

        }

        public void Detach()
        {
            if (AssociatedObject != null)
            {
                var treeView = AssociatedObject as C1.Xaml.C1TreeView;
                if (treeView != null)
                {
                    treeView.ItemClick -= treeView_ItemClick;
                }
                AssociatedObject = null;
            }

        }

        void ChangeChildNodes(C1TreeViewItem CurrentNode)
        {
            var checkbox = CurrentNode.Header as Windows.UI.Xaml.Controls.CheckBox;
            
            if (checkbox != null)
            {
                if (checkbox.IsChecked != null && CurrentNode.Items != null)
                {


                    foreach (var item in CurrentNode.Items)
                    {
                        var treeViewItem = item as C1TreeViewItem;
                        if (treeViewItem != null)
                        {
                            var childCheckBox = treeViewItem.Header as Windows.UI.Xaml.Controls.CheckBox;
                            if (childCheckBox != null)
                            {
                                childCheckBox.IsChecked = checkbox.IsChecked;
                            }
                            if (treeViewItem.Items != null)
                            {
                                ChangeChildNodes(treeViewItem);
                            }
                        }

                    }
                }
            }

        }
        void ChangedParentNodes(C1TreeViewItem CurrentNode)
        {
            if (CurrentNode.ParentItem != null && CurrentNode.ParentItem.Items != null)
            {
                bool? parentNodeState = true;
                int selectedCount = 0;
                int noSelectedCount = 0;

                foreach (var data in CurrentNode.ParentItem.Items)
                {
                    var treeViewItem = data as C1TreeViewItem;

                    if (treeViewItem != null)
                    {

                        var childCheckBox = treeViewItem.Header as Windows.UI.Xaml.Controls.CheckBox;
                        if (childCheckBox != null)
                        {
                            if (childCheckBox.IsChecked == true)
                            {
                                selectedCount++;
                            }
                            else if (childCheckBox.IsChecked == false)
                            {
                                noSelectedCount++;
                            }
                        }
                    }

                }


                if (selectedCount ==
                    CurrentNode.ParentItem.Items.Count)
                {
                    parentNodeState = true;
                }

                else if (noSelectedCount ==
                    CurrentNode.ParentItem.Items.Count)
                {
                    parentNodeState = false;
                }

                else
                {
                    parentNodeState = null;
                }
                var parentCheckBox = CurrentNode.ParentItem.Header as Windows.UI.Xaml.Controls.CheckBox;
                if (parentCheckBox != null)
                {
                    parentCheckBox.IsChecked = parentNodeState;
                }


                if (CurrentNode.ParentItem.ParentItem != null)
                {
                    ChangedParentNodes(CurrentNode.ParentItem);
                }

            }
        }
    }
}
