using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Collections;
using System.Xml.Linq;
using System.Reflection;
using System.Globalization;
using XMLExtractor;

namespace XMLExtractor {

    public class ReadXML
    {
        // Tags
        private static string XML_ELEMENT_STATIC_VALUES = "capitoli";
        private static string XML_ELEMENT_CHAPTERS = "capitoli";
        private static string XML_ELEMENT_SINGLECHAPTER = "capitolo";
        private static string XML_ELEMENT_SECTIONS = "sezioni";
        private static string XML_ELEMENT_SINGLESECTION = "sezione";
        private static string XML_ELEMENT_PAGES = "pagine";
        private static string XML_ELEMENT_PAGE = "pagina";
        private static string XML_ELEMENT_PARAGRAPHS = "paragrafi";
        private static string XML_ELEMENT_SINGLEPARAGRAPH = "paragrafo";
        private static string XML_ELEMENT_SUBPARAGRAPH = "sottoParagrafi";
        private static string XML_ELEMENT_CHARTS = "charts";
        private static string XML_ELEMENT_SINGLECHART = "chart";
        private static string XML_ELEMENT_CHARTMEDIA = "chartMediaDeiPrezzi";
        private static string XML_ELEMENT_CITIES = "tableCittaPrincipali";
        private static string XML_ELEMENT_IMAGES = "images";
        private static string XML_ELEMENT_SINGLEIMAGE = "image";
        private static string XML_ELEMENT_VALUES = "aspettativeValoriImmobili";
        private static string XML_ELEMENT_VARIABLE = "aspettativeVariazioneValore";
        private static string XML_ELEMENT_EVOLUTION = "tableEvoluzione";
        private static string XML_ELEMENT_IMPOSTA = "tableImposta";

        public static int idCapitolo { get; set; }
        public static int idSezione { get; set; }
        public static int idOrdinamento { get; set; }
        public static int idPagina { get; set; }


        // Tags List
        public static List<String> tagList = new List<String>(new[]{ 
            XML_ELEMENT_CHAPTERS, XML_ELEMENT_SINGLECHAPTER,                        //  Chapters
            XML_ELEMENT_SECTIONS, XML_ELEMENT_SINGLESECTION,                        //  Sections
            XML_ELEMENT_PAGES, XML_ELEMENT_PAGE,                                    //  Pages
            XML_ELEMENT_PARAGRAPHS, XML_ELEMENT_SINGLEPARAGRAPH,                    //  Paragraphs
            XML_ELEMENT_CHARTS, XML_ELEMENT_SINGLECHART, XML_ELEMENT_CHARTMEDIA,    //  Charts                                      
            XML_ELEMENT_IMAGES, XML_ELEMENT_SINGLEIMAGE,                            //  Images
            XML_ELEMENT_VALUES, XML_ELEMENT_VARIABLE,                               //  Keys & values for graphs
            XML_ELEMENT_CITIES, XML_ELEMENT_EVOLUTION, XML_ELEMENT_IMPOSTA});       //  Tables

        //  Extracted data class
        public static Data data = new Data();
        public static Data dataNucleo = new Data();

        /// <summary>
        /// XML File Reader
        /// </summary>
        public ReadXML()
        {
            // idCapitolo '1' and '2' belong to Copertina and Indice 
            idCapitolo = idPagina = 3;
            idSezione = idOrdinamento = 1;
            XmlTextReader reader = new XmlTextReader(Utility.PATH_XML);
            reader.WhitespaceHandling = WhitespaceHandling.Significant;
            reader.Read();
            reader.Read();
            string rs = reader.ReadOuterXml();
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(rs);
            XmlNode node = doc.DocumentElement;
            extractTag(node, XML_ELEMENT_CHAPTERS);
            dataNucleo = data;
            data.writeTables();
        }

        /// <summary>
        /// Extraction of the tag's children
        /// </summary>
        public static void extractTag(XmlNode child, string tag)
        {
            foreach (XmlNode chldNode in child.ChildNodes)
            {
                if (chldNode.Name.Equals(tag))
                {
                    var nameAttribute = chldNode.Name;
                    if (nameAttribute != null)
                        takeRightDB(chldNode, tag);
                }
            }
        }


        /// <summary>
        /// Given the tag, it selects the right extractor object
        /// </summary>
        public static void takeRightDB(XmlNode attribute, string tag)
        {
            switch (tag)
            {
                case "capitoli":
                    extractTag(attribute, XML_ELEMENT_SINGLECHAPTER);
                    break;
                case "capitolo":
                    Chapter.writeChapter(attribute, tagList, XML_ELEMENT_SINGLESECTION, data);
                    idSezione = 1;
                    break;
                case "sezione":
                    Section.writeSection(attribute, tagList, "", data);
                    break;
                case "pagina":
                    Page.writePage(attribute, tagList, "", data);
                    break;
                case "paragrafi":
                    extractTag(attribute, XML_ELEMENT_SINGLEPARAGRAPH);
                    break;
                case "paragrafo":
                    Paragraph.writeParagraph(attribute, tagList, "", data);
                    break;
                case "charts":
                    extractTag(attribute, XML_ELEMENT_SINGLECHART);
                    break;
                case "chart":
                    Chart.writeChart(attribute, tagList, "", data);
                    break;
                case "chartMediaDeiPrezzi":
                    ChartMediaDeiPrezzi.writeChartMediaDeiPrezzi(attribute, tagList, "", data);
                    break;
                case "images":
                    extractTag(attribute, XML_ELEMENT_SINGLEIMAGE);
                    break;
                case "image":
                    Image.writeImage(attribute, tagList, "", data);
                    break;
                case "tableCittaPrincipali":
                    TableCittaPrincipali.writeCittaPrincipali(attribute, tagList, "", data);
                    break;
                case "aspettativeValoriImmobili":
                    AspettativeValoriImmobili.writeAspettativeValoriImmobili(attribute, tagList, "", data);
                    break;
                case "aspettativeVariazioneValore":
                    AspettativeVariazioneValore.writeAspettativeVariazioneValore(attribute, tagList, "", data);
                    break;
                case "tableEvoluzione":
                    TableEvoluzione.writeTableEvoluzione(attribute, tagList, "", data);
                    break;
                case "tableImposta":
                    TableImposta.writeTableImposta(attribute, tagList, "", data);
                    break;
            }
        }


    }    
}
