using System;
using System.Collections.Generic;
using IdeaBlade.EntityModel;

namespace MvvmLightDemo
{
    /// <summary>
    /// Interface for Mvvm Light demo repository
    /// </summary>
    public interface IDemoDataService
    {

        /// <summary>
        /// Load all customers asynchronously
        /// </summary>
        /// <param name="success">Action to perform with the results of the query when it succeeds.</param>
        /// <param name="fail">Action to perform with the query exception if fails.</param>
        void LoadCustomers(
            Action<IEnumerable<Customer>> success = null,
            Action<Exception> fail = null);

    }

    /// <summary>
    /// Production repository for Mvvm Light demo
    /// </summary>
    public class DemoDataService : IDemoDataService
    {
        public DemoDataService()
        {
            Manager = new NorthwindIBEntities();
        }

        protected NorthwindIBEntities Manager { get; set; }

        public void LoadCustomers(
            Action<IEnumerable<Customer>> success = null,
            Action<Exception> fail = null)
        {

            Manager.Customers
              .OrderBy(c => c.CompanyName)
              .ExecuteAsync(op =>
              {
                  if (op.CompletedSuccessfully)
                  {
                      if (null != success)
                      {
                          success(op.Results);
                      }
                  }
                  else
                  {
                      if (null != fail)
                      {
                          op.MarkErrorAsHandled();
                          fail(op.Error);
                      }
                  }
              });
        }

    }

    /// <summary>
    /// Design time repository for Mvvm Light demo
    /// </summary>
    public class DesignDemoDataService: IDemoDataService
    {
        public void LoadCustomers(
              Action<IEnumerable<Customer>> success = null,
              Action<Exception> fail = null)
        {
            if (null != success) success(CreateDesignCustomers());
        }

        private static void CreateDesignOrders(Customer cust)
        {
            var orders = new List<Order>();
            var orderCounter = 1;
            while (orderCounter <= 3)
            {
                orders.Add(new Order
                {
                    OrderID = orderCounter,
                    ShipCity = "ShipCity " + orderCounter++,
                    OrderDate = new DateTime(2012, 09, orderCounter),
                    Customer = cust
                });
            }
        }

        /// <summary>Create collection of fake design-time customers</summary>
        private static IEnumerable<Customer> CreateDesignCustomers()
        {
            var custs = new List<Customer>();

            // First customer is for full detail customer design 
            var cust = new Customer
            {
                CustomerID = Guid.NewGuid(),
                CompanyName = "The Design Width Company Name",
                Address = "123 Main Street",
                City = "Anytown",
                ContactName = "Harry Fidurcci",
                Phone = "(510) 555 1212 x10",
                Country = "France",
            };
            CreateDesignOrders(cust);
            custs.Add(cust);

            // 10 dummy customers for listboxes
            var custCounter = 1;
            while (custCounter <= 10)
            {
                var cust2 = new Customer
                           {
                               CustomerID = Guid.NewGuid(), 
                               CompanyName = "Customer " + custCounter++, 
                               Country = "USA"
                           };
                custs.Add(cust2);
            }

            return custs;
        }
    }
}
