using System;
using Microsoft.Win32;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;



namespace LLNet.Tienda.MainForms.Forms
{
    public partial class frmConfiguration : GUI.FormBases.frmBase
    {
        public string MiFormato;  // GUSTAVO MIRANDA
        public static string isIBM = "";


        public frmConfiguration()
        {
            InitializeComponent();

            GUI.CApp.TraducirForm(this);
            
        }

        private void frmConfiguration_Load(object sender, EventArgs e)
        {
            // Lee el archivo de conexiones
            dsConexiones.Clear();
            dsConexiones.ReadXml(GUI.CApp.ExeDir + "Conexiones.xml");

            DataSet dsIdiomas = new DataSet();
            dsIdiomas.ReadXml(GUI.CApp.ExeDir + "Idiomas.xml");
            this.lueIdioma.DataSource = dsIdiomas.Tables[0];
            this.lueIdioma.DisplayMember = "Nombre";
            this.lueIdioma.ValueMember = "Nombre"; 

            RegistryKey key = Registry.CurrentUser;
            key = key.OpenSubKey(@"Software\Logistika\POS Retail 8.0.0", true);
            if (key != null)
            {
                rowCaja.Properties.Value = key.GetValue("Caja", 1);
                rowEsquema.Properties.Value = key.GetValue("Conexion", "");
                rowRuta.Properties.Value = key.GetValue("Path", "");
                rowLicencia.Properties.Value = key.GetValue("Licence", "");
                rowIdioma.Properties.Value = key.GetValue("Idioma", "");
                rowLineas.Properties.Value = key.GetValue("LineasDocumento", "");
                MiFormato = Convert.ToString(key.GetValue("FormatoImpresion", ""));   // GUSTAVO MIRANDA
                erTipoLector.Properties.Value = key.GetValue("TipoLector", "");//Tipo Card Reader
                erNombreCR.Properties.Value = key.GetValue("NombreLector", "");
                erPuertoLR.Properties.Value = key.GetValue("LectorBAC", "");
                GUI.CApp.vLectorBac = erPuertoLR.Properties.Value.ToString();

                rowIBM.Properties.Value = key.GetValue("IBM", ""); //EMANUEL CARVAJAL MARÌN 
                isIBM = Convert.ToString(rowIBM.Properties.Value = key.GetValue("IBM", ""));
               
                
                switch(MiFormato)
                    {
                    case "0":
                         rowFormato.Properties.Value = "0 - Ticket";
                         break;
                    case "1":
                         rowFormato.Properties.Value = "1 - PreImpreso";
                         break;
                    default:
                        rowFormato.Properties.Value = "2 - Base de Datos";
                        break;
                    }
              //  rowFormato.Properties.Value = key.GetValue("FormatoImpresion", "");  // GUSTAVO MIRANDA

                key = key.OpenSubKey(@"Impresoras\Ticket", true);

                if (key != null)
                {
                    rowFactTkt.Properties.Value = key.GetValue("Factura", "");
                    rowNCTkt.Properties.Value = key.GetValue("Nota de Credito", "");
                    rowValeTkt.Properties.Value = key.GetValue("Vale", "");                    
                }
                else
                {
                    key = Registry.CurrentUser.OpenSubKey(@"Software\Logistika\POS Retail 8.0.0", true);
                    key.CreateSubKey("Impresoras");
                    key = key.OpenSubKey("Impresoras", true);
                    key.CreateSubKey("Ticket");
                }

                key = Registry.CurrentUser.OpenSubKey(@"Software\Logistika\POS Retail 8.0.0\Impresoras\Preimpresos", true);
                    
                if (key != null)
                {
                    rowFactPre.Properties.Value = key.GetValue("Factura", "");
                    rowNCPre.Properties.Value = key.GetValue("Nota de Credito", "");
                    rowValePre.Properties.Value = key.GetValue("Vale", "");
                    rowPagarePre.Properties.Value = key.GetValue("Pagare", "");
                    rowGarantiaPre.Properties.Value = key.GetValue("Garantia", "");
                    rowCertificadoPre.Properties.Value = key.GetValue("Certificado", "");
                    rowImpresora_Arqueos.Properties.Value = key.GetValue("Arqueos", "");
                }
                else
                {
                    key = Registry.CurrentUser.OpenSubKey(@"Software\Logistika\POS Retail 8.0.0\Impresoras", true);
                    key.CreateSubKey("Preimpresos");
                }
                    
                }

            }

        private void frmConfiguration_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            { 
                case Keys.F10:
                    btnReportes_Click(null, null);
                    break;
                case Keys.F11:
                    btnGarantias_Click(null, null);
                    break;
                case Keys.F12:
                    btnAceptar_Click(null, null);
                    break;
                case Keys.Escape:
                    btnCancelar.PerformClick();
                    break;
            }
        }

        private void btnAceptar_Click(object sender, EventArgs e)
        {
            RegistryKey key = Registry.CurrentUser;
            key = key.OpenSubKey(@"Software\Logistika\POS Retail 8.0.0", true);

            key.SetValue("Caja", rowCaja.Properties.Value);
            key.SetValue("Conexion", rowEsquema.Properties.Value);
            key.SetValue("Path", rowRuta.Properties.Value);
            key.SetValue("Licence", rowLicencia.Properties.Value);
            key.SetValue("Idioma", rowIdioma.Properties.Value);
            key.SetValue("LineasDocumento", rowLineas.Properties.Value);
            MiFormato = Convert.ToString(rowFormato.Properties.Value);  // GUSTAVO MIRANDA
            MiFormato = MiFormato.Substring(0, 1);  // GUSTAVO MIRANDA
            key.SetValue("FormatoImpresion", MiFormato); // GUSTAVO MIRANDA
            key.SetValue("IBM", rowIBM.Properties.Value);
            key.SetValue("TipoLector", erTipoLector.Properties.Value);//Tipo Card Reader
            key.SetValue("NombreLector", erNombreCR.Properties.Value);
            key.SetValue("LectorBAC", erPuertoLR.Properties.Value); //Lector BAC
            GUI.CApp.vLectorBac = erPuertoLR.Properties.Value.ToString();

            key = key.OpenSubKey(@"Impresoras\Ticket", true);


            if (key == null)
            {
                key = Registry.CurrentUser.OpenSubKey(@"Software\Logistika\POS Retail 8.0.0", true);
                key.CreateSubKey("Impresoras");
                key = key.OpenSubKey("Impresoras", true);
                key.CreateSubKey("Ticket");
                key = key.OpenSubKey("Ticket", true);
            }

            key.SetValue("Factura", Utils.Helpers.ObjToStr(rowFactTkt.Properties.Value) );
            key.SetValue("Nota de Credito", Utils.Helpers.ObjToStr(rowNCTkt.Properties.Value) );
            key.SetValue("Vale", Utils.Helpers.ObjToStr(rowValeTkt.Properties.Value) );

            key = Registry.CurrentUser.OpenSubKey(@"Software\Logistika\POS Retail 8.0.0\Impresoras\Preimpresos", true);

            if (key == null)
            {
                key = Registry.CurrentUser.OpenSubKey(@"Software\Logistika\POS Retail 8.0.0\Impresoras", true);
                key.CreateSubKey("Preimpresos");
                key = key.OpenSubKey("Preimpresos");
            }

            key.SetValue("Factura", Utils.Helpers.ObjToStr(rowFactPre.Properties.Value));
            key.SetValue("Nota de Credito", Utils.Helpers.ObjToStr(rowNCPre.Properties.Value));
            key.SetValue("Vale", Utils.Helpers.ObjToStr(rowValePre.Properties.Value));
            key.SetValue("Pagare", Utils.Helpers.ObjToStr(rowPagarePre.Properties.Value));
            key.SetValue("Garantia", Utils.Helpers.ObjToStr(rowGarantiaPre.Properties.Value));
            key.SetValue("Certificado", Utils.Helpers.ObjToStr(rowCertificadoPre.Properties.Value));
            key.SetValue("Arqueos", Utils.Helpers.ObjToStr(rowImpresora_Arqueos.Properties.Value));

        }

        private void btnReportes_Click(object sender, EventArgs e)
        {
            Reportes.frmReportes vdrSls = (Reportes.frmReportes)GUI.CApp.CreateForm(typeof(Reportes.frmReportes), true);                        
        }

        private void btnDiccionario_Click(object sender, EventArgs e)
        {
            GUI.CApp.idioma = rowIdioma.Properties.Value.ToString();
            Forms.frmIdiomas vdrSls = (Forms.frmIdiomas)GUI.CApp.CreateForm(typeof(Forms.frmIdiomas), true);

            RegistryKey key = Registry.CurrentUser;
            key = key.OpenSubKey(@"Software\Logistika\POS Retail 8.0.0", true);
            key.SetValue("Idioma", rowIdioma.Properties.Value);         
        }

        private void panelControl2_Paint(object sender, PaintEventArgs e)
        {

        }

        private void vGridControl1_Click(object sender, EventArgs e)
        {

        }

        private void btnGarantias_Click(object sender, EventArgs e)
        {
            Forms.frmConfigWarranty fcw = new frmConfigWarranty();
            fcw.ShowDialog();
            fcw.Dispose();

        }
    }
}