using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using DataLibrary.DataObjects;

namespace UsingObjects
{
    /// <summary>
    /// Summary description for ProductForm.
    /// </summary>
    public class ProductForm : System.Windows.Forms.Form
    {
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.ComboBox cbCategory;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.TextBox edProductName;
        private System.Windows.Forms.Label label4;
        private System.Windows.Forms.ComboBox cbSupplier;
        private System.Windows.Forms.CheckBox chkDiscontinued;
        private System.Windows.Forms.Label label5;
        private System.Windows.Forms.TextBox edQuantityPerUnit;
        private System.Windows.Forms.Label label6;
        private System.Windows.Forms.NumericUpDown edReorderLevel;
        private System.Windows.Forms.Label label7;
        private System.Windows.Forms.TextBox edUnitPrice;
        private System.Windows.Forms.Label label8;
        private System.Windows.Forms.NumericUpDown edUnitsInStock;
        private System.Windows.Forms.Label label9;
        private System.Windows.Forms.NumericUpDown edUnitsOnOrder;
        private System.Windows.Forms.Label label10;
        private System.Windows.Forms.Button btnOk;
        private System.Windows.Forms.Button btnCancel;
        private System.Windows.Forms.PictureBox picCategory;
        private System.Windows.Forms.Label labSupplierCity;
        private System.Windows.Forms.Label labSupplierCountry;
        private System.Windows.Forms.Label label11;
        private System.Windows.Forms.Label label12;
        private System.ComponentModel.IContainer components = null;

        public ProductForm()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

            //
            // TODO: Add any constructor code after InitializeComponent call
            //
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose(disposing);
        }

        #region Windows Form 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()
        {
            this.label1 = new System.Windows.Forms.Label();
            this.cbCategory = new System.Windows.Forms.ComboBox();
            this.label2 = new System.Windows.Forms.Label();
            this.label3 = new System.Windows.Forms.Label();
            this.edProductName = new System.Windows.Forms.TextBox();
            this.label4 = new System.Windows.Forms.Label();
            this.cbSupplier = new System.Windows.Forms.ComboBox();
            this.chkDiscontinued = new System.Windows.Forms.CheckBox();
            this.label5 = new System.Windows.Forms.Label();
            this.edQuantityPerUnit = new System.Windows.Forms.TextBox();
            this.label6 = new System.Windows.Forms.Label();
            this.edReorderLevel = new System.Windows.Forms.NumericUpDown();
            this.label7 = new System.Windows.Forms.Label();
            this.edUnitPrice = new System.Windows.Forms.TextBox();
            this.label8 = new System.Windows.Forms.Label();
            this.edUnitsInStock = new System.Windows.Forms.NumericUpDown();
            this.label9 = new System.Windows.Forms.Label();
            this.edUnitsOnOrder = new System.Windows.Forms.NumericUpDown();
            this.label10 = new System.Windows.Forms.Label();
            this.btnOk = new System.Windows.Forms.Button();
            this.btnCancel = new System.Windows.Forms.Button();
            this.picCategory = new System.Windows.Forms.PictureBox();
            this.labSupplierCity = new System.Windows.Forms.Label();
            this.labSupplierCountry = new System.Windows.Forms.Label();
            this.label11 = new System.Windows.Forms.Label();
            this.label12 = new System.Windows.Forms.Label();
            ((System.ComponentModel.ISupportInitialize)(this.edReorderLevel)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.edUnitsInStock)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.edUnitsOnOrder)).BeginInit();
            this.SuspendLayout();
            // 
            // label1
            // 
            this.label1.Font = new System.Drawing.Font("Tahoma", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.label1.Location = new System.Drawing.Point(127, 10);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(124, 20);
            this.label1.TabIndex = 0;
            this.label1.Text = "Product Information";
            // 
            // cbCategory
            // 
            this.cbCategory.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.cbCategory.Location = new System.Drawing.Point(248, 52);
            this.cbCategory.MaxDropDownItems = 6;
            this.cbCategory.Name = "cbCategory";
            this.cbCategory.Size = new System.Drawing.Size(116, 21);
            this.cbCategory.TabIndex = 4;
            this.cbCategory.SelectedIndexChanged += new System.EventHandler(this.cbCategory_SelectedIndexChanged);
            // 
            // label2
            // 
            this.label2.Location = new System.Drawing.Point(248, 32);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(68, 16);
            this.label2.TabIndex = 3;
            this.label2.Text = "Category:";
            // 
            // label3
            // 
            this.label3.Location = new System.Drawing.Point(12, 32);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(80, 16);
            this.label3.TabIndex = 1;
            this.label3.Text = "Product Name:";
            // 
            // edProductName
            // 
            this.edProductName.Location = new System.Drawing.Point(12, 52);
            this.edProductName.Name = "edProductName";
            this.edProductName.Size = new System.Drawing.Size(220, 21);
            this.edProductName.TabIndex = 2;
            this.edProductName.Text = "";
            // 
            // label4
            // 
            this.label4.Location = new System.Drawing.Point(12, 92);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(60, 16);
            this.label4.TabIndex = 5;
            this.label4.Text = "Supplier:";
            // 
            // cbSupplier
            // 
            this.cbSupplier.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.cbSupplier.Location = new System.Drawing.Point(12, 112);
            this.cbSupplier.MaxDropDownItems = 6;
            this.cbSupplier.Name = "cbSupplier";
            this.cbSupplier.Size = new System.Drawing.Size(230, 21);
            this.cbSupplier.TabIndex = 6;
            this.cbSupplier.SelectedIndexChanged += new System.EventHandler(this.cbSupplier_SelectedIndexChanged);
            // 
            // chkDiscontinued
            // 
            this.chkDiscontinued.Location = new System.Drawing.Point(268, 112);
            this.chkDiscontinued.Name = "chkDiscontinued";
            this.chkDiscontinued.Size = new System.Drawing.Size(92, 20);
            this.chkDiscontinued.TabIndex = 7;
            this.chkDiscontinued.Text = "Discontinued";
            // 
            // label5
            // 
            this.label5.Location = new System.Drawing.Point(12, 148);
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size(100, 16);
            this.label5.TabIndex = 8;
            this.label5.Text = "Quantity Per Unit:";
            // 
            // edQuantityPerUnit
            // 
            this.edQuantityPerUnit.Location = new System.Drawing.Point(12, 168);
            this.edQuantityPerUnit.Name = "edQuantityPerUnit";
            this.edQuantityPerUnit.Size = new System.Drawing.Size(172, 21);
            this.edQuantityPerUnit.TabIndex = 9;
            this.edQuantityPerUnit.Text = "";
            // 
            // label6
            // 
            this.label6.Location = new System.Drawing.Point(204, 152);
            this.label6.Name = "label6";
            this.label6.Size = new System.Drawing.Size(80, 16);
            this.label6.TabIndex = 12;
            this.label6.Text = "Reorder Level:";
            // 
            // edReorderLevel
            // 
            this.edReorderLevel.Location = new System.Drawing.Point(300, 148);
            this.edReorderLevel.Maximum = new System.Decimal(new int[] {
																		   1000,
																		   0,
																		   0,
																		   0});
            this.edReorderLevel.Name = "edReorderLevel";
            this.edReorderLevel.Size = new System.Drawing.Size(64, 21);
            this.edReorderLevel.TabIndex = 13;
            this.edReorderLevel.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
            // 
            // label7
            // 
            this.label7.Location = new System.Drawing.Point(12, 208);
            this.label7.Name = "label7";
            this.label7.Size = new System.Drawing.Size(64, 16);
            this.label7.TabIndex = 10;
            this.label7.Text = "Unit Price:";
            // 
            // edUnitPrice
            // 
            this.edUnitPrice.Location = new System.Drawing.Point(96, 204);
            this.edUnitPrice.Name = "edUnitPrice";
            this.edUnitPrice.Size = new System.Drawing.Size(88, 21);
            this.edUnitPrice.TabIndex = 11;
            this.edUnitPrice.Text = "";
            // 
            // label8
            // 
            this.label8.Location = new System.Drawing.Point(204, 180);
            this.label8.Name = "label8";
            this.label8.Size = new System.Drawing.Size(80, 16);
            this.label8.TabIndex = 14;
            this.label8.Text = "Units In Stock:";
            // 
            // edUnitsInStock
            // 
            this.edUnitsInStock.Location = new System.Drawing.Point(300, 176);
            this.edUnitsInStock.Maximum = new System.Decimal(new int[] {
																		   1000,
																		   0,
																		   0,
																		   0});
            this.edUnitsInStock.Name = "edUnitsInStock";
            this.edUnitsInStock.Size = new System.Drawing.Size(64, 21);
            this.edUnitsInStock.TabIndex = 15;
            this.edUnitsInStock.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
            // 
            // label9
            // 
            this.label9.Location = new System.Drawing.Point(204, 208);
            this.label9.Name = "label9";
            this.label9.Size = new System.Drawing.Size(84, 16);
            this.label9.TabIndex = 16;
            this.label9.Text = "Units On Order:";
            // 
            // edUnitsOnOrder
            // 
            this.edUnitsOnOrder.Location = new System.Drawing.Point(300, 204);
            this.edUnitsOnOrder.Maximum = new System.Decimal(new int[] {
																		   1000,
																		   0,
																		   0,
																		   0});
            this.edUnitsOnOrder.Name = "edUnitsOnOrder";
            this.edUnitsOnOrder.Size = new System.Drawing.Size(64, 21);
            this.edUnitsOnOrder.TabIndex = 17;
            this.edUnitsOnOrder.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
            // 
            // label10
            // 
            this.label10.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.label10.Location = new System.Drawing.Point(12, 244);
            this.label10.Name = "label10";
            this.label10.Size = new System.Drawing.Size(352, 2);
            this.label10.TabIndex = 18;
            // 
            // btnOk
            // 
            this.btnOk.Location = new System.Drawing.Point(109, 256);
            this.btnOk.Name = "btnOk";
            this.btnOk.TabIndex = 19;
            this.btnOk.Text = "OK";
            this.btnOk.Click += new System.EventHandler(this.btnOk_Click);
            // 
            // btnCancel
            // 
            this.btnCancel.Location = new System.Drawing.Point(193, 256);
            this.btnCancel.Name = "btnCancel";
            this.btnCancel.TabIndex = 20;
            this.btnCancel.Text = "Cancel";
            // 
            // picCategory
            // 
            this.picCategory.Location = new System.Drawing.Point(346, 12);
            this.picCategory.Name = "picCategory";
            this.picCategory.Size = new System.Drawing.Size(16, 16);
            this.picCategory.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage;
            this.picCategory.TabIndex = 21;
            this.picCategory.TabStop = false;
            // 
            // labSupplierCity
            // 
            this.labSupplierCity.Location = new System.Drawing.Point(108, 88);
            this.labSupplierCity.Name = "labSupplierCity";
            this.labSupplierCity.Size = new System.Drawing.Size(80, 16);
            this.labSupplierCity.TabIndex = 22;
            this.labSupplierCity.Text = "City";
            // 
            // labSupplierCountry
            // 
            this.labSupplierCountry.Location = new System.Drawing.Point(246, 88);
            this.labSupplierCountry.Name = "labSupplierCountry";
            this.labSupplierCountry.Size = new System.Drawing.Size(84, 16);
            this.labSupplierCountry.TabIndex = 23;
            this.labSupplierCountry.Text = "Country";
            // 
            // label11
            // 
            this.label11.Location = new System.Drawing.Point(76, 88);
            this.label11.Name = "label11";
            this.label11.Size = new System.Drawing.Size(32, 16);
            this.label11.TabIndex = 24;
            this.label11.Text = "City:";
            // 
            // label12
            // 
            this.label12.Location = new System.Drawing.Point(192, 88);
            this.label12.Name = "label12";
            this.label12.Size = new System.Drawing.Size(52, 16);
            this.label12.TabIndex = 25;
            this.label12.Text = "Country:";
            // 
            // ProductForm
            // 
            this.AcceptButton = this.btnOk;
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 14);
            this.CancelButton = this.btnCancel;
            this.ClientSize = new System.Drawing.Size(377, 295);
            this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.label12,
																		  this.label11,
																		  this.labSupplierCountry,
																		  this.labSupplierCity,
																		  this.picCategory,
																		  this.btnCancel,
																		  this.btnOk,
																		  this.label10,
																		  this.edUnitsOnOrder,
																		  this.label9,
																		  this.edUnitsInStock,
																		  this.label8,
																		  this.edUnitPrice,
																		  this.label7,
																		  this.edReorderLevel,
																		  this.label6,
																		  this.edQuantityPerUnit,
																		  this.label5,
																		  this.chkDiscontinued,
																		  this.cbSupplier,
																		  this.label4,
																		  this.edProductName,
																		  this.label3,
																		  this.label2,
																		  this.cbCategory,
																		  this.label1});
            this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "ProductForm";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
            this.Text = "ProductForm";
            this.Layout += new System.Windows.Forms.LayoutEventHandler(this.ProductForm_Layout);
            ((System.ComponentModel.ISupportInitialize)(this.edReorderLevel)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.edUnitsInStock)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.edUnitsOnOrder)).EndInit();
            this.ResumeLayout(false);

        }
        #endregion

        private ImageList _images;
        private C1.Data.C1DataSet _c1DataSet;
        private C1.Data.C1DataRow _c1DataRow = null;
        private ProductsRow_tableView _product = null;

        internal ProductForm(string wincaption, C1.Data.C1DataSet dataSet,
            ImageList images)
            : this()
        {
            this.Text = wincaption;
            _c1DataSet = dataSet;
            _images = images;
            cbCategory.DataSource = dataSet;
            cbCategory.DisplayMember = "Categories.CategoryName";
            cbCategory.ValueMember = "Categories.CategoryID";
            cbSupplier.DataSource = dataSet;
            cbSupplier.DisplayMember = "Suppliers.CompanyName";
            cbSupplier.ValueMember = "Suppliers.SupplierID";
        }

        private void CheckString(object sender, ConvertEventArgs cevent)
        {
            if (cevent.DesiredType != typeof(string))
                return;
            if (!(cevent.Value is System.DBNull))
                cevent.Value = (String)cevent.Value;
            else
                cevent.Value = "";
        }

        private void CheckBoolean(object sender, ConvertEventArgs cevent)
        {
            if (cevent.DesiredType != typeof(Boolean))
                return;
            if (!(cevent.Value is System.DBNull))
                cevent.Value = (Boolean)cevent.Value;
            else
                cevent.Value = false;
        }

        private void DecimalToString(object sender, ConvertEventArgs cevent)
        {
            if (cevent.DesiredType != typeof(String))
                return;
            if (!(cevent.Value is System.DBNull))
                cevent.Value = ((decimal)cevent.Value).ToString();
            else
                cevent.Value = "";
        }

        private void CheckDecimal(object sender, ConvertEventArgs cevent)
        {
            if (cevent.DesiredType != typeof(decimal))
                return;
            if (!(cevent.Value is System.DBNull))
                cevent.Value = (short)cevent.Value;
            else
                cevent.Value = 0;
        }

        private void ProductForm_Layout(object sender, System.Windows.Forms.LayoutEventArgs e)
        {
            if (_c1DataRow["CategoryID"] is System.DBNull)
                cbCategory.SelectedIndex = -1;
            if (_c1DataRow["SupplierID"] is System.DBNull)
                cbSupplier.SelectedIndex = -1;
        }

        internal void SetCurrentRow(C1.Data.C1DataRow dataRow)
        {
            _product = ProductsRow_tableView.Obj(dataRow);
            _c1DataRow = dataRow;
            edProductName.Text = _product.IsProductNameNull()
                ? "" : _product.ProductName;
            if (!_product.IsCategoryIDNull())
                cbCategory.SelectedValue = _product.CategoryID;
            if (!_product.IsSupplierIDNull())
                cbSupplier.SelectedValue = _product.SupplierID;
            chkDiscontinued.Checked = _product.IsDiscontinuedNull()
                ? false : _product.Discontinued;
            edQuantityPerUnit.Text = _product.IsQuantityPerUnitNull()
                ? "" : _product.QuantityPerUnit;
            edUnitPrice.Text = _product.IsUnitPriceNull()
                ? "" : _product.UnitPrice.ToString();
            edReorderLevel.Value = _product.IsReorderLevelNull()
                ? (short)0 : _product.ReorderLevel;
            edUnitsInStock.Value = _product.IsUnitsInStockNull()
                ? (short)0 : _product.UnitsInStock;
            edUnitsOnOrder.Value = _product.IsUnitsOnOrderNull()
                ? (short)0 : _product.UnitsOnOrder;
        }

        private void cbCategory_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            int index = cbCategory.SelectedIndex;
            picCategory.Image = (index >= 0 && index < _images.Images.Count)
                ? _images.Images[index] : null;
        }

        private void cbSupplier_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            if (cbSupplier.SelectedIndex != -1)
            {
                C1.Data.C1DataRow row = _c1DataSet.TableViews["Suppliers"].
                    Rows[cbSupplier.SelectedIndex];
                labSupplierCity.Text = row["City"] as String;
                labSupplierCountry.Text = row["Country"] as String;
            }
            else
            {
                labSupplierCity.Text = "";
                labSupplierCountry.Text = "";
            }
        }

        private void btnOk_Click(object sender, System.EventArgs e)
        {
            if (edProductName.Text.Length > 0)
                _product.ProductName = edProductName.Text;
            else
                _product.SetProductNameNull();
            if (cbCategory.SelectedIndex != -1)
                _product.CategoryID = (int)cbCategory.SelectedValue;
            else
                _product.SetCategoryIDNull();
            if (cbSupplier.SelectedIndex != -1)
                _product.SupplierID = (int)cbSupplier.SelectedValue;
            else
                _product.SetSupplierIDNull();
            _product.Discontinued = chkDiscontinued.Checked;
            if (edQuantityPerUnit.Text.Length > 0)
                _product.QuantityPerUnit = edQuantityPerUnit.Text;
            else
                _product.SetQuantityPerUnitNull();
            if (edUnitPrice.Text.Length > 0)
                _product.UnitPrice = decimal.Parse(edUnitPrice.Text);
            else
                _product.SetUnitPriceNull();
            _product.ReorderLevel = (short)edReorderLevel.Value;
            _product.UnitsInStock = (short)edUnitsInStock.Value;
            _product.UnitsOnOrder = (short)edUnitsOnOrder.Value;
            _c1DataRow.EndEdit();
            DialogResult = DialogResult.OK;
        }
    }
}
