using System;
using System.Threading;
using System.Web.UI;

namespace NKC_WF.WebUserControls
{
    public partial class cmp_barcode : BaseUserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                resetInput();
                resetMessage();
            }
            else if (string.IsNullOrEmpty(inputAcquired))
            {
                checkRaiseEv();
            }
        }
        /// <summary>
        /// evento modifica testo
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void txtBarcode_TextChanged(object sender, EventArgs e)
        {
            checkRaiseEv();
        }
        /// <summary>
        /// Verifica se inviare evento
        /// </summary>
        private void checkRaiseEv()
        {
            if (valueRead != inputAcquired)
            {
                // registro NUOVO cmd
                inputAcquired = valueRead;
                // alzo evento SOLO SE il nuovo valore è diverso da null...
                if (!string.IsNullOrEmpty(inputAcquired))
                {
                    raiseEvent();
                }
            }
            else
            {
                if (string.IsNullOrEmpty(inputAcquired))
                {
                    resetInput();
                }
            }
            resetInput();
        }
        /// <summary>
        /// Comando acquisito da Barcode
        /// </summary>
        public string inputAcquired
        {
            get
            {
                return hfLastCmd.Value;
            }
            set
            {
                hfLastCmd.Value = value;
            }
        }
        /// <summary>
        /// Valore acquisito in lettura barcode
        /// </summary>
        protected string valueRead
        {
            get
            {
                return txtBarcode.Text.Trim();
            }
            set
            {
                txtBarcode.Text = value;
            }
        }
        /// <summary>
        /// Reset input del barcode SENZA generare evento...
        /// </summary>
        /// <returns></returns>
        public void resetInput()
        {
            valueRead = "";
            txtBarcode.Focus();
        }
        /// <summary>
        /// reset messaggio
        /// </summary>
        public void resetMessage()
        {
            lbtReset.Visible = false;
            lblOutput.Text = "";
        }
        /// <summary>
        /// Gestione output da mostrare (opzionale
        /// </summary>
        /// <param name="cssClass">Enum delle classi permesse</param>
        /// <param name="messaggio"></param>
        public void showOutput(AppData.cssClass cssClass, string messaggio)
        {
            // In primis: mostro qualcosa SOLO SE ho del testo
            lbtReset.Visible = !string.IsNullOrEmpty(messaggio);
            lblOutput.Text = messaggio;
            //lbtReset.Attributes.Remove("class");
            //lbtReset.Attributes.Add("class", $"btn btn-block {cssClass}");
            lbtReset.CssClass = $"btn btn-sm btn-block py-0 btn-{cssClass}";
        }
        /// <summary>
        /// reset lettura
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lbtReset_Click(object sender, EventArgs e)
        {
            Response.Redirect(Request.RawUrl);
        }
    }

}