
namespace GMLL00ImportadorXsd.Logica
{
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Xml;
    using System.Reflection;
    using System.IO;
    using System.Globalization;
    using System.Configuration;
    

    public class ObtenerDatos
    {
        private static string m_currentDir = string.Empty;
        XmlDocument datosConexion = null;

        private string servidor = string.Empty;
        private string puerto = string.Empty;
        private string database = string.Empty;
        private string usuario = string.Empty;
        private string password = string.Empty;
        private string imagenPlan = string.Empty;
        

        private string longString = string.Empty;
        private string longDouble = string.Empty;
        private string nombreDominioEs = string.Empty;
        private string nombreDominioEu = string.Empty;
        private string adjuntarNombreCapa = string.Empty;
        private string strFichConfig = string.Empty;

        public string Servidor
        {
            get { return servidor; }
            set { servidor = value; }
        }
        public string Puerto
        {
            get { return puerto; }
            set { puerto = value; }
        }
        public string Database
        {
            get { return database; }
            set { database = value; }
        }
        public string Usuario
        {
            get { return usuario; }
            set { usuario = value; }
        }
        public string Password
        {
            get { return password; }
            set { password = value; }
        }
        public string LongString
        {
            get { return longString; }
            set { longString = value; }
        }
        public string LongDouble
        {
            get { return longDouble; }
            set { longDouble = value; }
        }

        public string NombreDominioES
        {
            get { return nombreDominioEs; }
            set { nombreDominioEs = value; }
        }
        public string NombreDominioEU
        {
            get { return nombreDominioEu; }
            set { nombreDominioEu = value; }
        }
        public string AdjuntarNombreCapa
        {
            get { return adjuntarNombreCapa; }
            set { adjuntarNombreCapa = value; }
        }
        public string ImagenPlan
        {
            get { return imagenPlan; }
            set { imagenPlan = value; }
        }





        private bool IsNumber(string inputvalue)
        {           
            System.Text.RegularExpressions.Regex isnumber = new System.Text.RegularExpressions.Regex((@"^-?[0-9]+(\?[0-9]+)?$"));          
            return isnumber.IsMatch(inputvalue);

        }


        public ObtenerDatos()
        {

            string strObtenerRuta = string.Empty;
           
            strObtenerRuta = CurrentDir;

            strFichConfig =Convert.ToString ( ConfigurationManager.AppSettings["NombreFicheroConf"],null);
            datosConexion = new XmlDocument();
            if (File.Exists(strObtenerRuta + "\\" + strFichConfig)){

                datosConexion.Load(strObtenerRuta + "\\" + strFichConfig);

                XmlNodeList nodoPrincipal = datosConexion.GetElementsByTagName("FeatureSource");
                XmlNodeList lista = ((XmlElement)nodoPrincipal[0]).GetElementsByTagName("Parameter");
                foreach (XmlElement nodo in lista)
                {
                    int i = 0;
                    XmlNodeList nombre = nodo.GetElementsByTagName("Name");
                    XmlNodeList valor = nodo.GetElementsByTagName("Value");

                    if (nombre[i].InnerText.Equals("ImagebPlan")) { imagenPlan = valor[i].InnerText; }
                    if (nombre[i].InnerText.Equals("Service")) { servidor = valor[i].InnerText; }
                    if (nombre[i].InnerText.Equals("DataStore")) { database = valor[i].InnerText; }
                    if (nombre[i].InnerText.Equals("Username")) { usuario = valor[i].InnerText; }
                    if (nombre[i].InnerText.Equals("Password")) { password = valor[i].InnerText; }
                    if (nombre[i].InnerText.Equals("Port")) { puerto = valor[i].InnerText; }
                    
                    if (nombre[i].InnerText.Equals("LongString"))
                    {
                        longString = valor[i].InnerText;
                        
                            if ((!IsNumber(longString.Trim())) || string.IsNullOrEmpty(longString.Trim()) || (Convert.ToInt16(longString, CultureInfo.CurrentCulture) <= 0)  )
                            {
                                longString = "255";
                            }
                       
                    }
                    if (nombre[i].InnerText.Equals("LongDouble"))
                    {
                        longDouble = valor[i].InnerText;
                        if (longDouble.IndexOf(",", StringComparison.Ordinal) == -1)
                        {

                            longDouble = "18,2";
                        }
                    }
                    if (nombre[i].InnerText.Equals("NombreDominioEs")) { nombreDominioEs = valor[i].InnerText; }
                    if (nombre[i].InnerText.Equals("NombreDominioEu")) { nombreDominioEu = valor[i].InnerText; }
                    if (nombre[i].InnerText.Equals("AdjuntarNombreCapa")) { adjuntarNombreCapa = valor[i].InnerText; }
                   
                }

            }
            else
            {
                //Error
            }

        }
        public void GuardaParametros()
        {
            XmlNodeList nodoPrincipal = datosConexion.GetElementsByTagName("FeatureSource");
            XmlNodeList lista = ((XmlElement)nodoPrincipal[0]).GetElementsByTagName("Parameter");
            foreach (XmlElement nodo in lista)
            {
                int i = 0;
                XmlNodeList nombres = nodo.GetElementsByTagName("Name");
                XmlNodeList valor = nodo.GetElementsByTagName("Value");
                if (nombres[i].InnerText.Equals("Service")) { valor[i].InnerText = servidor; }
                if (nombres[i].InnerText.Equals("DataStore")) { valor[i].InnerText = database; }
                if (nombres[i].InnerText.Equals("Username")) { valor[i].InnerText = usuario; }
                if (nombres[i].InnerText.Equals("Password")) { valor[i].InnerText = password; }
                if (nombres[i].InnerText.Equals("Port")) { valor[i].InnerText = puerto; }
            }


            string strObtenerRuta = string.Empty;
            strObtenerRuta = CurrentDir;

            datosConexion.Save(strObtenerRuta + "\\" + strFichConfig); 
            



        }

        public static string CurrentDir
        {
            get
            {
                if (string.IsNullOrEmpty(m_currentDir))
                {
                    Assembly asm = Assembly.GetExecutingAssembly();
                    FileInfo info = new FileInfo(asm.Location);
                    m_currentDir = info.DirectoryName;
                }
                return m_currentDir;
            }
        }
    }
}
