using MTC;
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;

namespace MTC_Adapter
{
    #region -- AdapterConf Class --

    /// <summary>
    /// This Configuration class is basically just a set of 
    /// properties with a couple of static methods to manage
    /// the serialization to and deserialization from a
    /// simple XML file.
    /// 
    /// ref: http://www.cambiaresearch.com/articles/33/how-can-i-easily-manage-an-xml-configuration-file-in-dotnet
    /// </summary>
    [Serializable]
    public class AdapterConf
    {
        string sNomeAdapt;
        int nVers;
        double tContOreMaccOn;
        double tContOreMaccLav;
        double tContSlittaTast;
        int[] _ContGiriElettrom;
        float[] _ContKmMovAssi;
        tipoAdapter etipoAdapt;

        element[] _VacuumPump;
        element[] _VacuumAct;
        element[] _Lubro;
        element[] _SlittaMag;
        element[] _ProtMag;
        element[] _Cooler;
        element[] _Press;
        element[] _Temp;
        element[] _Path;
        element[] _UnOp;
        element[] _Axis;
        element[] _MemArea;

        /// <summary>
        /// init conf adapter
        /// </summary>
        public AdapterConf()
        {
            sNomeAdapt = "";
            etipoAdapt = tipoAdapter.DEMO;
        }
        public int nVacuumPump
        {
            get
            {
                int answ = 0;
                if (VacuumPump != null)
                {
                    try
                    {
                        answ = Convert.ToInt32(VacuumPump.Length);
                    }
                    catch
                    { }
                }
                return answ;
            }
        }
        public int nVacuumAct
        {
            get
            {
                int answ = 0;
                if (VacuumAct != null)
                {
                    try
                    {
                        answ = Convert.ToInt32(VacuumAct.Length);
                    }
                    catch
                    { }
                }
                return answ;
            }
        }
        public int nLubro
        {
            get
            {
                int answ = 0;
                if (Lubro != null)
                {
                    try
                    {
                        answ = Convert.ToInt32(Lubro.Length);
                    }
                    catch
                    { }
                }
                return answ;
            }
        }
        public int nSlittaMag
        {
            get
            {
                int answ = 0;
                if (SlittaMag != null)
                {
                    try
                    {
                        answ = Convert.ToInt32(SlittaMag.Length);
                    }
                    catch
                    { }
                }
                return answ;
            }
        }
        public int nProtMag
        {
            get
            {
                int answ = 0;
                if (ProtMag != null)
                {
                    try
                    {
                        answ = Convert.ToInt32(ProtMag.Length);
                    }
                    catch
                    { }
                }
                return answ;
            }
        }
        public int nCooler
        {
            get
            {
                int answ = 0;
                if (Cooler != null)
                {
                    try
                    {
                        answ = Convert.ToInt32(Cooler.Length);
                    }
                    catch
                    { }
                }
                return answ;
            }
        }
        public int nPress
        {
            get
            {
                int answ = 0;
                if (Press != null)
                {
                    try
                    {
                        answ = Convert.ToInt32(Press.Length);
                    }
                    catch
                    { }
                }
                return answ;
            }
        }
        public int nTemp
        {
            get
            {
                int answ = 0;
                if (Temp != null)
                {
                    try
                    {
                        answ = Convert.ToInt32(Temp.Length);
                    }
                    catch
                    { }
                }
                return answ;
            }
        }
        public int nPath
        {
            get
            {
                int answ = 0;
                if (Path != null)
                {
                    try
                    {
                        answ = Convert.ToInt32(Path.Length);
                    }
                    catch
                    { }
                }
                return answ;
            }
        }
        public int nUnOp
        {
            get
            {
                int answ = 0;
                if (UnOp != null)
                {
                    try
                    {
                        answ = Convert.ToInt32(UnOp.Length);
                    }
                    catch
                    { }
                }
                return answ;
            }
        }
        public int nAxis
        {
            get
            {
                int answ = 0;
                if (Axis != null)
                {
                    try
                    {
                        answ = Convert.ToInt32(Axis.Length);
                    }
                    catch
                    { }
                }
                return answ;
            }
        }
        public int nMemArea
        {
            get
            {
                int answ = 0;
                if (MemArea != null)
                {
                    try
                    {
                        answ = Convert.ToInt32(MemArea.Length);
                    }
                    catch
                    { }
                }
                return answ;
            }
        }

        /// <summary>
        /// Serializzazione XML dell'oggetto conf dell'adapter
        /// </summary>
        /// <param name="file"></param>
        /// <param name="c"></param>
        public static void Serialize(string file, AdapterConf c)
        {
            // prima provo a creare il file vuoto...
            if (!File.Exists(file))
            {
                string dirPath = file.Substring(0, file.LastIndexOf('\\'));
                // verifico directory
                if (!Directory.Exists(dirPath))
                {
                    Directory.CreateDirectory(dirPath);
                }
            }
            // salvo effettivamente file...
            System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(c.GetType());
            StreamWriter writer = File.CreateText(file);
            xs.Serialize(writer, c);
            writer.Flush();
            writer.Close();
        }
        /// <summary>
        /// deserializzazione oggetto conf adapter
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        public static AdapterConf Deserialize(string file)
        {
            XmlSerializer xs = new XmlSerializer(typeof(AdapterConf));
            StreamReader reader = File.OpenText(file);
            AdapterConf c = (AdapterConf)xs.Deserialize(reader);
            reader.Close();
            return c;
        }
        /// <summary>
        /// restitusice forma XML grezza del file
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        public static string rawXml(string file)
        {
            string answ = "";
            System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(typeof(AdapterConf));
            StreamReader reader = File.OpenText(file);
            answ = reader.ReadToEnd();
            reader.Close();
            return answ;
        }

        public int Version
        {
            get { return nVers; }
            set { nVers = value; }
        }
        public string NomeAdapt
        {
            get { return sNomeAdapt; }
            set { sNomeAdapt = value; }
        }
        public tipoAdapter TipoAdapt
        {
            get { return etipoAdapt; }
            set { etipoAdapt = value; }
        }
        public double ContOreMaccOn
        {
            get { return tContOreMaccOn; }
            set { tContOreMaccOn = value; }
        }
        public double ContOreMaccLav
        {
            get { return tContOreMaccLav; }
            set { tContOreMaccLav = value; }
        }
        public double ContSlittaTast
        {
            get { return tContSlittaTast; }
            set { tContSlittaTast = value; }
        }
        public int[] ContGiriElettrom
        {
            get { return _ContGiriElettrom; }
            set { _ContGiriElettrom = value; }
        }
        public float[] ContKmMovAssi
        {
            get { return _ContKmMovAssi; }
            set { _ContKmMovAssi = value; }
        }
        public element[] VacuumPump
        {
            get { return _VacuumPump; }
            set { _VacuumPump = value; }
        }
        public element[] VacuumAct
        {
            get { return _VacuumAct; }
            set { _VacuumAct = value; }
        }
        public element[] Lubro
        {
            get { return _Lubro; }
            set { _Lubro = value; }
        }
        public element[] SlittaMag
        {
            get { return _SlittaMag; }
            set { _SlittaMag = value; }
        }
        public element[] ProtMag
        {
            get { return _ProtMag; }
            set { _ProtMag = value; }
        }
        public element[] Cooler
        {
            get { return _Cooler; }
            set { _Cooler = value; }
        }
        public element[] Press
        {
            get { return _Press; }
            set { _Press = value; }
        }
        public element[] Temp
        {
            get { return _Temp; }
            set { _Temp = value; }
        }
        public element[] Path
        {
            get { return _Path; }
            set { _Path = value; }
        }
        public element[] UnOp
        {
            get { return _UnOp; }
            set { _UnOp = value; }
        }
        public element[] Axis
        {
            get { return _Axis; }
            set { _Axis = value; }
        }
        public element[] MemArea
        {
            get { return _MemArea; }
            set { _MemArea = value; }
        }

    }


    [Serializable]
    [XmlType(TypeName = "dataRef")]
    public struct DataRefItem<K, V>
    {
        public K Key { get; set; }
        public V Value { get; set; }

        public DataRefItem(K k, V v) : this() { Key = k; Value = v; }
    }

    /// <summary>
    /// classe elemento base in cui salvare i dati di conf x recupero dati adapters
    /// </summary>
    public class element
    {
        /// <summary>
        /// identificativo univoco x classe di elemento
        /// </summary>
        public string ident;
        /// <summary>
        /// Elenco riferimento dati x recupero (es posizioni memoria separate da #)
        /// </summary>
        public List<DataRefItem<string, string>> dataRefList;

        /// <summary>
        /// init empty
        /// </summary>
        public element()
        {
            ident = "";
            dataRefList = new List<DataRefItem<string, string>>();
        }
        /// <summary>
        /// init element con dati
        /// </summary>
        /// <param name="Idx">Identificativo univoco</param>
        /// <param name="DataRef">Parametri x recupero dati in forma dictionary</param>
        public element(string Idx, List<DataRefItem<string, string>> DataRef)
        {
            ident = Idx;
            dataRefList = DataRef;
        }
    }

    #endregion
}
