using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using Telerik.Windows.Controls;
using Telerik.Windows.Data;
using Telerik.Windows.Controls.GridView;

namespace MultipleChildDataInTabControl
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();

            GridViewTableDefinition ordersDefinition = new GridViewTableDefinition();
            ordersDefinition.Relation = new PropertyRelation("Orders");
            ordersDefinition.Relation.Name = "Orders";


            GridViewTableDefinition cancelledOrdersDefinition = new GridViewTableDefinition();
            cancelledOrdersDefinition.Relation = new PropertyRelation("CancelledOrders");
            cancelledOrdersDefinition.Relation.Name = "CancelledOrders";

            this.radGridView1.TableDefinition.ChildTableDefinitions.Add(ordersDefinition);
            this.radGridView1.TableDefinition.ChildTableDefinitions.Add(cancelledOrdersDefinition);
            this.radGridView1.ItemsSource = GetSampleHierarchy();
        }

        public static IList GetSampleHierarchy()
        {
            Func<int, List<Order>> ordersGen = customerIndex =>
            {
                var q = from i in Enumerable.Range(1, 10)
                        select new Order
                        {
                            ID = (10 * customerIndex) + i,
                            Description = "Order " + (10 * customerIndex + i)
                        };

                return q.ToList();
            };

            Func<int, List<Order>> cancelledOrdersGen = customerIndex =>
            {
                var q = from i in Enumerable.Range(1, 5)
                        select new Order
                        {
                            ID = (20 * customerIndex) + i,
                            Description = "Order " + (20 * customerIndex + i)
                        };

                return q.ToList();
            };

            var t = from i in Enumerable.Range(1, 10)
                    select new Customer
                    {
                        Age = i,
                        JobCode = i,
                        Name = "Customer " + i,
                        Orders = ordersGen(i),
                        CancelledOrders = cancelledOrdersGen(i)
                    };

            return t.ToList();
        }
    }
}
