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 Microsoft.Phone.Controls;

namespace PhoneAppOrders
{
    public partial class MainPage : PhoneApplicationPage
    {
        OrdersDataContext odc;
        // Constructor
        public MainPage()
        {
            InitializeComponent();

            
            odc = new OrdersDataContext("Data Source=isostore:/Orders.sdf");
            
            if (odc.DatabaseExists())
            {
                odc.DeleteDatabase();
            }
            odc.CreateDatabase();

            this.DataContext = odc;

            CreateDBEntries();

            lbCustomers.ItemsSource = odc.Customers;

        }

        private void CreateDBEntries()
        {
            Customer c = new Customer
            {
                CustomerID = "MMMC",
                CompanyName = "MyCompany",
                ContactName = "Max Mustermann",
                Address = "Teststr. 17",
                City = "Testcity"
            };
            odc.Customers.InsertOnSubmit(c);

            c = new Customer
            {
                CustomerID = "MOMC",
                CompanyName = "MyOtherCompany",
                ContactName = "Jens Musterfeld",
                Address = "Test Allee 23",
                City = "Hausentest"
            };
            odc.Customers.InsertOnSubmit(c);

            Product p = new Product
            {
                ProductName = "Testproduct1"
            };
            odc.Products.InsertOnSubmit(p);
            
            p = new Product
            {
                ProductName = "Testproduct2"
            };
            odc.Products.InsertOnSubmit(p);
            
            p = new Product
            {
                ProductName = "Testproduct3"
            };
            odc.Products.InsertOnSubmit(p);

            p = new Product
            {
                ProductName = "Testproduct4"
            };
            odc.Products.InsertOnSubmit(p);
            odc.SubmitChanges();

            Order o = new Order
            {
                Customer = odc.Customers.ToList()[0],
                OrderDate = DateTime.Now
            };
            odc.Orders.InsertOnSubmit(o);

            OrderDetail od = new OrderDetail
            {
                Order = o,
                Product = odc.Products.ToList()[0],
                Quantity = 1,
                UnitPrice = 1
            };
            odc.OrderDetails.InsertOnSubmit(od);

            od = new OrderDetail
            {
                Order = o,
                Product = odc.Products.ToList()[1],
                Quantity = 1,
                UnitPrice = 1
            };
            odc.OrderDetails.InsertOnSubmit(od);

            od = new OrderDetail
            {
                Order = o,
                Product = odc.Products.ToList()[2],
                Quantity = 1,
                UnitPrice = 1
            };
            odc.OrderDetails.InsertOnSubmit(od);

            o = new Order
            {
                Customer = odc.Customers.ToList()[1],
                OrderDate = DateTime.Now
            };
            odc.Orders.InsertOnSubmit(o);

            od = new OrderDetail
            {
                Order = o,
                Product = odc.Products.ToList()[1],
                Quantity = 1,
                UnitPrice = 1
            };
            odc.OrderDetails.InsertOnSubmit(od);

            od = new OrderDetail
            {
                Order = o,
                Product = odc.Products.ToList()[2],
                Quantity = 1,
                UnitPrice = 1
            };
            odc.OrderDetails.InsertOnSubmit(od);

            od = new OrderDetail
            {
                Order = o,
                Product = odc.Products.ToList()[3],
                Quantity = 1,
                UnitPrice = 1
            };
            odc.OrderDetails.InsertOnSubmit(od);

            o = new Order
            {
                Customer = odc.Customers.ToList()[0],
                OrderDate = DateTime.Now
            };
            odc.Orders.InsertOnSubmit(o);

            od = new OrderDetail
            {
                Order = o,
                Product = odc.Products.ToList()[1],
                Quantity = 1,
                UnitPrice = 1
            };
            odc.OrderDetails.InsertOnSubmit(od);

            od = new OrderDetail
            {
                Order = o,
                Product = odc.Products.ToList()[3],
                Quantity = 1,
                UnitPrice = 1
            };
            odc.OrderDetails.InsertOnSubmit(od);
            odc.SubmitChanges();
        }
    }
}