using System;
using System.ComponentModel;
using System.Collections;
using System.Diagnostics;
using C1.Data;
using Northwind.DataObjects;

namespace Northwind
{
    /// <summary>
    /// C1SchemaDef container class: main class of the data library.
    /// Contains the C1SchemaDef component managing the data schema. 
    /// </summary>
    public class DataClass : System.ComponentModel.Component
    {
        /// <summary>
        /// C1SchemaDef component managing the data schema.
        /// Needs to be public in order to allow access to the library.
        /// </summary>
        public C1.Data.C1SchemaDef schemaDef;
        private C1.Data.C1TableLogic table_Customers;
        private C1.Data.C1TableLogic table_Order_Details;
        private C1.Data.C1DataSetLogic dataset_ProductsOrders;

        public DataClass(System.ComponentModel.IContainer container)
        {
            /// <summary>
            /// Required for Windows.Forms Class Composition Designer support
            /// </summary>
            container.Add(this);
            InitializeComponent();
        }

        public DataClass()
        {
            /// <summary>
            /// Required for Windows.Forms Class Composition Designer support
            /// </summary>
            InitializeComponent();
        }

        #region Component Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(DataClass));
            this.schemaDef = new C1.Data.C1SchemaDef();
            this.table_Customers = new C1.Data.C1TableLogic();
            this.table_Order_Details = new C1.Data.C1TableLogic();
            this.dataset_ProductsOrders = new C1.Data.C1DataSetLogic();
            ((System.ComponentModel.ISupportInitialize)(this.schemaDef)).BeginInit();
            // 
            // schemaDef
            // 
            this.schemaDef.DummyImage = ((System.Drawing.Bitmap)(resources.GetObject("schemaDef.DummyImage")));
            this.schemaDef.CreateSchema += new System.EventHandler(this.schemaDef_CreateSchema);
            this.schemaDef.SerializedSchemaXML = this.schemaDef.GetSchemaFromResources(resources, "schemaDef", this);
            this.schemaDef.SerializedDesignTimeOptionsXML = this.schemaDef.GetDesignTimeOptionsFromResources(resources, "schemaDef", this);
            // 
            // table_Customers
            // 
            this.table_Customers.SchemaComponent = this.schemaDef;
            this.table_Customers.SchemaPersist = this.schemaDef;
            this.table_Customers.Table = "Customers";
            this.table_Customers.BeforeFieldChange += new C1.Data.FieldChangeEventHandler(this.table_Customers_BeforeFieldChange);
            // 
            // table_Order_Details
            // 
            this.table_Order_Details.SchemaComponent = this.schemaDef;
            this.table_Order_Details.SchemaPersist = this.schemaDef;
            this.table_Order_Details.Table = "Order Details";
            this.table_Order_Details.BeforeFieldChange += new C1.Data.FieldChangeEventHandler(this.table_Order_Details_BeforeFieldChange);
            this.table_Order_Details.AfterFieldChange += new C1.Data.FieldChangeEventHandler(this.table_Order_Details_AfterFieldChange);
            // 
            // dataset_ProductsOrders
            // 
            this.dataset_ProductsOrders.DataSetDef = "ProductsOrders";
            this.dataset_ProductsOrders.SchemaComponent = this.schemaDef;
            this.dataset_ProductsOrders.SchemaPersist = this.schemaDef;
            this.dataset_ProductsOrders.AfterAddNew += new C1.Data.RowChangeEventHandler(this.dataset_ProductsOrders_AfterAddNew);
            ((System.ComponentModel.ISupportInitialize)(this.schemaDef)).EndInit();

        }
        #endregion

        private void table_Customers_BeforeFieldChange(object sender, C1.Data.FieldChangeEventArgs e)
        {
            if (e.Field.Name == "CustomerID")
                e.NewValue = ((string)e.NewValue).ToUpper();
        }

        private void table_Order_Details_BeforeFieldChange(object sender, C1.Data.FieldChangeEventArgs e)
        {
            if (e.Field.Name == "Quantity")
            {
                if ((short)e.NewValue < 1)
                    e.NewValue = (short)1;
            }
            else if (e.Field.Name == "Discount")
            {
                if ((float)e.NewValue < 0 || (float)e.NewValue > 1)
                    throw new ApplicationException("Discount must be between 0 and 1");
            }
        }

        private void table_Order_Details_AfterFieldChange(object sender, C1.Data.FieldChangeEventArgs e)
        {
            if (e.Field.Name == "Quantity")
            {
                Order_DetailsRow orderDetail = Order_DetailsRow.Obj(e.Row);
                ProductsRow product = orderDetail.GetProductsRow();
                if (product != null)
                {
                    short unitsOrdered = (short)((short)e.NewValue - (e.OldValue == Convert.DBNull ? (short)0 : (short)e.OldValue));
                    product.UnitsInStock = (short)(product.UnitsInStock - unitsOrdered);
                    if (product.UnitsInStock < 0)
                        product.UnitsInStock = 0;
                    short newUnitsOnOrder = product.UnitsOnOrder;
                    newUnitsOnOrder += unitsOrdered;
                    if (newUnitsOnOrder < 0)
                        newUnitsOnOrder = 0;
                    product.UnitsOnOrder = newUnitsOnOrder;
                }
            }
        }

        private void dataset_ProductsOrders_AfterAddNew(object sender, C1.Data.RowChangeEventArgs e)
        {
            if (e.DataTable.SchemaTable.Name == "CustOrdersDetails")
            {
                CustOrdersDetailsRow_tableView order = CustOrdersDetailsRow_tableView.Obj(e.Row);
                if (order.IsShipNameNull())
                    order.ShipName = order.CompanyName;
                if (order.IsShipAddressNull())
                    order.ShipAddress = order.Address;
                if (order.IsShipCityNull())
                    order.ShipCity = order.City;
                if (order.IsShipRegionNull())
                    order.ShipRegion = order.Region;
                if (order.IsShipPostalCodeNull())
                    order.ShipPostalCode = order.PostalCode;
                if (order.IsShipCountryNull())
                    order.ShipCountry = order.Country;
            }
        }

        #region Switch to C1NWind.mdb database

        private void schemaDef_CreateSchema(object sender, EventArgs e)
        {
            string dbPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal) +
                "\\ComponentOne Samples\\Common\\C1NWind.mdb";
            if (System.IO.File.Exists(dbPath))
            {
                C1.Data.SchemaObjects.Connection conn = schemaDef.Schema.Connections[0];
                conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dbPath;
            }
        }

        #endregion

    }
}
