using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Globalization;
using System.Text;
using System.Xml;
using System.Xml.XPath;
using System.Windows.Forms;
using GMLC01HerramientasCad.Logica;
using System.Collections;

namespace GMLC01HerramientasCad.Logica
{
    
    public class ConfigGmlParser
    {
        private XmlDocument xmlDoc;
        private ArrayList configs = new ArrayList();

        public ConfigGmlParser(String xmlFile)
        {
            try
            {
                xmlDoc = new XmlDocument();
                xmlDoc.Load(xmlFile);

                this.LoadInfo();

                if (this.xmlDoc == null)
                    throw new ExcepcionGMLC(Properties.Resources.FicheroConfigGmlError);
            }
            catch (ExcepcionGMLC)
            {
                throw;
            }
            catch (Exception e)
            {
                Log.RegisterError(e);
                throw new ExcepcionGMLC(e.Message, e);
            }
        }

        public ArrayList Configurations { 
            get{return this.configs;}
        }

        private void LoadInfo()
        {
            if(this.xmlDoc != null){
                
                XmlNode configurations = this.xmlDoc.GetElementsByTagName("configurations")[0];
                XmlNodeList lista = configurations.ChildNodes;
                foreach (XmlNode elem in lista){
                    ConfigGml conf = new ConfigGml();
                    // Valores
                    XmlNodeList campos = elem.ChildNodes;
                    foreach (XmlNode campo in campos)
                    {
                        if (String.Compare(campo.Name.ToLower(CultureInfo.CurrentCulture), "title", StringComparison.CurrentCulture) == 0)
                        {
                            XmlNodeList idiomas = campo.ChildNodes;
                            Hashtable titulos = new Hashtable();
                            foreach (XmlNode idioma in idiomas)
                            {
                                titulos.Add(idioma.Name.ToLower(CultureInfo.CurrentCulture), idioma.InnerText);
                            }
                            conf.Titles = titulos;
                        }
                        if (String.Compare(campo.Name.ToLower(CultureInfo.CurrentCulture), "id", StringComparison.CurrentCulture) == 0)
                        {
                            conf.IdGml = campo.InnerText;
                        }
                        if (String.Compare(campo.Name.ToLower(CultureInfo.CurrentCulture), "nillable", StringComparison.CurrentCulture) == 0)
                        {
                            conf.NillableGml = campo.InnerText;
                        }
                        if (String.Compare(campo.Name.ToLower(CultureInfo.CurrentCulture), "uom", StringComparison.CurrentCulture) == 0)
                        {
                            conf.UomGml = campo.InnerText;
                        }
                        if (String.Compare(campo.Name.ToLower(CultureInfo.CurrentCulture), "dimension", StringComparison.CurrentCulture) == 0)
                        {
                            conf.Dimension = campo.InnerText;
                        }
                        if (String.Compare(campo.Name.ToLower(CultureInfo.CurrentCulture),"srs",StringComparison.CurrentCulture) == 0)
                        {
                            conf.Srs = campo.InnerText;
                        }
                        if (String.Compare(campo.Name.ToLower(CultureInfo.CurrentCulture), "coordinateformat", StringComparison.CurrentCulture) == 0)
                        {
                            XmlNodeList elementos = campo.ChildNodes;
                            foreach (XmlNode geometria in elementos)
                            {
                                conf.CoordinatesFormat.Add(geometria.Name.ToLower(CultureInfo.CurrentCulture), geometria.InnerText);
                            }
                        }
                        if (String.Compare(campo.Name.ToLower(CultureInfo.CurrentCulture), "head", StringComparison.CurrentCulture) == 0)
                        {
                            conf.Head = campo.InnerText;
                        }
                    }
                    this.configs.Add(conf);
                }
            }else
                throw new ExcepcionGMLC("ConfigGml error.");
        }

    }
}
