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

namespace XMLExtractor
{
     public class Legenda {

        public string id { get; set; }
        public List<string> value { get; set; }

        public static int writeDB(SqlConnection conn, List<Legenda> legenda)
        {
            if (legenda == null)
                return 0;
            string sqlCommand = "(idREP_IMM_MON_Legenda, keyLegenda1, valueLegenda1, keyLegenda2, valueLegenda2, keyLegenda3, valueLegenda3, "
                + "keyLegenda4, valueLegenda4, keyLegenda5, valueLegenda5, keyLegenda6, valueLegenda6, keyLegenda7, valueLegenda7, keyLegenda8, valueLegenda8, "
                + "keyLegenda9, valueLegenda9, keyLegenda10, valueLegenda10) "
                + "VALUES (@idREP_IMM_MON_Legenda, @keyLegenda1, @valueLegenda1, @keyLegenda2, @valueLegenda2, @keyLegenda3, @valueLegenda3, "
                + "@keyLegenda4, @valueLegenda4, @keyLegenda5, @valueLegenda5, @keyLegenda6, @valueLegenda6, @keyLegenda7, @valueLegenda7, @keyLegenda8, @valueLegenda8, "
                + "@keyLegenda9, @valueLegenda9, @keyLegenda10, @valueLegenda10)";


            SqlCommand insertTo = new SqlCommand("INSERT INTO REP_IMM_MON_Legenda " + sqlCommand, conn);
            int primaryKey = Convert.ToInt32((new SqlCommand("SELECT count(*) from REP_IMM_MON_Legenda", conn)).ExecuteScalar()) + 1;

            for (int i = 1; i <= 10; i++)
            {
                if (legenda != null && (i - 1) < legenda.Count)
                {
                    insertTo.Parameters.AddWithValue("@keyLegenda" + i, legenda[i - 1].id);
                    string value = "";
                    int j = 0;
                    foreach (var item in legenda[i - 1].value) { value += item + (j != legenda[i - 1].value.Count - 1 ? "<br>" : ""); j++; }   
                    insertTo.Parameters.AddWithValue("@valueLegenda" + i, value);
                }
                else
                {
                    insertTo.Parameters.AddWithValue("@keyLegenda" + i, DBNull.Value);
                    insertTo.Parameters.AddWithValue("@valueLegenda" + i, DBNull.Value);
                }
            }

            insertTo.Parameters.AddWithValue("@idREP_IMM_MON_Legenda", primaryKey);
            int rowsUpdatedInserted = insertTo.ExecuteNonQuery();
            if (rowsUpdatedInserted != 0)
                return primaryKey;
            return 0;
        }

        public static List<Legenda> getLegenda(XmlNode attribute)
        {
            List<Legenda> legendaList = new List<Legenda>();
            Legenda legenda = new Legenda();
            List<string> valueList = new List<string>();
            PropertyInfo propertyInfo;

            foreach(XmlNode attr in attribute.ChildNodes)
                foreach (XmlNode innerAttr in attr.ChildNodes)
                {
                    foreach (XmlNode inner in innerAttr.ChildNodes)
                    {
                        propertyInfo = legenda.GetType().GetProperty(inner.Name);
                        if (inner.Name.ToLower().Equals("id"))
                            propertyInfo.SetValue(legenda, Convert.ChangeType(getSymbol(inner.InnerText), propertyInfo.PropertyType), null);
                        else
                        {
                            valueList.Add(inner.InnerText);
                            if(inner.NextSibling == null)
                                propertyInfo.SetValue(legenda, Convert.ChangeType(valueList, propertyInfo.PropertyType), null);
                        }
                    }

                    valueList = new List<string>();
                    legendaList.Add(legenda);
                    legenda = new Legenda();
                }

            return legendaList;
        }

        public static string getSymbol(string symbol)
        {
            switch (symbol)
            {
                case "+":
                    return "plus.png";
                case "++":
                    return "2plus.png";
                case "-":
                    return "minus.png";
                case "--":
                    return "2minus.png";
                case "=":
                    return "equals.png";
                default:
                    return symbol;
            }
        }
    }
}
