using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using C1.Silverlight.OrgChart;

namespace CollapseExpand
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            CreateData();
        }

        // toggle OrgChart orientation when user clicks the checkbox on the main page
        void CheckBox_Click(object sender, RoutedEventArgs e)
        {
            _orgChart.Orientation = ((CheckBox)sender).IsChecked.Value
                ? Orientation.Horizontal
                : Orientation.Vertical;
        }

        // rebuild the chart using new random data
        void Button_Refresh_Click(object sender, RoutedEventArgs e)
        {
            CreateData();
        }

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

        // collapse the chart to a given level
        void CollapseExpand(C1OrgChart node, int level, int maxLevel)
        {
            if (level >= maxLevel)
            {
                node.IsCollapsed = true;
            }
            else
            {
                node.IsCollapsed = false;
                foreach (var subNode in node.ChildNodes)
                {
                    CollapseExpand(subNode, level + 1, maxLevel);
                }
            }
        }

        // create some random data and assign it to the chart
        void CreateData()
        {
            var p = Data.Person.CreatePerson(15);
            _tbTotal.Text = string.Format(" ({0} items total)", p.TotalCount);
            _orgChart.Header = p;
        }
    }
}
