// (C) Copyright 2010 by GEOGRAMA 
//
using System;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;

using GMLC01HerramientasCad.Logica;
using COMUN = GMLC01HerramientasCad.Logica;

using System.Windows.Forms;

// This line is not mandatory, but improves loading performances
[assembly: CommandClass(typeof(GMLC01HerramientasCad.Acad.MyCommands))]

namespace GMLC01HerramientasCad.Acad
{

    // This class is instantiated by AutoCAD for each document when
    // a command is called by the user the first time in the context
    // of a given document. In other words, non static data in this class
    // is implicitly per-document!
    public static class MyCommands
    {

        internal static SeleccionElementoCtl selElemCtl;

        // The CommandMethod attribute can be applied to any public  member 
        // function of any public class.
        // The function should take no arguments and return nothing.
        // If the method is an intance member then the enclosing class is 
        // intantiated for each document. If the member is a static member then
        // the enclosing class is NOT intantiated.
        //
        // NOTE: CommandMethod has overloads where you can provide helpid and
        // context menu.

        [CommandMethod("GMLC01HerramientasCAD", "IDIBUJAR", CommandFlags.Modal | CommandFlags.Redraw)]
        public static void Dibujar()
        {
            try
            {
                selElemCtl.Continua();
            }
            catch (System.Exception ex)
            {
                Log.RegisterError(ex);
                throw new COMUN.ExcepcionGMLC("", ex);
            }
        }

        ///// <summary>
        ///// Punto de entrada para el comando DIBUJAR
        ///// </summary>
        //[CommandMethod("GMLC01HerramientasCAD", "IDIBUJAR", CommandFlags.Modal | CommandFlags.Redraw)]
        //public static void Dibujar()
        //{
        //    try
        //    {
        //        IntDibujar();
        //    }
        //    catch (System.Exception ex)
        //    {
        //        Log.RegisterError(ex);
        //        throw new COMUN.ExcepcionGMLC("", ex);
        //    }
        //}
        //private static void IntDibujar()
        //{
        //    System.Globalization.CultureInfo mainCult = new System.Globalization.CultureInfo(System.Threading.Thread.CurrentThread.CurrentCulture.Name);
        //    if (!String.IsNullOrEmpty(MyPlugin.cultureName))
        //    {
        //        System.Threading.Thread.CurrentThread.CurrentCulture = System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(MyPlugin.cultureName);
        //    }
        //    // Si no se cargó el XSD, no se hará nada.
        //    if (ModComun.XSD == null)
        //    {
        //        System.Windows.Forms.MessageBox.Show(COMUN.Properties.Resources.ElFicheroXSDNoHaSidoCargado, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, 0);
        //        return;
        //    }

        //    try
        //    {
        //        if (MyPlugin.dibujarElemento == null)
        //            MyPlugin.dibujarElemento = new DibujarEnts();

        //        ((DibujarEnts)MyPlugin.dibujarElemento).Dibujar();
        //    }
        //    /*catch (Exception e)
        //    {
        //        MessageBox.Show(e.Message, e.Source);
        //    }*/
        //    finally
        //    {
        //    }
        //    System.Threading.Thread.CurrentThread.CurrentCulture = System.Threading.Thread.CurrentThread.CurrentUICulture = mainCult;
        //}

        [CommandMethod("GMLC01HerramientasCAD", "IPALETA", CommandFlags.Modal | CommandFlags.Redraw)]
        public static void MuestraPaleta()
        {
            try
            {
                GMLCPaleteSet.LoadPaleteSet();
            }
            catch (System.Exception ex)
            {
                Log.RegisterError(ex);
                throw new COMUN.ExcepcionGMLC("", ex);
            }
        }

        /// <summary>
        /// Punto de entrada para el comando INVENTARIO CONSULTAR
        /// </summary>
        [CommandMethod("GMLC01HerramientasCAD", "ICONSULTAR", CommandFlags.Modal | CommandFlags.Redraw)]
        public static void Consultar()
        {
            try
            {
                IntConsultar();
            }
            catch (System.Exception ex)
            {
                Log.RegisterError(ex);
                throw new COMUN.ExcepcionGMLC("", ex);
            }
        }
        private static void IntConsultar()
        {
            System.Globalization.CultureInfo mainCult = new System.Globalization.CultureInfo(System.Threading.Thread.CurrentThread.CurrentCulture.Name);
            if (!String.IsNullOrEmpty(MyPlugin.cultureName))
            {
                System.Threading.Thread.CurrentThread.CurrentCulture = System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(MyPlugin.cultureName);
            }
            // Si no se cargó el XSD, no se hará nada.
            if (ModComun.XSD == null)
            {
                System.Windows.Forms.MessageBox.Show(COMUN.Properties.Resources.ElFicheroXSDNoHaSidoCargado, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, 0);
                return;
            }

            ConsultarElementos consultar = new ConsultarElementos();
            consultar.Init();
            System.Threading.Thread.CurrentThread.CurrentCulture = System.Threading.Thread.CurrentThread.CurrentUICulture = mainCult;
        }

        /// <summary>
        /// Punto de entrada para el comando INVENTARIO TEMATICO
        /// </summary>
        [CommandMethod("GMLC01HerramientasCAD", "ITEMATICO", CommandFlags.Modal | CommandFlags.Redraw)]
        public static void Tematico()
        {
            try
            {
                IntTematico();
            }
            catch (System.Exception ex)
            {
                Log.RegisterError(ex);
                throw new COMUN.ExcepcionGMLC("", ex);
            }
        }
        private static void IntTematico()
        {
            System.Globalization.CultureInfo mainCult = new System.Globalization.CultureInfo(System.Threading.Thread.CurrentThread.CurrentCulture.Name);

            if (!String.IsNullOrEmpty(MyPlugin.cultureName))
            {
                System.Threading.Thread.CurrentThread.CurrentCulture = System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(MyPlugin.cultureName);
            }
            // Si no se cargó el XSD, no se hará nada.
            if (ModComun.XSD == null)
            {
                System.Windows.Forms.MessageBox.Show(COMUN.Properties.Resources.ElFicheroXSDNoHaSidoCargado, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, 0);
                return;
            }

            try
            {
                if (MyPlugin.tematicos == null)
                    MyPlugin.tematicos = new Tematicos();

                MyPlugin.tematicos.ActivaTematicos();
            }
            /*catch (ApplicationException e)
            {
                System.Windows.Forms.MessageBox.Show(e.Message, e.Source);
            }
            catch (Exception e)
            {
                System.Windows.Forms.MessageBox.Show(e.Message, e.Source);
            }*/
            finally
            {
            }
            System.Threading.Thread.CurrentThread.CurrentCulture = System.Threading.Thread.CurrentThread.CurrentUICulture = mainCult;
        }

        /// <summary>
        /// Punto de entrada para el comando INVENTARIO BUSCAR
        /// </summary>
        [CommandMethod("GMLC01HerramientasCAD", "IBUSCAR", CommandFlags.Modal | CommandFlags.Redraw)]
        public static void Buscar()
        {
            try
            {
                IntBusca();
            }
            catch (System.Exception ex)
            {
                Log.RegisterError(ex);
                throw new COMUN.ExcepcionGMLC("", ex);
            }
        }
        private static void IntBusca()
        {
            System.Globalization.CultureInfo mainCult = new System.Globalization.CultureInfo(System.Threading.Thread.CurrentThread.CurrentCulture.Name);
            if (!String.IsNullOrEmpty(MyPlugin.cultureName))
            {
                System.Threading.Thread.CurrentThread.CurrentCulture = System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(MyPlugin.cultureName);
            }
            // Si no se cargó el XSD, no se hará nada.
            if (ModComun.XSD == null)
            {
                System.Windows.Forms.MessageBox.Show(COMUN.Properties.Resources.ElFicheroXSDNoHaSidoCargado, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, 0);
                return;
            }

            //BuscarElementos buscar = new BuscarElementos();
            try
            {
                if (MyPlugin.buscarElementos == null)
                    MyPlugin.buscarElementos = new BuscarElementos();

                MyPlugin.buscarElementos.ActivaBuscarElementos();
            }
            /*catch (ApplicationException e)
            {
                System.Windows.Forms.MessageBox.Show(e.Message, e.Source);
            }
            catch (Exception e)
            {
                System.Windows.Forms.MessageBox.Show(e.Message, e.Source);
            }*/
            finally
            {
            }
            System.Threading.Thread.CurrentThread.CurrentCulture = System.Threading.Thread.CurrentThread.CurrentUICulture = mainCult;
        }

        /// <summary>
        /// Punto de entrada para el comando INVENTARIO ETIQUETAR
        /// </summary>
        [CommandMethod("GMLC01HerramientasCAD", "IETIQUETAR", CommandFlags.Modal | CommandFlags.Redraw)]
        public static void Etiquetar()
        {
            try
            {
                IntEtiquetar();
            }
            catch (System.Exception ex)
            {
                Log.RegisterError(ex);
                throw new COMUN.ExcepcionGMLC("", ex);
            }
        }
        private static void IntEtiquetar()
        {
            System.Globalization.CultureInfo mainCult = new System.Globalization.CultureInfo(System.Threading.Thread.CurrentThread.CurrentCulture.Name);
            if (!String.IsNullOrEmpty(MyPlugin.cultureName))
            {
                System.Threading.Thread.CurrentThread.CurrentCulture = System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(MyPlugin.cultureName);
            }
            // Si no se cargó el XSD, no se hará nada.
            if (ModComun.XSD == null)
            {
                System.Windows.Forms.MessageBox.Show(COMUN.Properties.Resources.ElFicheroXSDNoHaSidoCargado, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, 0);
                return;
            }

            EtiquetarElementos etiquetar = new EtiquetarElementos();
            etiquetar.LanzaEtiquetarElementos();

            System.Threading.Thread.CurrentThread.CurrentCulture = System.Threading.Thread.CurrentThread.CurrentUICulture = mainCult;
        }

        /// <summary>
        /// Punto de entrada para el comando INVENTARIO IMPORTAR GML
        /// </summary>
        [CommandMethod("GMLC01HerramientasCAD", "IIMPORTARGML", CommandFlags.Modal)]
        public static void Importar()
        {
            try
            {
                IntImportar();
            }
            catch (System.Exception ex)
            {
                Log.RegisterError(ex);
                throw new COMUN.ExcepcionGMLC("", ex);
            }
        }
        private static void IntImportar()
        {
            System.Globalization.CultureInfo mainCult = new System.Globalization.CultureInfo(System.Threading.Thread.CurrentThread.CurrentCulture.Name);
            if (!String.IsNullOrEmpty(MyPlugin.cultureName))
            {
                System.Threading.Thread.CurrentThread.CurrentCulture = System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(MyPlugin.cultureName);
            }
            // Si no se cargó el XSD, no se hará nada.
            if (ModComun.XSD == null)
            {
                System.Windows.Forms.MessageBox.Show(COMUN.Properties.Resources.ElFicheroXSDNoHaSidoCargado, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, 0);
                return;
            }

            try
            {
                new GML().Importar();
            }
            catch (ApplicationException e)
            {
                System.Windows.Forms.MessageBox.Show(e.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, 0);
            }
            System.Threading.Thread.CurrentThread.CurrentCulture = System.Threading.Thread.CurrentThread.CurrentUICulture = mainCult;
        }

        /// <summary>
        /// Punto de entrada para el comando INVENTARIO EXPORTAR GML
        /// </summary>
        [CommandMethod("GMLC01HerramientasCAD", "IEXPORTARGML", CommandFlags.Modal)]
        public static void Exportar()
        {
            try
            {
                IntExportar();
            }
            catch (System.Exception ex)
            {
                Log.RegisterError(ex);
                throw new COMUN.ExcepcionGMLC("", ex);
            }
        }
        private static void IntExportar()
        {
            System.Globalization.CultureInfo mainCult = new System.Globalization.CultureInfo(System.Threading.Thread.CurrentThread.CurrentCulture.Name);
            if (!String.IsNullOrEmpty(MyPlugin.cultureName))
            {
                System.Threading.Thread.CurrentThread.CurrentCulture = System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(MyPlugin.cultureName);
            }
            // Si no se cargó el XSD, no se hará nada.
            if (ModComun.XSD == null)
            {
                System.Windows.Forms.MessageBox.Show(COMUN.Properties.Resources.ElFicheroXSDNoHaSidoCargado, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, 0);
                return;
            }

            try
            {
                new GML().Exportar();
            }
            catch (ApplicationException e)
            {
                System.Windows.Forms.MessageBox.Show(e.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, 0);
            }
            System.Threading.Thread.CurrentThread.CurrentCulture = System.Threading.Thread.CurrentThread.CurrentUICulture = mainCult;
        }

        /// <summary>
        /// Punto de entrada para el comando INVENTARIO IDIOMA
        /// </summary>
        [CommandMethod("GMLC01HerramientasCAD", "IIDIOMA", CommandFlags.Modal)]
        public static void Idioma()
        {
            try
            {
                IntIdioma();
            }
            catch (System.Exception ex)
            {
                Log.RegisterError(ex);
                throw new COMUN.ExcepcionGMLC("", ex);
            }
        }
        private static void IntIdioma()
        {
            string mainCult = System.Threading.Thread.CurrentThread.CurrentCulture.Name;
            if (!String.IsNullOrEmpty(MyPlugin.cultureName))
            {
                System.Threading.Thread.CurrentThread.CurrentCulture = System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(MyPlugin.cultureName);
            }
            // Si no se cargó el XSD, no se hará nada.
            if (ModComun.XSD == null)
            {
                System.Windows.Forms.MessageBox.Show(COMUN.Properties.Resources.ElFicheroXSDNoHaSidoCargado, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, 0);
                return;
            }

            ModComun.ActivarIdioma();

            if (MyPlugin.cultureName != System.Threading.Thread.CurrentThread.CurrentCulture.Name)
            {
                if (MyPlugin.dibujarElemento != null) MyPlugin.dibujarElemento.Dispose();
                MyPlugin.dibujarElemento = null;
                if (MyPlugin.buscarElementos != null) MyPlugin.buscarElementos.Dispose();
                MyPlugin.buscarElementos = null;
                if (MyPlugin.tematicos != null) MyPlugin.tematicos.Dispose();
                MyPlugin.tematicos = null;

                MyPlugin.cultureName = System.Threading.Thread.CurrentThread.CurrentCulture.Name;

                System.Threading.Thread.CurrentThread.CurrentCulture = System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(mainCult);
                Cui.CuiRebuild();

                GMLCPaleteSet.ActualizarIdiomaInterfaz();
            }
        }

        /// <summary>
        /// Punto de entrada para el comando INVENTARIO IDIOMA
        /// </summary>
        [CommandMethod("GMLC01HerramientasCAD", "IACERCADE", CommandFlags.Modal)]
        public static void AcercaWindow()
        {
            try
            {
                IntAcercaWindow();
            }
            catch (System.Exception ex)
            {
                Log.RegisterError(ex);
                throw new COMUN.ExcepcionGMLC("", ex);
            }
        }
        private static void IntAcercaWindow()
        {
            System.Globalization.CultureInfo mainCult = new System.Globalization.CultureInfo(System.Threading.Thread.CurrentThread.CurrentCulture.Name);
            if (!String.IsNullOrEmpty(MyPlugin.cultureName))
            {
                System.Threading.Thread.CurrentThread.CurrentCulture = System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(MyPlugin.cultureName);
            }
            // Si no se cargó el XSD, no se hará nada.
            if (ModComun.XSD == null)
            {
                System.Windows.Forms.MessageBox.Show(COMUN.Properties.Resources.ElFicheroXSDNoHaSidoCargado, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, 0);
                return;
            }

            AboutBox abobo = new AboutBox();
            abobo.ShowDialog();
            abobo.Dispose();

            System.Threading.Thread.CurrentThread.CurrentCulture = System.Threading.Thread.CurrentThread.CurrentUICulture = mainCult;
        }
    }
}
