using Newtonsoft.Json;
using SteamWare;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Reflection;

namespace GMW_data
{
  /// <summary>
  /// classe gestione utility
  /// </summary>
  public class utils
  {
    #region area table adapters

    public DS_UtilityTableAdapters.STPTableAdapter taSTP;
    public DS_UtilityTableAdapters.v_selTipoCellaTableAdapter taSelTipoCella;
    public DS_UtilityTableAdapters.v_selUdcByPartTableAdapter taUdcByPart;
    public DS_UtilityTableAdapters.v_selDestinatariListePrelievoTableAdapter taDestListePre;
    public DS_UtilityTableAdapters.v_selParticolariTableAdapter taSelPart;
    public DS_UtilityTableAdapters.v_selTipoListaTableAdapter taSelTipoLista;
    public DS_UtilityTableAdapters.v_selOperatoriTableAdapter taSelOperatori;
    public DS_UtilityTableAdapters.v_selParticolariTableAdapter taSelParticolari;
    public DS_UtilityTableAdapters.v_selImballiTableAdapter taSelImballi;
    public DS_UtilityTableAdapters.RemoteRebootLogTableAdapter taRRL;
    public DS_UtilityTableAdapters.RT_PageValTableAdapter taRTPV;

    public DS_batchTableAdapters.AS400_ComandiTableAdapter taAs400;

    /// <summary>
    /// init dei table adapters
    /// </summary>
    protected void initTA()
    {
      taSTP = new GMW_data.DS_UtilityTableAdapters.STPTableAdapter();
      taSelTipoCella = new GMW_data.DS_UtilityTableAdapters.v_selTipoCellaTableAdapter();
      taUdcByPart = new GMW_data.DS_UtilityTableAdapters.v_selUdcByPartTableAdapter();
      taDestListePre = new GMW_data.DS_UtilityTableAdapters.v_selDestinatariListePrelievoTableAdapter();
      taSelPart = new GMW_data.DS_UtilityTableAdapters.v_selParticolariTableAdapter();
      taSelTipoLista = new GMW_data.DS_UtilityTableAdapters.v_selTipoListaTableAdapter();
      taSelOperatori = new DS_UtilityTableAdapters.v_selOperatoriTableAdapter();
      taSelParticolari = new DS_UtilityTableAdapters.v_selParticolariTableAdapter();
      taSelImballi = new DS_UtilityTableAdapters.v_selImballiTableAdapter();
      taRRL = new DS_UtilityTableAdapters.RemoteRebootLogTableAdapter();
      taRTPV = new DS_UtilityTableAdapters.RT_PageValTableAdapter();

      taAs400 = new GMW_data.DS_batchTableAdapters.AS400_ComandiTableAdapter();
    }
    /// <summary>
    /// effettua setup dei connection strings da web.config delal singola applicazione
    /// </summary>
    protected virtual void setupConnectionStringBase()
    {
      string connString = memLayer.ML.confReadString("GMWConnectionString");
      // connections del db
      taSTP.Connection.ConnectionString = connString;
      taSelTipoCella.Connection.ConnectionString = connString;
      taUdcByPart.Connection.ConnectionString = connString;
      taDestListePre.Connection.ConnectionString = connString;
      taSelPart.Connection.ConnectionString = connString;
      taSelTipoLista.Connection.ConnectionString = connString;
      taSelOperatori.Connection.ConnectionString = connString;
      taSelParticolari.Connection.ConnectionString = connString;
      taSelImballi.Connection.ConnectionString = connString;
      taRRL.Connection.ConnectionString = connString;
      taRTPV.Connection.ConnectionString = connString;

      taAs400.Connection.ConnectionString = connString;
    }

    #endregion

    #region Inizializzazione

    protected utils()
    {
      initTA();
      setupConnectionStringBase();
      setupElOperatori();

      // aggiunta x gestione timeout esteso (ove necessario)!
      fixCommandTimeout();
    }

    /// <summary>
    /// sistemazione timeout comandi nei tableadapter
    /// </summary>
    private void fixCommandTimeout()
    {
      SetAllCommandTimeouts(taSTP, memLayer.ML.confReadInt("sqlLongCommandTimeout"));
      SetAllCommandTimeouts(taAs400, memLayer.ML.confReadInt("sqlLongCommandTimeout"));
    }
    void SetAllCommandTimeouts(object adapter, int timeout)
    {
      var commands = adapter.GetType().InvokeMember(
              "CommandCollection",
              BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.NonPublic,
              null, adapter, new object[0]);
      var sqlCommand = (SqlCommand[])commands;
      foreach (var cmd in sqlCommand)
      {
        cmd.CommandTimeout = timeout;
      }
    }
    /// <summary>
    /// oggetto static per fare chiamate sul magazzino
    /// </summary>
    public static utils obj = new utils();

    #endregion

    #region area gestione RTPageVal


    /// <summary>
    /// Valori RT associati a PAGINA + KEY
    /// </summary>
    /// <param name="pagina">Valore chiave</param>
    /// <param name="chiave">Valore chiave</param>
    /// <returns></returns>
    public string RTVal(string pagina, string chiave)
    {
      string answ = "";
      try
      {
        // verifico SE HO valore...
        DS_Utility.RT_PageValDataTable tab = taRTPV.getByPageChiave(pagina, chiave);
        if (tab.Rows.Count > 0)
        {
          answ = tab[0].valore;
        }
      }
      catch
      {
        answ = "";
      }
      return answ;
    }

    #endregion

    #region area operatori

    /// <summary>
    /// oggetto elenco operatori, chiave codOperatore
    /// </summary>
    public Dictionary<string, string> elOperatori;
    /// <summary>
    /// resetta il vocabolario rileggendo i dati...
    /// </summary>
    public void resetElOperatori()
    {
      memLayer.ML.emptyCacheVal("elOperatori");
      setupElOperatori();
    }
    /// <summary>
    /// setup in ram elenco operatori
    /// </summary>
    private void setupElOperatori()
    {
      if (memLayer.ML.isInCacheObject("elOperatori"))
      {
        if (memLayer.ML.cacheOnRedis)
        {
          elOperatori = JsonConvert.DeserializeObject<Dictionary<string, string>>(memLayer.ML.objCacheObj("elOperatori").ToString());
        }
        else
        {
          elOperatori = (Dictionary<string, string>)memLayer.ML.objCacheObj("elOperatori");
        }   
      }
      else
      {
        elOperatori = ricaricaElOperatori();
        if (memLayer.ML.cacheOnRedis)
        {
          string serVal = JsonConvert.SerializeObject(elOperatori);
          memLayer.ML.setCacheVal("elOperatori", serVal, true);
        }
        else
        {
          memLayer.ML.setCacheVal("elOperatori", elOperatori, false);
        }  
      }
    }
    /// <summary>
    /// ricarico elenco operatori
    /// </summary>
    /// <returns></returns>
    private Dictionary<string, string> ricaricaElOperatori()
    {
      Dictionary<string, string> answ = new Dictionary<string, string>();
      foreach (DS_Utility.v_selOperatoriRow riga in taSelOperatori.GetData())
      {
        answ.Add(riga.CodSoggetto, string.Format("{0} {1}", riga.Cognome, riga.Nome));
      }
      return answ;
    }
    /// <summary>
    /// fornisce traduzione cognome nome da cod operatore (tab in ram)
    /// </summary>
    /// <param name="codOperatore"></param>
    /// <returns></returns>
    public string getOperatoreByCod(string codOperatore)
    {
      string answ = "n.d.";
      try
      {
        answ = elOperatori[codOperatore];
      }
      catch
      { }
      return answ;
    }  

    #endregion

    #region area utils x UDC

    /// <summary>
    /// Helper gestioen dati UDC per KIT
    /// </summary>
    public class objUdcKit
    {
      public string UDC;
      public string Particolare;
        public int Qta;
    }

    #endregion
  }
 }
public enum tipoCartellino
{
  cartAL,
  cartALANkit,
  cartALRX,
  cartALTT,
  cartAnime,
  cartFiniti,
  cartFinitiRxSP,
  cartFinitiSP,
  cartFusi,
  cartLiquidiF10,
  cartLiquidiF18,
  cartOdette,
  cartMP,
  cartSterr,
  cartTara,
  cartTratt,
  cartWip,
  cartND
}
public enum tipoSelettore
{
  cdc,
  cellaMagazzino,
  magazzino,
  UDC_byPart
}
