using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MTC_Adapter
{
    public partial class SetupAdapter : Form
    {
        public SetupAdapter()
        {
            InitializeComponent();

            // fix icon!
            this.Icon = Icon.ExtractAssociatedIcon(defIconFilePath);
        }
        protected string defIconFilePath
        {
            get
            {
                return string.Format(@"{0}\MTCA.ico", utils.confDir);
            }
        }
        /// <summary>
        /// Percorso file completo
        /// </summary>
        protected string filePath
        {
            get
            {
                return string.Format("{0}/{1}", utils.CRS("dataConfPath"), txtFileName.Text);
            }
        }

        protected int numVacPump
        {
            get
            {
                int answ = 0;
                try
                {
                    answ = Convert.ToInt32(nVacPump.Text);
                }
                catch
                { }
                return answ;
            }
            set
            {
                nVacPump.Text = value.ToString();
            }
        }
        protected int numVacAct
        {
            get
            {
                int answ = 0;
                try
                {
                    answ = Convert.ToInt32(nVacAct.Text);
                }
                catch
                { }
                return answ;
            }
            set
            {
                nVacAct.Text = value.ToString();
            }
        }
        protected int numLubro
        {
            get
            {
                int answ = 0;
                try
                {
                    answ = Convert.ToInt32(nLubro.Text);
                }
                catch
                { }
                return answ;
            }
            set
            {
                nLubro.Text = value.ToString();
            }
        }
        protected int numCooler
        {
            get
            {
                int answ = 0;
                try
                {
                    answ = Convert.ToInt32(nCooler.Text);
                }
                catch
                { }
                return answ;
            }
            set
            {
                nCooler.Text = value.ToString();
            }
        }
        protected int numPress
        {
            get
            {
                int answ = 0;
                try
                {
                    answ = Convert.ToInt32(nPress.Text);
                }
                catch
                { }
                return answ;
            }
            set
            {
                nPress.Text = value.ToString();
            }
        }
        protected int numTemp
        {
            get
            {
                int answ = 0;
                try
                {
                    answ = Convert.ToInt32(nTempe.Text);
                }
                catch
                { }
                return answ;
            }
            set
            {
                nTempe.Text = value.ToString();
            }
        }
        protected int numPath
        {
            get
            {
                int answ = 0;
                try
                {
                    answ = Convert.ToInt32(nPath.Text);
                }
                catch
                { }
                return answ;
            }
            set
            {
                nPath.Text = value.ToString();
            }
        }
        protected int numUnOp
        {
            get
            {
                int answ = 0;
                try
                {
                    answ = Convert.ToInt32(nUnOp.Text);
                }
                catch
                { }
                return answ;
            }
            set
            {
                nUnOp.Text = value.ToString();
            }
        }
        protected int numAxis
        {
            get
            {
                int answ = 0;
                try
                {
                    answ = Convert.ToInt32(nAssi.Text);
                }
                catch
                { }
                return answ;
            }
            set
            {
                nAssi.Text = value.ToString();
            }
        }

        private void btnCreateFile_Click(object sender, EventArgs e)
        {
            // creo un nuovo file adapter...
            AdapterConf c = new AdapterConf();
            // nome da txtbox, vers 1
            c.NomeAdapt = txtAdapter.Text;
            c.Version = 1;
            // tipo da selettore
            string valTipo = "";
            try
            {
                valTipo = cbTipoAdapt.SelectedItem.ToString().ToUpper();
            }
            catch
            {
                valTipo = cbTipoAdapt.SelectedText;
            }
            switch (valTipo)
            {
                case "FANUC":
                    c.TipoAdapt = tipoAdapter.FANUC;
                    break;
                case "SIEMENS":
                    c.TipoAdapt = tipoAdapter.SIEMENS;
                    break;
                case "HMI-ESA":
                case "HMI_ESA":
                case "ESAGV":
                    c.TipoAdapt = tipoAdapter.ESAGV;
                    break;
                case "DEMO":
                default:
                    c.TipoAdapt = tipoAdapter.DEMO;
                    break;
            }

            // init lista parametri
            List<DataRefItem<string, string>> listaDR;

            // VAC PUMP
            if (numVacPump > 0)
            {
                // creo elementi richiesti
                element[] VacuumPump = new element[numVacPump];
                // ciclo x istanziare!
                for (int i = 0; i < numVacPump; i++)
                {
                    // inizializzo list x parametri
                    listaDR = new List<DataRefItem<string, string>>();
                    listaDR.Add(new DataRefItem<string, string>(string.Format("VacPump_{0:00}_Status", i + 1), "0"));
                    listaDR.Add(new DataRefItem<string, string>(string.Format("VacPump_{0:00}_WrkTime", i + 1), "0"));
                    VacuumPump[i] = new element(string.Format("VacPump_{0:00}", i + 1), string.Format("Pompa {0}", i + 1), fonteDati.Random, listaDR);
                }
                // salvo oggetto
                c.VacuumPump = VacuumPump;
            }

            // VACUUM ACT
            if (numVacAct > 0)
            {
                // creo elementi richiesti
                element[] VacuumAct = new element[numVacAct];
                // ciclo x istanziare!
                for (int i = 0; i < numVacAct; i++)
                {
                    // inizializzo list x parametri
                    listaDR = new List<DataRefItem<string, string>>();
                    listaDR.Add(new DataRefItem<string, string>(string.Format("VacAct_{0:00}_Count", i + 1), "0"));
                    VacuumAct[i] = new element(string.Format("VacAct_{0:00}", i + 1), string.Format("Attuatore vuoto {0}", i + 1), fonteDati.Random, listaDR);
                }
                // salvo oggetto
                c.VacuumAct = VacuumAct;
            }

            // LUBRO
            if (numLubro > 0)
            {
                // creo elementi richiesti
                element[] Lubro = new element[numLubro];
                // ciclo x istanziare!
                for (int i = 0; i < numLubro; i++)
                {
                    // inizializzo list x parametri
                    listaDR = new List<DataRefItem<string, string>>();
                    listaDR.Add(new DataRefItem<string, string>(string.Format("Lubro_{0:00}_Num", i + 1), "0"));
                    Lubro[i] = new element(string.Format("Lubro_{0:00}", i + 1), string.Format("Lubrorefrigerante {0}", i + 1), fonteDati.Random, listaDR);
                }
                // salvo oggetto
                c.Lubro = Lubro;
            }

            // COOLER
            if (numCooler > 0)
            {
                // creo elementi richiesti
                element[] Cooler = new element[numCooler];
                // ciclo x istanziare!
                for (int i = 0; i < numCooler; i++)
                {
                    // inizializzo list x parametri
                    listaDR = new List<DataRefItem<string, string>>();
                    listaDR.Add(new DataRefItem<string, string>(string.Format("Cooler_{0:00}_Status", i + 1), "0"));
                    Cooler[i] = new element(string.Format("Cooler_{0:00}", i + 1), string.Format("Cooler {0}", i + 1), fonteDati.Random, listaDR);
                }
                // salvo oggetto
                c.Cooler = Cooler;
            }

            // PRESS
            if (numPress > 0)
            {
                // creo elementi richiesti
                element[] Press = new element[numPress];
                // ciclo x istanziare!
                for (int i = 0; i < numPress; i++)
                {
                    // inizializzo list x parametri
                    listaDR = new List<DataRefItem<string, string>>();
                    listaDR.Add(new DataRefItem<string, string>(string.Format("Press_{0:00}_Value", i + 1), "0"));
                    Press[i] = new element(string.Format("Press_{0:00}", i + 1), string.Format("Pressostato {0}", i + 1), fonteDati.Random, listaDR);
                }
                // salvo oggetto
                c.Press = Press;
            }

            // TEMPERATURE
            if (numTemp > 0)
            {
                // creo elementi richiesti
                element[] Temp = new element[numTemp];
                // ciclo x istanziare!
                for (int i = 0; i < numTemp; i++)
                {
                    // inizializzo list x parametri
                    listaDR = new List<DataRefItem<string, string>>();
                    listaDR.Add(new DataRefItem<string, string>(string.Format("Temp_{0:00}_Value", i + 1), "0"));
                    Temp[i] = new element(string.Format("Temp_{0:00}", i + 1), string.Format("Sonda Temperatura {0}", i + 1), fonteDati.Random, listaDR);
                }
                // salvo oggetto
                c.Temp = Temp;
            }

            // PATH
            if (numPath > 0)
            {
                // creo elementi richiesti
                element[] Path = new element[numPath];
                // ciclo x istanziare!
                for (int i = 0; i < numPath; i++)
                {
                    // inizializzo list x parametri
                    listaDR = new List<DataRefItem<string, string>>();
                    listaDR.Add(new DataRefItem<string, string>(string.Format("Path_{0:00}_FeedRate", i + 1), "0"));
                    listaDR.Add(new DataRefItem<string, string>(string.Format("Path_{0:00}_FeedOverr", i + 1), "0"));
                    listaDR.Add(new DataRefItem<string, string>(string.Format("Path_{0:00}_RapidOverr", i + 1), "0"));
                    listaDR.Add(new DataRefItem<string, string>(string.Format("Path_{0:00}_PosActX", i + 1), "0"));
                    listaDR.Add(new DataRefItem<string, string>(string.Format("Path_{0:00}_PosActY", i + 1), "0"));
                    listaDR.Add(new DataRefItem<string, string>(string.Format("Path_{0:00}_PosActZ", i + 1), "0"));
                    listaDR.Add(new DataRefItem<string, string>(string.Format("Path_{0:00}_PosActI", i + 1), "0"));
                    listaDR.Add(new DataRefItem<string, string>(string.Format("Path_{0:00}_PosActJ", i + 1), "0"));
                    listaDR.Add(new DataRefItem<string, string>(string.Format("Path_{0:00}_PosActK", i + 1), "0"));
                    listaDR.Add(new DataRefItem<string, string>(string.Format("Path_{0:00}_Type", i + 1), "0"));
                    // aggiunta x revisione 2016.05.05
                    listaDR.Add(new DataRefItem<string, string>(string.Format("Path_{0:00}_PartId", i + 1), "0"));
                    listaDR.Add(new DataRefItem<string, string>(string.Format("Path_{0:00}_PZ_TOT", i + 1), "0"));
                    listaDR.Add(new DataRefItem<string, string>(string.Format("Path_{0:00}_Cod_M", i + 1), "0"));
                    listaDR.Add(new DataRefItem<string, string>(string.Format("Path_{0:00}_Cod_S", i + 1), "0"));
                    listaDR.Add(new DataRefItem<string, string>(string.Format("Path_{0:00}_Cod_T", i + 1), "0"));
                    listaDR.Add(new DataRefItem<string, string>(string.Format("Path_{0:00}_RUN_MODE", i + 1), "0"));
                    listaDR.Add(new DataRefItem<string, string>(string.Format("Path_{0:00}_EXE_MODE", i + 1), "0"));
                    listaDR.Add(new DataRefItem<string, string>(string.Format("Path_{0:00}_CurrProg", i + 1), "0"));
                    listaDR.Add(new DataRefItem<string, string>(string.Format("Path_{0:00}_CurrProg_RowNum", i + 1), "0"));
                    listaDR.Add(new DataRefItem<string, string>(string.Format("Path_{0:00}_ActiveAxes", i + 1), "0"));
                    // aggiunto 2016.05.10
                    listaDR.Add(new DataRefItem<string, string>(string.Format("Path_{0:00}_CodG_Act", i + 1), "0"));
                    listaDR.Add(new DataRefItem<string, string>(string.Format("Path_{0:00}_SubMode", i + 1), "0"));

                    Path[i] = new element(string.Format("Path_{0:00}", i + 1), string.Format("Path num {0}", i + 1), fonteDati.Random, listaDR);
                }
                // salvo oggetto
                c.Path = Path;
            }

            // UNITA OPERATRICI
            if (numUnOp > 0)
            {
                // creo elementi richiesti
                element[] UnOp = new element[numUnOp];
                // ciclo x istanziare!
                for (int i = 0; i < numUnOp; i++)
                {
                    // inizializzo list x parametri
                    listaDR = new List<DataRefItem<string, string>>();
                    listaDR.Add(new DataRefItem<string, string>(string.Format("UnOp_{0:00}_ToolId", i + 1), "0"));
                    listaDR.Add(new DataRefItem<string, string>(string.Format("UnOp_{0:00}_NumCU", i + 1), "0"));
                    listaDR.Add(new DataRefItem<string, string>(string.Format("UnOp_{0:00}_Status", i + 1), "0"));
                    listaDR.Add(new DataRefItem<string, string>(string.Format("UnOp_{0:00}_VitaRes", i + 1), "0"));
                    listaDR.Add(new DataRefItem<string, string>(string.Format("UnOp_{0:00}_Speed", i + 1), "0"));
                    listaDR.Add(new DataRefItem<string, string>(string.Format("UnOp_{0:00}_SpeedOverr", i + 1), "0"));
                    listaDR.Add(new DataRefItem<string, string>(string.Format("UnOp_{0:00}_Load", i + 1), "0"));
                    listaDR.Add(new DataRefItem<string, string>(string.Format("UnOp_{0:00}_AccTime", i + 1), "0"));
                    listaDR.Add(new DataRefItem<string, string>(string.Format("UnOp_{0:00}_VitaResType", i + 1), "0"));
                    UnOp[i] = new element(string.Format("UnOp_{0:00}", i + 1), string.Format("Unita Op num {0}", i + 1), fonteDati.Random, listaDR);
                }
                // salvo oggetto
                c.UnOp = UnOp;
            }

            // ASSI
            if (numAxis > 0)
            {
                // creo elementi richiesti
                element[] Axis = new element[numAxis];
                // ciclo x istanziare!
                for (int i = 0; i < numAxis; i++)
                {
                    // inizializzo list x parametri
                    listaDR = new List<DataRefItem<string, string>>();
                    listaDR.Add(new DataRefItem<string, string>(string.Format("Axis_{0:00}_MainProc", i + 1), "0"));
                    listaDR.Add(new DataRefItem<string, string>(string.Format("Axis_{0:00}_IsMast", i + 1), "0"));
                    listaDR.Add(new DataRefItem<string, string>(string.Format("Axis_{0:00}_MastId", i + 1), "0"));
                    listaDR.Add(new DataRefItem<string, string>(string.Format("Axis_{0:00}_Type", i + 1), "0"));
                    listaDR.Add(new DataRefItem<string, string>(string.Format("Axis_{0:00}_Dir", i + 1), "0"));
                    listaDR.Add(new DataRefItem<string, string>(string.Format("Axis_{0:00}_Load", i + 1), "0"));
                    listaDR.Add(new DataRefItem<string, string>(string.Format("Axis_{0:00}_PosAct", i + 1), "0"));
                    listaDR.Add(new DataRefItem<string, string>(string.Format("Axis_{0:00}_PosTgt", i + 1), "0"));
                    listaDR.Add(new DataRefItem<string, string>(string.Format("Axis_{0:00}_FeedAct", i + 1), "0"));
                    listaDR.Add(new DataRefItem<string, string>(string.Format("Axis_{0:00}_FeedOver", i + 1), "0"));
                    listaDR.Add(new DataRefItem<string, string>(string.Format("Axis_{0:00}_AccelAct", i + 1), "0"));
                    listaDR.Add(new DataRefItem<string, string>(string.Format("Axis_{0:00}_AccTime", i + 1), "0"));
                    listaDR.Add(new DataRefItem<string, string>(string.Format("Axis_{0:00}_Battery", i + 1), "0"));
                    listaDR.Add(new DataRefItem<string, string>(string.Format("Axis_{0:00}_DistDone", i + 1), "0"));
                    listaDR.Add(new DataRefItem<string, string>(string.Format("Axis_{0:00}_InvDDone", i + 1), "0"));
                    Axis[i] = new element(string.Format("Axis_{0:00}", i + 1), string.Format("Asse num {0}", i + 1), fonteDati.Random, listaDR);
                }
                // salvo oggetto
                c.Axis = Axis;
            }

            // Serializzo oggetto conf su file
            AdapterConf.Serialize(filePath, c);
        }

        private void btnLoad_Click(object sender, EventArgs e)
        {
            // Read the configuration object from a file
            AdapterConf c2 = AdapterConf.Deserialize(filePath);

            txtAdapter.Text = c2.NomeAdapt;
            cbTipoAdapt.SelectedText = c2.TipoAdapt.ToString();
            //cbTipoAdapt.SelectedItem = c2.TipoAdapt;
            numVacPump = c2.nVacuumPump;
            numVacAct = c2.nVacuumAct;
            numLubro = c2.nLubro;
            numCooler = c2.nCooler;
            numPress = c2.nPress;
            numTemp = c2.nTemp;
            numPath = c2.nPath;
            numUnOp = c2.nUnOp;
            numAxis = c2.nAxis;
        }
    }
}
