using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Telerik.Windows.Controls;
using System.Collections.ObjectModel;
using Telerik.Windows.Controls.GridView;

namespace TreeGridHybridUsingRowDetails_WPF
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        private ObservableCollection<Person> persons;
        public Window1()
        {
            InitializeComponent();
            persons = GetSampleObservableCollectionOfPersons();
            this.RadGridView1.ItemsSource = from p in persons
                                            where p.Parent == null
                                            select p;

            this.RadGridView1.RowLoaded += new EventHandler<Telerik.Windows.Controls.GridView.RowLoadedEventArgs>(RadGridView1_RowLoaded);
        }
        void RadGridView1_RowLoaded(object sender, Telerik.Windows.Controls.GridView.RowLoadedEventArgs e)
        {
            if (!(e.Row is GridViewHeaderRow))
            {
                e.Row.SetBinding(GridViewRow.DetailsVisibilityProperty, new Binding("IsExpanded") { Source = e.Row.DataContext, Mode = BindingMode.TwoWay });
            }
            GridViewCell c;
        }

        const int NUMBER_OF_GROUPS = 8;
        const int NUMBER_OF_SUB_GROUPS = 10;
        private int NUMBER_OF_EMPLOYEES_IN_EACH_GROUP = 20;
        private ObservableCollection<Person> GetSampleObservableCollectionOfPersons()
        {
            ObservableCollection<Person> persons = new ObservableCollection<Person>();
            for (int groupIndex = 0; groupIndex < NUMBER_OF_GROUPS; groupIndex++)
            {
                Person group = new Person(groupIndex, "Group " + groupIndex, "G" + groupIndex);
                persons.Add(group);
                for (int subGroupIndex = 0; subGroupIndex < NUMBER_OF_SUB_GROUPS; subGroupIndex++)
                {
                    Person subGroup = new Person(subGroupIndex, "Sub Group " + subGroupIndex, "S G " + subGroupIndex);
                    subGroup.Parent = group;
                    persons.Add(subGroup);
                    NUMBER_OF_EMPLOYEES_IN_EACH_GROUP = (NUMBER_OF_EMPLOYEES_IN_EACH_GROUP == 20) ? 3 : 20;
                    for (int employeeIndex = 0; employeeIndex < NUMBER_OF_EMPLOYEES_IN_EACH_GROUP; employeeIndex++)
                    {
                        Person employee = new Person(employeeIndex, "Employee " + employeeIndex, "E" + employeeIndex);
                        employee.Parent = subGroup;
                        persons.Add(employee);
                    }
                }
            }

            return persons;
        }

        private void subgrid_Loaded(object sender, RoutedEventArgs e)
        {
            Person person = ((FrameworkElement)sender).DataContext as Person;

            ((RadGridView)sender).ItemsSource = from p in persons
                                                where p.Parent == person
                                                select p;
        }
    }
}
