//========================================================================
// Copyright(C): RFAI France
//
// CLR Version : 4.0.30319.18052
// NameSpace : Matching
// FileName : ClusterInformation
//
// Created by : JC (jiangchar@gamil.com) at 2/5/2014 10:59:30 PM
//
//========================================================================
#region Using directives

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

#endregion

namespace Matching
{
    /// <summary>
    /// This class contains the information of ClusterInfo.xml
    /// </summary>
    class ClusterInformation
    {
        /***************************************************************/
        /*************************Attributs*****************************/
        /***************************************************************/

        #region Attributs

        //labels : the group number
        List<int> listClusterLabel;
        //means : the center of every group
        List<VectorFMGE> listCenter;
        //cliques : the names of cliques
        List<List<String>> listCliques;

        #endregion

        /***************************************************************/
        /*************************Accesseurs****************************/
        /***************************************************************/

        #region Accesseurs

        public List<int> ListClusterLabel
        {
            get { return listClusterLabel; }
            set { listClusterLabel = value; }
        }

        public List<VectorFMGE> ListCenter
        {
            get { return listCenter; }
            set { listCenter = value; }
        }

        public List<List<String>> ListCliques
        {
            get { return listCliques; }
            set { listCliques = value; }
        }


        #endregion

        /***************************************************************/
        /************************Constructeurs**************************/
        /***************************************************************/

        #region Constructeurs

        public ClusterInformation()
        {

             listClusterLabel = new List<int>();

             ListCenter = new List<VectorFMGE>();

             listCliques = new List<List<String>> ();

        }

        #endregion

        /***************************************************************/
        /*********************Méthodes publics**************************/
        /***************************************************************/

        #region Méthodes publics

        /// <summary>
        /// Save the object of this class into XML
        /// </summary>
        /// <param name="StrDictionary">The directory of XML</param>
        public void SaveAsXML(string StrDictionary)
        {
            //verifier que tout les processuss sont fini.
            if (ListCenter.Count == 0)
            {
                Console.Write("Cluster information empty. \n");
                return;
            }
            if (ListClusterLabel.Count == 0)
            {
                Console.Write("Cluster information empty. \n");
                return;
            }
            if (ListCliques.Count == 0)
            {
                Console.Write("Cluster information empty. \n");
                return;
            }
            //on commence à ecrire
            try
            {
                XmlDocument XmlWriFGM = new XmlDocument();
                XmlElement rootElement = XmlWriFGM.CreateElement("ClusterInfo");
                XmlWriFGM.AppendChild(rootElement);
                for (int i = 0; i < ListClusterLabel.Count; i++)
                {
                    XmlElement XmlWriFGM_1 = XmlWriFGM.CreateElement("Cluster_label");
                    XmlWriFGM_1.SetAttribute("Label", ListClusterLabel[i].ToString());
                    rootElement.AppendChild(XmlWriFGM_1);
                    XmlElement XmlWriFGM_1_1 = XmlWriFGM.CreateElement("FGME_means");
                    XmlWriFGM_1_1.InnerText = ListCenter[i].GetString();
                    XmlWriFGM_1.AppendChild(XmlWriFGM_1_1);
                    XmlElement XmlWriFGM_1_2 = XmlWriFGM.CreateElement("List_cliques");

                    //-----------concat le ListCliques------------------------------------------------
                    var builder = new StringBuilder();
                    string StrListClique = "";

                    foreach (string str in ListCliques[i])
                    {
                        if (str.Contains(" "))
                            throw new Exception("clique name cannot contain blank!");
                        builder.Append(str + " ");
                        //StrListClique += str + " ";
                    }
                    StrListClique = builder.ToString();
                    if (StrListClique != "")
                        StrListClique = StrListClique.Substring(0, StrListClique.LastIndexOf(" "));
                    else
                        StrListClique = "";
                    //--------------------------------------------------------------------------------

                    XmlWriFGM_1_2.InnerText = StrListClique;
                    XmlWriFGM_1.AppendChild(XmlWriFGM_1_2);
                }
                XmlWriFGM.Save(StrDictionary + "\\ClusterInfo.xml");
            }
            catch (Exception e)
            {
                Console.Write(e.Message);
                throw new Exception("Fail to write into XML. \n", e);
            }
        }

        /// <summary>
        /// Read a XML into an object of this class
        /// </summary>
        /// <param name="StrDictionary">The directory of XML</param>
        public void ReadFromXML(string StrDictionary)
        {
            XmlDocument XmlReaFGM = new XmlDocument();
            try
            {
                XmlReaFGM.Load(StrDictionary + "\\ClusterInfo.xml");
            }
            catch (Exception e)
            {
                Console.Write(e.Message);
                throw new Exception("Fail to read XML. \n", e);
            }
            //trouver la racine
            XmlNode XmlReaFGM_root = XmlReaFGM.SelectSingleNode("ClusterInfo");
            //trouver la liste de filles de la racine
            XmlNodeList XmlReaFGM_NodeList = XmlReaFGM_root.ChildNodes;
            foreach (XmlNode node in XmlReaFGM_NodeList)
            {
                XmlElement XmlEeaFGM_Elem = (XmlElement)node;
                XmlElement XmlEeaFGM_means = (XmlElement)node.FirstChild;
                XmlElement XmlEeaFGM_cliques = (XmlElement)node.LastChild;
                String s = XmlEeaFGM_Elem.GetAttribute("Label");
                Convert.ToInt32(XmlEeaFGM_Elem.GetAttribute("Label"));
                ListClusterLabel.Add(Convert.ToInt32(XmlEeaFGM_Elem.GetAttribute("Label")));

                ListCenter.Add(new VectorFMGE(XmlEeaFGM_means.InnerText));
                List<String> cliques = new List<String>();
                cliques = XmlEeaFGM_cliques.InnerText.Split(' ').ToList();
                ListCliques.Add(cliques);
            }
        }

        #endregion

    }
}
