using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using LLNet.Utils;
using LLNet.GUI;

namespace LLNet.Tienda.MainForms
{
    public partial class frmOpenPOS : GUI.FormBases.frmBase
    {
        string url;

        public frmOpenPOS()
        {
            InitializeComponent();
            txtMonto.EditValue = 0.00;

            GUI.CApp.TraducirForm(this);

            //Dirección web del wcf EVERTEC
            this.url = Helpers.Nvl(CApp.GetParam("BDW"), "").ToString();

            CambiarKey();
        }

        #region Echo
        /// <summary>
        /// Prueba de conexión
        /// </summary>
        private bool Echo()
        {
            bool exito = false;
            try
            {
                EVERTECEPS _Service = new EVERTECEPS(url);

                string result = _Service.EchoTest("EchoTest");
                exito = true;
            }
            catch (Exception ex)
            {
                exito = false;
            }

            return exito;
        }
        #endregion

        #region CambiarKey
        /// <summary>
        /// Verifica si la key para las transacciones con el BCR está vencida o no,
        /// si lo está, la cambia
        /// </summary>
        private void CambiarKey()
        {
            Utils.QueryParams qp = new QueryParams();
            qp.AddReplaceParam("CAJA", Program.Caja.ToString());
            string fecha = Helpers.Nvl(GUI.CApp.bll.GetSingleValue("ObtFechaK", qp, "PDV"), "").ToString();

            if (fecha != "")
            {
                //if (fecha.Substring(0, 10) == DateTime.Today.ToShortDateString())
                if (DateTime.Parse(fecha.Substring(0, 10)) <= DateTime.Parse(DateTime.Today.ToShortDateString()))
                {
                    if (url != "")
                    {
                        if (Echo())
                        {
                            string CardAcqID = Helpers.Nvl(CApp.GetParam("CAI"), "").ToString();

                            if (CardAcqID != "")
                            {
                                DataSet ds_datos = CApp.bll.getDataSet("PDV", "ObtTermKey", qp);
                                if (ds_datos.Tables.Count > 0 && ds_datos.Tables[0].Rows.Count > 0)
                                {
                                    qp = new QueryParams();
                                    qp.AddReplaceParam("SECUENCIA", "WMS_S_IDLOG");
                                    string secuencia = Helpers.Nvl(CApp.bll.GetSingleValue("SECUENCIA", qp, "General"), "").ToString();

                                    //Log Inicio
                                    qp = new QueryParams();
                                    qp.AddReplaceParam("IDLOG", secuencia);
                                    string texto = CardAcqID + "," + ds_datos.Tables[0].Rows[0].ItemArray[0].ToString() + "," +
                                        ds_datos.Tables[0].Rows[0].ItemArray[1].ToString();
                                    qp.AddReplaceParam("TEXTO", texto);
                                    qp.AddReplaceParam("CAJA", Program.Caja.ToString());
                                    GUI.CApp.bll.ExecuteSql("WMS_LOG_INI_BCR", qp, "PDV");

                                    EVERTECEPS _Service = new EVERTECEPS(url);
                                    string key = _Service.KeyExchange(CardAcqID, ds_datos.Tables[0].Rows[0].ItemArray[0].ToString(),
                                        ds_datos.Tables[0].Rows[0].ItemArray[1].ToString());

                                    //Log Fin
                                    qp = new QueryParams();
                                    qp.AddReplaceParam("IDLOG", secuencia);
                                    texto = key;
                                    qp.AddReplaceParam("TEXTO", texto);
                                    GUI.CApp.bll.ExecuteSql("WMS_LOG_FIN_BCR", qp, "PDV");

                                    qp = new QueryParams();
                                    qp.AddReplaceParam("CAJA", Program.Caja.ToString());
                                    qp.AddReplaceParam("KEY", key);
                                    GUI.CApp.bll.ExecuteSql("AlmKey", qp, "PDV");
                                }
                            }
                        }
                    }
                }
            }
        }
        #endregion 

        private void frmOpenPOS_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Enter :
                case Keys.F12:
                    this.DialogResult = DialogResult.OK;
                    break;
                case Keys.Escape :
                    this.DialogResult = DialogResult.Cancel;
                    break;
            }
        }

        private void btnAceptar_Click(object sender, EventArgs e)
        {

        }
    }
}