using C1.Xaml.OrgChart;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Media;

// The User Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234236

namespace ControlExplorer.Controls.OrgChart
{
    public sealed partial class CollapseExpand : UserControl
    {
        public CollapseExpand()
        {
            this.InitializeComponent();
            CreateData();
        }

        // collapse the chart to level 1
        void Button_Collapse_Click(object sender, RoutedEventArgs e)
        {
            ToggleCollapseExpand(_orgChart, 0, 1);
        }


        // collapse the chart to a given level
        void ToggleCollapseExpand(C1OrgChart node, int level, int maxLevel)
        {
            //ToggleButton button = null;
            if (level >= maxLevel)
            {
                node.IsCollapsed = true;
                // Get ToggleButton and set its IsCheced property to true.
                SetToggleButtonState(node, true);
            }
            else
            {
                node.IsCollapsed = false;
                SetToggleButtonState(node, false);
                foreach (var subNode in node.ChildNodes)
                {
                    ToggleCollapseExpand(subNode, level + 1, maxLevel);
                }
            }
        }

        // create some random data and assign it to the chart
        void CreateData()
        {
            var p = Person.CreatePerson(3);
            _orgChart.Header = p;
        }


        /// <summary>
        /// Get ToggleButton from orgChart and set its IsChecked property to isChecked;
        /// </summary>
        private void SetToggleButtonState(DependencyObject chart, bool isChecked)
        {
            ToggleButton button = null;
            int count = VisualTreeHelper.GetChildrenCount(chart);
            for (int index = 0; index < count; index++)
            {
                var child = VisualTreeHelper.GetChild(chart, index);
                if (child is ToggleButton)
                {
                    button = (ToggleButton)child;
                    button.IsChecked = isChecked;
                }
                else
                {
                    SetToggleButtonState(child, isChecked);
                }
            }
        }

        private void CheckedChanged(object sender)
        {
            ToggleButton toggleButton = sender as ToggleButton;
            FrameworkElement parent = VisualTreeHelper.GetParent(toggleButton) as FrameworkElement;
            while (parent != null && !(parent is C1OrgChart))
            {
                parent = VisualTreeHelper.GetParent(parent) as FrameworkElement;
            }
            if (parent != null)
            {
                C1OrgChart orgChart = parent as C1OrgChart;
                if (toggleButton.IsChecked != null)
                {
                    orgChart.IsCollapsed = toggleButton.IsChecked.Value;
                }
            }
        }

        private void ToggleButton_Click(object sender, RoutedEventArgs e)
        {
            CheckedChanged(sender);
        }
    }
}
