using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace CommonLayer
{
    public sealed class Settings
    {
        public Settings()
        { }
        public static string Patch = "Setting.xml";
        public static string ConnectionString = "";
        public static string Name = "";

        public static void LoadSettings()
        {
            if (System.IO.File.Exists(Settings.Patch) == false)
            {
                XmlTextWriter xmlTextWriter = new XmlTextWriter(Settings.Patch, ASCIIEncoding.UTF8);
                xmlTextWriter.WriteStartDocument();
                xmlTextWriter.WriteComment("Grocery Store Settings. Created By Mahdi Ghorbanpour");
                xmlTextWriter.WriteStartElement("Root");
                xmlTextWriter.WriteStartElement("ConnectionString");
                xmlTextWriter.WriteAttributeString("GroceryConnectionString", "Data Source=.;Initial Catalog=Grocery;Integrated Security=True");
                xmlTextWriter.WriteEndElement();
                xmlTextWriter.WriteStartElement("name");
                xmlTextWriter.WriteAttributeString("Froshgah", "الکترو امواج آسمان");
                xmlTextWriter.WriteEndElement();
                xmlTextWriter.WriteEndElement();
                xmlTextWriter.WriteEndDocument();
                xmlTextWriter.Close();
            }
            XmlDocument xmlDoc = new XmlDocument();
            XmlNodeList xmlNodeList;
            XmlNode xmlNode;
            xmlDoc.Load(Patch);
            xmlNodeList = xmlDoc.GetElementsByTagName("ConnectionString");
            xmlNode = xmlNodeList.Item(0);
            Settings.ConnectionString = xmlNode.Attributes.Item(0).Value;
            xmlNodeList = xmlDoc.GetElementsByTagName("name");
            xmlNode = xmlNodeList.Item(0);
            Settings.Name = xmlNode.Attributes.Item(0).Value;
        }

        public static void WriteSettings(string _Con, string _name)
        {
            XmlTextWriter xmlTextWriter = new XmlTextWriter(Settings.Patch, ASCIIEncoding.UTF8);
            xmlTextWriter.WriteStartDocument();
            xmlTextWriter.WriteComment("Grocery Store Settings. Created By Mahdi Ghorbanpour");
            xmlTextWriter.WriteStartElement("Root");
            xmlTextWriter.WriteStartElement("ConnectionString");
            xmlTextWriter.WriteAttributeString("GroceryConnectionString", _Con);
            xmlTextWriter.WriteEndElement();
            xmlTextWriter.WriteStartElement("name");
            xmlTextWriter.WriteAttributeString("Froshgah", _name);
            xmlTextWriter.WriteEndElement();
            xmlTextWriter.WriteEndElement();
            xmlTextWriter.WriteEndDocument();
            xmlTextWriter.Close();
        }
    }
}
