using SteamWare;

namespace GMW_data
{
  /// <summary>
  /// fa da proxy singleton x gli oggetti legati ai DATAMATRIX
  /// </summary>
  public class DataMatrix
  {
    #region area table adapters

    public DS_DataMatrixTableAdapters.ElencoDataMatrixTableAdapter taElencoDM;
    public DS_DataMatrixTableAdapters.v_trasfDataMatrixTableAdapter taDtMtxOrig;
    public DS_DataMatrixTableAdapters.stp_verificaDtxTableAdapter taVerifica;
    public DS_DataMatrixTableAdapters.stp_decodeDtxTableAdapter taDecode;
    public DS_DataMatrixTableAdapters.stp_conteggioDtxTableAdapter taConteggio;
    public DS_DataMatrixTableAdapters.v_DtxAcquisitiTableAdapter taDtxAcq;
    public DS_DataMatrixTableAdapters.Dtx2UDCTableAdapter taDtx2UDC;
    public DS_DataMatrixTableAdapters.stp_getFirstLastDtxTableAdapter taFirstLastUdc;
    public DS_DataMatrixTableAdapters.stp_DtxGetAvailFreeAnomTableAdapter taDtxAvailFree;
    public DS_DataMatrixTableAdapters.stp_DtxCheckBlockedTableAdapter taDtxBlocked;
    public DS_DataMatrixTableAdapters.Attr2DtxTableAdapter taAtt2Dtx;
    public DS_DataMatrixTableAdapters.AnagFormatiDtmxTableAdapter taAnagFormDtx;
    public DS_DataMatrixTableAdapters.v_DtxAcquisitiTableAdapter taDtxAcquisiti;
    public DS_DataMatrixTableAdapters.v_ElencoDmtxTableAdapter taVED;
    public DS_DataMatrixTableAdapters.TabSpecDtxTableAdapter taSpecDtmx;
    public DS_DataMatrixTableAdapters.TrascPartNumTableAdapter taTPN;
    public DS_DataMatrixTableAdapters.TrascDatamatrixTableAdapter taTDtmx;
    public DS_DataMatrixTableAdapters.Linea2Dtx_IETableAdapter taL2D;

    /// <summary>
    /// init dei table adapters
    /// </summary>
    protected void initTA()
    {
      taElencoDM = new GMW_data.DS_DataMatrixTableAdapters.ElencoDataMatrixTableAdapter();
      taDtMtxOrig = new GMW_data.DS_DataMatrixTableAdapters.v_trasfDataMatrixTableAdapter();
      taDtMtxOrig = new GMW_data.DS_DataMatrixTableAdapters.v_trasfDataMatrixTableAdapter();
      taVerifica = new DS_DataMatrixTableAdapters.stp_verificaDtxTableAdapter();
      taDecode = new DS_DataMatrixTableAdapters.stp_decodeDtxTableAdapter();
      taConteggio = new DS_DataMatrixTableAdapters.stp_conteggioDtxTableAdapter();
      taDtxAcq = new DS_DataMatrixTableAdapters.v_DtxAcquisitiTableAdapter();
      taDtx2UDC = new DS_DataMatrixTableAdapters.Dtx2UDCTableAdapter();
      taFirstLastUdc = new DS_DataMatrixTableAdapters.stp_getFirstLastDtxTableAdapter();
      taDtxAvailFree = new DS_DataMatrixTableAdapters.stp_DtxGetAvailFreeAnomTableAdapter();
      taDtxBlocked = new DS_DataMatrixTableAdapters.stp_DtxCheckBlockedTableAdapter();
      taAtt2Dtx = new DS_DataMatrixTableAdapters.Attr2DtxTableAdapter();
      taAnagFormDtx = new DS_DataMatrixTableAdapters.AnagFormatiDtmxTableAdapter();
      taDtxAcquisiti = new DS_DataMatrixTableAdapters.v_DtxAcquisitiTableAdapter();
      taVED = new DS_DataMatrixTableAdapters.v_ElencoDmtxTableAdapter();
      taSpecDtmx = new DS_DataMatrixTableAdapters.TabSpecDtxTableAdapter();
      taTPN = new DS_DataMatrixTableAdapters.TrascPartNumTableAdapter();
      taTDtmx = new DS_DataMatrixTableAdapters.TrascDatamatrixTableAdapter();
      taL2D = new DS_DataMatrixTableAdapters.Linea2Dtx_IETableAdapter();
    }
    /// <summary>
    /// effettua setup dei connection strings da web.config delal singola applicazione
    /// </summary>
    protected virtual void setupConnectionStringBase()
    {
      // connections strings del db verso GMW
      string connString = memLayer.ML.confReadString("GMWConnectionString");
      taElencoDM.Connection.ConnectionString = connString;
      taDtMtxOrig.Connection.ConnectionString = connString;
      taVerifica.Connection.ConnectionString = connString;
      taDecode.Connection.ConnectionString = connString;
      taConteggio.Connection.ConnectionString = connString;
      taDtxAcq.Connection.ConnectionString = connString;
      taDtx2UDC.Connection.ConnectionString = connString;
      taFirstLastUdc.Connection.ConnectionString = connString;
      taDtxAvailFree.Connection.ConnectionString = connString;
      taDtxBlocked.Connection.ConnectionString = connString;
      taAtt2Dtx.Connection.ConnectionString = connString;
      taAnagFormDtx.Connection.ConnectionString = connString;
      taDtxAcquisiti.Connection.ConnectionString = connString;
      taVED.Connection.ConnectionString = connString;
      taSpecDtmx.Connection.ConnectionString = connString;
      taTPN.Connection.ConnectionString = connString;
      taTDtmx.Connection.ConnectionString = connString;
      taL2D.Connection.ConnectionString = connString;
    }

    #endregion

    public DataMatrix()
    {
      initTA();
      setupConnectionStringBase();
    }

    /// <summary>
    /// singleton datamatrix
    /// </summary>
    public static DataMatrix mgr = new DataMatrix();
    /// <summary>
    /// effettua import nuovi dati datamatrix/gitterbox
    /// </summary>
    /// <returns></returns>
    public bool importNuoviDati()
    {
      bool answ = false;
      try
      {
        taElencoDM.stp_DtMtrx_import(memLayer.ML.cdv("CodCS"), memLayer.ML.cdvi("IdxPosizioneOdette"), MagClass.magazzino.CodSoggCurrUser);
        answ = true;
      }
      catch
      { }
      return answ;
    }
    /// <summary>
    /// restituisce codice gitterbox dato datamatrix
    /// </summary>
    /// <param name="codDataMatrix"></param>
    /// <returns></returns>
    public string gitterboxFromDataMatrix(decimal codDataMatrix)
    {
      string answ = "";
      try
      {
        answ = taElencoDM.getByCode(codDataMatrix)[0].CodGitterbox;
      }
      catch
      { }
      return answ;
    }
    /// <summary>
    /// restituisce codice gitterbox ORIGINALE (pre import) dato datamatrix
    /// </summary>
    /// <param name="codDataMatrix"></param>
    /// <returns></returns>
    public string gitterboxOrigFromDataMatrix(decimal codDataMatrix)
    {
      string answ = "";
      try
      {
        answ = taDtMtxOrig.getByDtMtx(codDataMatrix.ToString())[0].CodGitterbox;
      }
      catch
      { }
      return answ;
    }
    /// <summary>
    /// verifica SE il codice datamatrix POSSA rappresentare un codice valido, ovvero
    /// - lunghezza compatibile coi formati supportati
    /// - decodifica datamatrix (da formato plausibile, 1 o +) con esito positivo
    /// </summary>
    /// <param name="DataMatrix"></param>
    /// <returns></returns>
    public bool maybeDatamatrix(string DataMatrix)
    {
      bool answ = false;
      // provo a verificare direttamente con score function...
      DS_DataMatrix.AnagFormatiDtmxDataTable tab = taAnagFormDtx.getFormatiCompatibili(DataMatrix);
      if (tab.Rows.Count > 0)
      {
        answ = true;
      }
      return answ;
    }
  }
}
