using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Reflection;
using System.Data.SqlClient;
using XMLExtractor;

namespace XMLExtractor
{
    public  class Page
    {
        public string titoloPagina { get; set; }
        public int idPagina { get; set; }
        public int idCapitolo { get; set; }
        public int idSezione { get; set; }
        public List<Nota> note { get; set; }

        public void writeDB(SqlConnection conn)
        {
            int idREP_IMM_MON_Template = getIdREP_IMM_MON_Template(conn, Utility.ID_SECTION+1);
            if (idREP_IMM_MON_Template != 0)
            {
                string sqlCommand = "(idREP_IMM_MON_Pagina, idREP_IMM_MON_Template, titoloPagina, nota1, nota2, nota3, nota4, nota5) VALUES "
                    + "(@idREP_IMM_MON_Pagine,@idREP_IMM_MON_Template, @titoloPagina, @nota1, @nota2, @nota3, @nota4, @nota5)";
                SqlCommand insertTo = new SqlCommand("INSERT INTO REP_IMM_MON_Pagine " + sqlCommand, conn);
                insertTo.Parameters.AddWithValue("@idREP_IMM_MON_Pagine", this.idPagina);
                insertTo.Parameters.AddWithValue("@idREP_IMM_MON_Template", idREP_IMM_MON_Template);
                insertTo.Parameters.AddWithValue("@titoloPagina", this.titoloPagina);
                for (int i = 1; i <= 5; i++)
                {
                    if (this.note != null && i <= this.note.Count)
                        insertTo.Parameters.AddWithValue("@nota" + i, note[i-1].valore);
                    else
                        insertTo.Parameters.AddWithValue("@nota" + i, DBNull.Value);
                }
                int rowsUpdatedInserted = insertTo.ExecuteNonQuery();
            }

        }

        public static int getIdREP_IMM_MON_Template(SqlConnection conn, int section)
        {
            string sqlCommand = "SELECT a.idREP_IMM_MON_Template FROM REP_IMM_MON_Template a INNER JOIN TemplateStrutturaModelli b "
            + "ON a.idTemplateStrutturaModelli = b.ID WHERE idSezione = @idSezione";
            SqlCommand idCmd = new SqlCommand(sqlCommand, conn);
            idCmd.Parameters.AddWithValue("@idSezione", section);
            return idCmd.ExecuteScalar() != null ? Convert.ToInt32(idCmd.ExecuteScalar()) : 0;
        }

        public int join(SqlConnection conn)
        {
            string sqlCommand = "SELECT a.idREP_IMM_MON_Template, FROM REP_IMM_MON_Template a "
            + "INNER JOIN TemplateStrutturaModelli b ON a.idTemplateStrutturaModelli = b.ID WHERE b.IdSezione = @idSezione";

            SqlCommand join = new SqlCommand(sqlCommand, conn);
            join.Parameters.AddWithValue("@idSezione", Utility.ID_SECTION + this.idSezione);
            Utility.ID_SECTION++;
            return join.ExecuteScalar() != null ? Convert.ToInt32(join.ExecuteScalar()) : 0;
        }

        public static List<string> getProperties()
        {
            List<string> properties = new List<string>();
            Page objectChart = new Page();
            foreach (var prop in objectChart.GetType().GetProperties())
            {
                properties.Add(prop.Name.ToLower());
            }

            properties.Remove("idcapitolo");
            properties.Remove("idsezione");
            properties.Remove("idpagina");
            properties.Remove("note");

            return properties;
        }

        public static void writePage(XmlNode attribute, List<string> tagList, string nextTag, Data data)
        {
            bool presenzaNota = attribute.ChildNodes.Cast<XmlNode>().Any(x => x.Name.Equals("note"));
            PropertyInfo propertyInfo;
            List<string> pageAttributes = Page.getProperties();
            if (presenzaNota)
                pageAttributes.Add("note");
            Page page = new Page();

            foreach (XmlNode attr in attribute.ChildNodes)
                if (!tagList.Contains(attr.Name.ToString()))
                {  
                    propertyInfo = page.GetType().GetProperty(attr.Name);

                    if (attr.Name.Equals("note"))
                        propertyInfo.SetValue(page, Convert.ChangeType(Nota.getNote(attr), propertyInfo.PropertyType), null);
                    else if (attr.Name.Equals("backgroundPagina"))
                        Image.writeIcon(attr.ChildNodes, data);
                    else
                        propertyInfo.SetValue(page, Convert.ChangeType(attr.InnerText, propertyInfo.PropertyType), null);


                    pageAttributes.Remove(attr.Name.ToLower());

                    if (!(pageAttributes.Count > 0))
                    {
                        page.idCapitolo = ReadXML.idCapitolo;
                        page.idSezione = ReadXML.idSezione;
                        page.idPagina = ReadXML.idPagina;
                        data.pageList.Add(page);
                        page = new Page();
                        pageAttributes = Page.getProperties();
                        if (presenzaNota)
                            pageAttributes.Add("note");
                    }
                }
                else
                {
                    //foreach (XmlNode nameAttr in attr.ChildNodes)
                    //if(attr.FirstChild.NodeType.ToString().Equals("Element"))
                    if (tagList.Contains(attr.FirstChild.Name))
                        ReadXML.extractTag(attr, attr.FirstChild.Name);
                    else
                        ReadXML.extractTag(attr.ParentNode, attr.Name.ToString());
           
                    //ReadXML.extractTag(attr, attr.Name.ToString());
                }
            ReadXML.idPagina++;
            ReadXML.idOrdinamento = 1;
        }
    }
}
