using System;
using System.Data;
using LLNet.Controls;
using Microsoft.Win32;
using System.Threading;
using System.Collections;
using System.Reflection;
using System.Collections.Generic;
using System.Windows.Forms;

namespace LLNet.Tienda
{
    /// <summary>
    /// Program: Clase principal de la aplicación 
    /// Developer: Wildmar Lopez, CERTIFICADA 28/11/2006
    /// </summary>
    public static class Program
    {
        /// <summary>
        /// Formulario principal del ingreso de pedidos
        /// </summary>
        public static MainForms.frmMainPOS FrmMainPos;
        public static MainForms.frmLogin FrmLogin;

        public static string MAFD;
        public static string FOVC;
        public static string FOEI;

        public static string regexDefault;
        public static string regexNit;
        public static string regexDireccion;
        public static string regexTelefono;
        public static string regexEmail;
        public static string regexTarjeta;
        public static string regexCheque;
        public static string opcion;
        public static string exonerado;
        public static DateTime fecha;
        public static DataSet prueba;


        private static decimal? caja;
        public static decimal? Caja
        {
            get
            {
                if (caja == null)
                {
                    RegistryKey key = Registry.CurrentUser;
                    key = key.OpenSubKey(@"Software\Logistika\POS Retail 8.0.0", true);
                    string s = Utils.Helpers.ObjToStr(key.GetValue("Caja", 1));
                    if (s != string.Empty)
                        caja = Convert.ToDecimal(s);
                }
                return caja;
            }
        }

        public static void ClearVerticalGrid(DevExpress.XtraVerticalGrid.VGridControl vg)
        {
            foreach (DevExpress.XtraVerticalGrid.Rows.BaseRow br in vg.Rows)
            {
                if (br.GetType() == typeof(DevExpress.XtraVerticalGrid.Rows.EditorRow))
                {
                    br.Properties.Value = null;
                }
                else if (br.GetType() == typeof(DevExpress.XtraVerticalGrid.Rows.MultiEditorRow))
                {
                    DevExpress.XtraVerticalGrid.Rows.MultiEditorRow mer = br as DevExpress.XtraVerticalGrid.Rows.MultiEditorRow;
                    for (int i = 0; i < mer.PropertiesCollection.Count; i++)
                    {
                        mer.PropertiesCollection[i].Value = null;
                    }
                }
            }
        }

        /// <summary>
        /// Contiene la jornada actual
        /// </summary>
        private static decimal? jornada;

        /// <summary>
        /// Devuelve la jornada actual en base al usuario y caja asignados previamente.
        /// </summary>
        public static Decimal? Jornada
        {
            get
            {
                if (GUI.CApp.GetAccess(GUI.CApp.ModulePrefix + "Payments") && Program.Caja != null)
                {
                    Utils.QueryParams qp = new LLNet.Utils.QueryParams();
                    qp.AddReplaceParam("USUARIO", GUI.CApp.usuario);
                    qp.AddReplaceParam("CAJA", Caja.ToString());
                    // parametro para seleccionar la jornada tomando en cuenta la caja
                    string caja = Utils.Helpers.ObjToStr(GUI.CApp.GetParam("VJSC"));
                    object objJornada;
                    if (caja == "S" || caja == "s")
                    {
                        objJornada = GUI.CApp.bll.GetSingleValue("GetOpenPosSCaja", qp, "PDV");
                    }
                    else
                    {
                        objJornada = GUI.CApp.bll.GetSingleValue("GetOpenPos", qp, "PDV");
                    }                                       
                    if (objJornada != null)
                        jornada = Convert.ToDecimal(objJornada);
                    else
                        jornada = null;
                }
                else
                    jornada = null;

                return jornada;
            }
        }

        public static void CerrarJornada()
        {
            jornada = null;
        }

        #region The main entry point for the application.

        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            GUI.CustomExceptionHandler eh = new GUI.CustomExceptionHandler();

            // Adds the event handler to to the event.
            Application.ThreadException += new ThreadExceptionEventHandler(eh.OnThreadException);

            GUI.CApp.bll = new LLNet.BLL.BLLServer();

            RegistryKey key = Registry.CurrentUser;
            key = key.OpenSubKey(@"Software\Logistika\POS Retail 8.0.0", true);
            GUI.CApp.ExeDir = key.GetValue("Path", Application.StartupPath + "\\").ToString();
            GUI.CApp.ExeDir = (GUI.CApp.ExeDir == "") ? Application.StartupPath + "\\" : GUI.CApp.ExeDir;
            GUI.CApp.EventLog = new LLEventLog(GUI.CApp.ExeDir);
            GUI.CApp.SolutionModule = "LLNET_TIENDA";
            GUI.CApp.CreateModules = true;
            GUI.CApp.ModulePrefix = "TDA_";

            FrmLogin = new MainForms.frmLogin();
            GUI.CApp.InitSkinsDesigners();
            Application.Run(FrmLogin);
        }

        #endregion

        #region Private and internal methods

        /// <summary>
        /// Edita el documento de acuerdo al documento y tipo enviado
        /// </summary>
        /// <param name="pDocument"></param>
        /// <param name="pType"></param>
        internal static void GetOrder(decimal pDocument, string pType)
        {
            object[] obj = new object[] { pDocument, pType };

            GUI.CApp.MainForm = null;
            GUI.CApp.MainForm = FrmLogin.htDesktops["Facturacion"] as Form;

            if (GUI.CApp.MainForm == null)
            {
                MainForms.frmMainPOS frm = new MainForms.frmMainPOS();
                frm.Init(obj);
                frm.Show();

                FrmLogin.htDesktops.Add("Facturacion", frm);
                GUI.CApp.MainForm = frm;
            }
            else
            {
                ((MainForms.frmMainPOS)GUI.CApp.MainForm).GetOrder(obj);
                GUI.CApp.MainForm.Show();
            }
        }

        public static void CloseWindow(Form wdw)
        {
            wdw.Hide();
            Program.FrmLogin.ShowInTaskbar = true;
            Program.FrmLogin.Show();
            
        }

        #endregion
    }
}