using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;
namespace Stone.Class
{
    class AccountUser
    {


       public string XmlDataFileName = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData) + "\\ParsZSDB" + "\\Account.xml";
       public string XmlSchemaFileName = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData) + "\\ParsZSDB" + "\\Account.xsd";

       public DataTable Account = new DataTable("Account");
       public DataSet DSFactor = new DataSet("DSFactor");
       public AccountUser()
       {
           #region Create_Account

           DataColumn IdFactor = new DataColumn("IdFactor", System.Type.GetType("System.String"));
           DataColumn Type = new DataColumn("Type", System.Type.GetType("System.String"));
           DataColumn Price = new DataColumn("Price", System.Type.GetType("System.String"));
           DataColumn Date = new DataColumn("Date", System.Type.GetType("System.String"));
           DataColumn Id = new DataColumn("Id", System.Type.GetType("System.Int32"));
           DataColumn Max = new DataColumn("Max", System.Type.GetType("System.String"), "Max(Id)");

           Account.Columns.Add(IdFactor);
           Account.Columns.Add(Type);
           Account.Columns.Add(Price);
           Account.Columns.Add(Date);
           Account.Columns.Add(Id);
           Account.Columns.Add(Max);
           DSFactor.Tables.Add(Account);

           #endregion

           #region Create_XMLfile

           if (!File.Exists(XmlDataFileName) || !File.Exists(XmlSchemaFileName))
           {
               Make_DTAccount();
               DSFactor.Tables["Account"].ReadXmlSchema(XmlSchemaFileName);
                DSFactor.Tables["Account"].ReadXml(XmlDataFileName);
           }

           else
           {
               DSFactor.Tables["Account"].ReadXmlSchema(XmlSchemaFileName);
               DSFactor.Tables["Account"].ReadXml(XmlDataFileName);
           }
           #endregion
       }

       private void Make_DTAccount()
       {

           if (!File.Exists(XmlDataFileName))
           {
               DSFactor.Tables["Account"].WriteXml(XmlDataFileName);
           }
           if (!File.Exists(XmlSchemaFileName))
           {
               DSFactor.Tables["Account"].WriteXml(XmlSchemaFileName);
           }

           DSFactor.Tables["Account"].WriteXmlSchema(XmlSchemaFileName);

       }


       public string GenerateIDFactor()
       {

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


       }



    }
}
