using System;
using System.Collections.Generic;
//using System.Collections;
using System.Text;
using System.Data;
using System.Configuration;
using System.Diagnostics;
using SEILoader.SEI;
using SEILoader.SIMPB;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;
using System.Runtime.CompilerServices;
using System.Threading;
using log4net;

namespace SEILoader
{
    class TngManager
    {
         private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
         
         static string tngServer;
         static string tngJobset;
         static string tngJob;
         static Boolean tngAttivo = false;


         
         static string funzione = "AllineamentoMassivoSEI";
         static string cawTo_Bin = Properties.Settings.Default.cawTo_Bin;
         static string fac_I_100 = Properties.Settings.Default.fac_I_100;
         static string fac_T_100 = Properties.Settings.Default.fac_T_100;
         static string fac_E_100 = Properties.Settings.Default.fac_E_100;

         static string argomentiCawToInizio = "";
         static string argomentiCawToTerminato = "";
         static string argomentiCawToErrore = "";

        public static void init(string[] args)
        {
                tngServer = args[0];
                tngJobset = args[1];
                tngJob = args[2];

                argomentiCawToInizio = "-n " + tngServer + " " + fac_I_100 + " " + tngJobset + " - " + tngJob + " - Funzione " + funzione + " INIZIATA";
                argomentiCawToTerminato = "-n " + tngServer + " " + fac_T_100 + " " + tngJobset + " - " + tngJob + " - Funzione " + funzione + " terminata con SUCCESSO";
                argomentiCawToErrore = "-n " + tngServer + " " + fac_E_100 + " " + tngJobset + " - " + tngJob + " - Funzione "; //+funzione + " terminata con ERRORE";

                tngAttivo = true;
                log.Info("inizializza tng - server=" + tngServer + "jobset=" + tngJobset + "job=" + tngJob);
        }

        public static void avviaTNG() 
        {
            StartTng( cawTo_Bin, argomentiCawToInizio);
        }

        public static void terminaTNG() 
        {
            StartTng( cawTo_Bin, argomentiCawToTerminato);
        }

        public static void erroreTNG() 
        {
            argomentiCawToErrore += funzione + " terminata con ERRORE";
          
            StartTng( cawTo_Bin, argomentiCawToErrore);
        }
       
        
        private static void StartTng( string cawTo_Bin, string argomentiCaw)
        {

            if (!tngAttivo) return;

            log.Info("chiamata TNG processo=" + cawTo_Bin + " argomenti=" + argomentiCaw);
          
            Process processTng = new Process();
            try
            {
                processTng = Process.Start(cawTo_Bin, argomentiCaw);
                while (!processTng.HasExited) ;
            }
            catch (Exception ex)
            {
                
                throw new Exception("cawTo_Bin: Non riesco ad eseguire " + cawTo_Bin + " sulla macchina " + System.Environment.MachineName + ":" + ex.Message);
            }
            finally
            {
                processTng.Close();
                processTng.Dispose();
            }
        }

     


    }
}