using System;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using SilverlightGridHierarchyUsingMVVMAndServices.Web;
using System.Collections.Generic;
using System.Windows.Ria.Data;
using System.Reflection;

namespace SilverlightGridHierarchyUsingMVVMAndServices.Web
{
    public class MyDataContext
    {
        static NorthwindDomainContext _domainContext;
        public static NorthwindDomainContext DomainContext
        {
            get
            {
                if (_domainContext == null)
                {
                    _domainContext = new NorthwindDomainContext();
                }

                return _domainContext;
            }
        }

        bool customersLoaded = false;
        public IEnumerable<Customer> Customers
        {
            get
            {
                if (!customersLoaded)
                {
                    DomainContext.Load(DomainContext.GetCustomersQuery());

                    customersLoaded = true;
                }

                return DomainContext.Customers;
            }
        }
    }

    public partial class Customer
    {
        bool ordersLoaded = false;
        public IEnumerable<Order> CustomerOrders
        {
            get
            {
                if (!ordersLoaded)
                {
                    var query = from o in MyDataContext.DomainContext.GetOrdersQuery()
                                where o.CustomerID == CustomerID
                                select o;

                    MyDataContext.DomainContext.Load(query);

                    ordersLoaded = true;
                }

                return Orders;
            }
        }
    }

    public partial class Order
    {
        bool detailsLoaded = false;
        public IEnumerable<Order_Detail> Details
        {
            get
            {
                if (!detailsLoaded)
                {
                    var query = from o in MyDataContext.DomainContext.GetOrder_DetailsQuery()
                                where o.OrderID == OrderID
                                select o;

                    MyDataContext.DomainContext.Load(query);

                    detailsLoaded = true;
                }

                return Order_Details;
            }
        }
    }
}