using GMW_data;
using System;

namespace GMW.WebUserControls
{
  public partial class mod_verificaDmtx : GMW_data.UserControl
  {
    /// <summary>
    /// Oggetto locale gestione classe datamatrix...
    /// </summary>
    protected DataMatrix DataMatrix_mgr = new DataMatrix();
    protected void Page_Load(object sender, EventArgs e)
    {
      txtBarcode.Focus();
    }
    /// <summary>
    /// controlla se ci sia un barcode
    /// </summary>
    private void checkBarcode()
    {
      bool isDecodOk = false;
      bool isInList = true;

      // verifico
      isDecodOk = (DataMatrix_mgr.taAnagFormDtx.getFormatiCompatibili(barcodeIn).Rows.Count > 0);
      isInList = (DataMatrix_mgr.taVED.getByKey(barcodeIn).Rows.Count > 0);

      // imposto di conseguenza grafica e info
      string classDivDec = "col-sm-6 bg-danger";
      string classDivList = "col-sm-6 bg-danger";
      if (isDecodOk)
      {
        //divDecod.Attributes.Add("class", "col-sm-6 bg-success");
        classDivDec = "col-sm-6 bg-success";
      }
      else
      {
        //divDecod.Attributes.Add("class", "col-sm-6 bg-danger");
        classDivDec = "col-sm-6 bg-danger";
      }
      if (isInList)
      {
        //divElenco.Attributes.Add("class", "col-sm-6 bg-success");
        classDivList = "col-sm-6 bg-success";
      }
      else
      {
        //divElenco.Attributes.Add("class", "col-sm-6 bg-danger");
        classDivList = "col-sm-6 bg-danger";
      }
      // aggiorno visualizzazione...
      if (isDecodOk || isInList)
      {
        lblBarcode.Text = string.Format("Datamatrix: <b>{0}</b>", barcodeIn);
        if (isDecodOk)
        {
          lblEsitoDec.Text = string.Format("{0}: <b>OK</b>", traduci("VerificaDecodifica"));
          DS_DataMatrix.AnagFormatiDtmxRow riga = (DS_DataMatrix.AnagFormatiDtmxRow)DataMatrix_mgr.taAnagFormDtx.getFormatiCompatibili(barcodeIn).Rows[0];
          lblFormatoDmtxDec.Text = string.Format("Formato: <b>{0}</b>", riga.FormatoDtmx);
          // controllo se il PartNum venga decodificato --> lo decodifico e mostro entrambi
          string PartNumIn = barcodeIn.Substring(riga.PartNumberPos - 1, riga.PartNumberLun);
          string PartNum = "";
          string Particolare = "";
          if (DataMatrix_mgr.taTPN.getByPN_IN(PartNumIn).Rows.Count > 0)
          {
            PartNum = ((DS_DataMatrix.TrascPartNumRow)DataMatrix_mgr.taTPN.getByPN_IN(PartNumIn).Rows[0]).PartNumberOUT;
          }
          else
          {
            PartNum = PartNumIn;
          }
          try
          {
            Particolare = ((DS_DataMatrix.TrascDatamatrixRow)DataMatrix_mgr.taTDtmx.getByPnFormato(riga.FormatoDtmx, PartNum).Rows[0]).Particolare;
          }
          catch
          {
            Particolare = "Non Trovato";
            lblEsitoDec.Text += "/<b>ND</b>";
            classDivDec = "col-sm-6 bg-warning";
          }
          lblPartNumDec.Text = string.Format("Part Num: <b>{0}</b>", PartNum);
          // se fosse stato decodificato
          if (PartNum != PartNumIn)
          {
            lblPartNumDec.Text += string.Format("<br><i>({0})</i>", PartNumIn);
          }
          lblCodPartDec.Text = string.Format("Cod Part: <b>{0}</b>", Particolare);
        }
        else
        {
          lblEsitoDec.Text += string.Format("{0}: <b>KO</b>", traduci("VerificaDecodifica"));
          lblFormatoDmtxDec.Text = "...";
          lblPartNumDec.Text = "..";
          lblCodPartDec.Text = ".";
        }
        if (isInList)
        {
          lblEsitoAcq.Text = string.Format("{0}: <b>OK</b>", traduci("VerificaAcquisizioni"));
          DS_DataMatrix.v_ElencoDmtxRow riga = ((DS_DataMatrix.v_ElencoDmtxRow)DataMatrix_mgr.taVED.getByKey(barcodeIn).Rows[0]);
          lblFormatoDmtxAcq.Text = string.Format("Formato MM: <b>{0}</b>", riga.FormatoDtmxMM);
          lblEsitoMarc.Text = string.Format("Esito marcatura MM: <b>{0}</b>", riga.EsitoMarcatura);
          lblPartNumAcq.Text = string.Format("Part Num MM: <b>{0}</b>", riga.PartNumber);
        }
        else
        {
          lblEsitoAcq.Text = string.Format("{0}: <b>KO</b>", traduci("VerificaAcquisizioni"));
          lblFormatoDmtxAcq.Text = "...";
          lblEsitoMarc.Text = "..";
          lblPartNumAcq.Text = ".";
        }
      }
      else
      {
        lblBarcode.Text = string.Format("Codice non riconosciuto: <b>{0}</b>", barcodeIn);
        lblEsitoDec.Text = "....";
        lblFormatoDmtxDec.Text = "...";
        lblPartNumDec.Text = "..";
        lblCodPartDec.Text = ".";
        lblEsitoAcq.Text = "....";
        lblFormatoDmtxAcq.Text = "...";
        lblEsitoMarc.Text = "..";
        lblPartNumAcq.Text = ".";
      }

      // fix grafico
      divDecod.Attributes.Remove("class");
      divDecod.Attributes.Add("class", classDivDec);
      divElenco.Attributes.Remove("class");
      divElenco.Attributes.Add("class", classDivList);

      // sistemazione generale
      barcodeIn = "";
      txtBarcode.Focus();
    }
    /// <summary>
    /// valore barcode
    /// </summary>
    public string barcodeIn
    {
      get
      {
        return txtBarcode.Text.Trim();
      }
      set
      {
        txtBarcode.Text = value;
      }
    }

    protected void txtBarcode_TextChanged(object sender, EventArgs e)
    {
      checkBarcode();
    }
  }
}