using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Serialization;

namespace Stone.Class
{


    class FactorClass
    {


       public string XmlDataFileNamePropertiesFactor = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData) + "\\ParsZSDB" + "\\PropertiesFactor.xml";
       public string XmlSchemaFileNamePropertiesFactor =System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData) + "\\ParsZSDB" + "\\PropertiesFactor.xsd";

       public string XmlDataFileNameDetailFactor = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData) + "\\ParsZSDB" + "\\DetailFactor.xml";
       public string XmlSchemaFileNameDetailFactor = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData) + "\\ParsZSDB" + "\\DetailFactor.xsd";


        public DataSet DSFactor = new DataSet("DSFactor");
        public DataTable TDetailFactor = new DataTable("TDetailFactor");
        public DataTable TPropertiesFactor = new DataTable("TPropertiesFactor");
        public DataTable PropertiesHelp = new DataTable("PropertiesHelp");
        public DataTable TDetailFactorManagment = new DataTable("TDetailFactorManagment");
        public FactorClass()
        {
            #region Create_TableDetailFactor
            DataColumn IdFactor = new DataColumn("IdFactor", System.Type.GetType("System.Int64"));
            DataColumn Id = new DataColumn("Id", System.Type.GetType("System.Int32"));
            DataColumn KindStone = new DataColumn("KindStone", System.Type.GetType("System.String"));
            DataColumn Count = new DataColumn("Count", System.Type.GetType("System.Int32"));
            DataColumn Length = new DataColumn("Length", System.Type.GetType("System.Double"));
            DataColumn Width = new DataColumn("Width",System.Type.GetType("System.Double"));
            DataColumn Measure = new DataColumn("Measure", System.Type.GetType("System.String"));
            DataColumn Price = new DataColumn("Price", System.Type.GetType("System.Int64"));
            DataColumn PriceAll = new DataColumn("PriceAll", System.Type.GetType("System.Int64"));
            DataColumn Date = new DataColumn("Date", System.Type.GetType("System.String"));
            TDetailFactor.Columns.Add(IdFactor);
            TDetailFactor.Columns.Add(Id);
            TDetailFactor.Columns.Add(KindStone);
            TDetailFactor.Columns.Add(Count);
            TDetailFactor.Columns.Add(Length);
            TDetailFactor.Columns.Add(Width);
            TDetailFactor.Columns.Add(Measure);
            TDetailFactor.Columns.Add(Price);
            TDetailFactor.Columns.Add(PriceAll);
            TDetailFactor.Columns.Add(Date);
            #endregion

            #region Create_TablePropertiesFactor
            DataColumn IdFactorProperties = new DataColumn("IdFactorProperties", System.Type.GetType("System.Int64"));
            DataColumn Name = new DataColumn("Name", System.Type.GetType("System.String"));
            DataColumn Tel = new DataColumn("Tel", System.Type.GetType("System.Int64"));
            DataColumn PriceFactor = new DataColumn("PriceFactor", System.Type.GetType("System.Int64"));
            DataColumn Mod = new DataColumn("Mod", System.Type.GetType("System.Int64"));
            DataColumn StringMoney = new DataColumn("StringMoney", System.Type.GetType("System.String"));
            DataColumn DatePer = new DataColumn("Date", System.Type.GetType("System.String"));
            DataColumn Address = new DataColumn("Address", System.Type.GetType("System.String"));
            DataColumn Max = new DataColumn("Max", System.Type.GetType("System.String"),"Max(IdFactorProperties)");
            TPropertiesFactor.Columns.Add(IdFactorProperties);
           
            TPropertiesFactor.Columns.Add(Name);
            TPropertiesFactor.Columns.Add(Tel);
            TPropertiesFactor.Columns.Add(PriceFactor);
            TPropertiesFactor.Columns.Add(Mod);
            TPropertiesFactor.Columns.Add(StringMoney);
            TPropertiesFactor.Columns.Add(DatePer);
            TPropertiesFactor.Columns.Add(Address);
            TPropertiesFactor.Columns.Add(Max);


            TPropertiesFactor.Locale = System.Globalization.CultureInfo.CurrentCulture;
            #endregion

            #region Create_TableDetailFactorManagment
       
                DataColumn IdFactor1 = new DataColumn("IdFactor", System.Type.GetType("System.Int64"));
                DataColumn Id1 = new DataColumn("Id", System.Type.GetType("System.Int32"));
                DataColumn KindStone1 = new DataColumn("KindStone", System.Type.GetType("System.String"));
                DataColumn Count1 = new DataColumn("Count", System.Type.GetType("System.Int32"));
                DataColumn Length1 = new DataColumn("Length", System.Type.GetType("System.Double"));
                DataColumn Width1 = new DataColumn("Width", System.Type.GetType("System.Double"));
                DataColumn Measure1 = new DataColumn("Measure", System.Type.GetType("System.String"));
                DataColumn Price1 = new DataColumn("Price", System.Type.GetType("System.Int64"));
                DataColumn PriceAll1 = new DataColumn("PriceAll", System.Type.GetType("System.Int64"));
                DataColumn Date2 = new DataColumn("Date", System.Type.GetType("System.String"));
                TDetailFactorManagment.Columns.Add(IdFactor1);
                TDetailFactorManagment.Columns.Add(Id1);
                TDetailFactorManagment.Columns.Add(KindStone1);
                TDetailFactorManagment.Columns.Add(Count1);
                TDetailFactorManagment.Columns.Add(Length1);
                TDetailFactorManagment.Columns.Add(Width1);
                TDetailFactorManagment.Columns.Add(Measure1);
                TDetailFactorManagment.Columns.Add(Price1);
                TDetailFactorManagment.Columns.Add(PriceAll1);
                TDetailFactorManagment.Columns.Add(Date2);
            
            #endregion



            #region Create_TablePropertiesHelp
                DataColumn IdFactorProperties1 = new DataColumn("IdFactorProperties", System.Type.GetType("System.Int64"));
                DataColumn Name1 = new DataColumn("Name", System.Type.GetType("System.String"));
                DataColumn Tel1 = new DataColumn("Tel", System.Type.GetType("System.Int64"));
                DataColumn Address1 = new DataColumn("Address", System.Type.GetType("System.String"));
                DataColumn PriceFactor1 = new DataColumn("PriceFactor", System.Type.GetType("System.Int64"));
                DataColumn Mod1 = new DataColumn("Mod", System.Type.GetType("System.Int64"));
                DataColumn Date1 = new DataColumn("Date", System.Type.GetType("System.String"));
                DataColumn StringMoney1 = new DataColumn("StringMoney", System.Type.GetType("System.String"));
                DataColumn Max1 = new DataColumn("Max", System.Type.GetType("System.String"), "Max(IdFactorProperties)");
                PropertiesHelp.Columns.Add(IdFactorProperties1);
                PropertiesHelp.Columns.Add(Name1);
                PropertiesHelp.Columns.Add(Tel1);
                PropertiesHelp.Columns.Add(Address1);
                PropertiesHelp.Columns.Add(PriceFactor1);
                PropertiesHelp.Columns.Add(Mod1);
                PropertiesHelp.Columns.Add(Date1);
                PropertiesHelp.Columns.Add(StringMoney1);
                PropertiesHelp.Columns.Add(Max1);
                #endregion



            DSFactor.Tables.Add(TDetailFactor);
            DSFactor.Tables.Add(TPropertiesFactor);
            DSFactor.Tables.Add(TDetailFactorManagment);
            DSFactor.Tables.Add(PropertiesHelp);

            #region Create_FileXML_PropertiesFactor|DetailFactor

            //Create Files PropertiesFactor .......
            if (!File.Exists(XmlDataFileNamePropertiesFactor) || !File.Exists(XmlSchemaFileNamePropertiesFactor))
            {
                Make_PropertiesFactor();
                DSFactor.Tables["TPropertiesFactor"].ReadXmlSchema(XmlSchemaFileNamePropertiesFactor);
                DSFactor.Tables["TPropertiesFactor"].ReadXml(XmlDataFileNamePropertiesFactor);
            }
            else
            {
                DSFactor.Tables["TPropertiesFactor"].ReadXmlSchema(XmlSchemaFileNamePropertiesFactor);
                DSFactor.Tables["TPropertiesFactor"].ReadXml(XmlDataFileNamePropertiesFactor);

            }

            //Create Files DetailFactor .......
            if (!File.Exists(XmlDataFileNameDetailFactor) || !File.Exists(XmlSchemaFileNameDetailFactor))
            {
                Make_DetailFactor();
                DSFactor.Tables["TDetailFactorManagment"].ReadXmlSchema(XmlSchemaFileNameDetailFactor);
                DSFactor.Tables["TDetailFactorManagment"].ReadXml(XmlDataFileNameDetailFactor);
            }
            {
                DSFactor.Tables["TDetailFactorManagment"].ReadXmlSchema(XmlSchemaFileNameDetailFactor);
                DSFactor.Tables["TDetailFactorManagment"].ReadXml(XmlDataFileNameDetailFactor);

            }

            #endregion

          
        }

        public string GenerateIDFactor()
        {

            if (DSFactor.Tables["TPropertiesFactor"].Rows.Count == 0)
            {
                return "1";
            }
            else
            {
                return (Int64.Parse(DSFactor.Tables["TPropertiesFactor"].Rows[0]["Max"].ToString()) + 1).ToString();
            }


        }

        private void Make_PropertiesFactor()
        {

            if (!File.Exists(XmlDataFileNamePropertiesFactor))
            {
                DSFactor.Tables["TPropertiesFactor"].WriteXml(XmlDataFileNamePropertiesFactor);
            }
            if (!File.Exists(XmlSchemaFileNamePropertiesFactor))
            {
                DSFactor.Tables["TPropertiesFactor"].WriteXml(XmlSchemaFileNamePropertiesFactor);
            }

            DSFactor.Tables["TPropertiesFactor"].WriteXmlSchema(XmlSchemaFileNamePropertiesFactor);

        }

        private void Make_DetailFactor()
        {

            if (!File.Exists(XmlDataFileNameDetailFactor))
            {
                DSFactor.Tables["TDetailFactorManagment"].WriteXml(XmlDataFileNameDetailFactor);
            }
            if (!File.Exists(XmlSchemaFileNameDetailFactor))
            {
                DSFactor.Tables["TDetailFactorManagment"].WriteXml(XmlSchemaFileNameDetailFactor);
            }

            DSFactor.Tables["TDetailFactorManagment"].WriteXmlSchema(XmlSchemaFileNameDetailFactor);

        }

    }
}
