using System;
using System.Collections.Generic;
using System.Text;

using COMUN = GMLC01HerramientasCad.Logica;

namespace GMLC01HerramientasCad.Acad
{
    class GMLCPaleteSet
    {
        internal static Autodesk.AutoCAD.Windows.PaletteSet myPaletteSet;
        internal static SeleccionElementoCtl selElemCtl;
        internal static ConsultaContainerCtl consCtl;

        internal static Autodesk.AutoCAD.Windows.Palette palSelElem;
        internal static Autodesk.AutoCAD.Windows.Palette palCons;

        private GMLCPaleteSet()
        {
        }

        internal static void LoadPaleteSet()
        {
            //check to see if paletteset is already created
            if (myPaletteSet == null)
            {
                //no so create it

                if (selElemCtl == null)
                {
                    //create new instance of user control
                    selElemCtl = new SeleccionElementoCtl();
                    if (COMUN.ModComun.XSD != null) selElemCtl.Initialize(COMUN.ModComun.XSD, true);
                }

                if (consCtl == null)
                {
                    //create new instance of user control
                    consCtl = new ConsultaContainerCtl();
                }
                //El parámetro "IPALETA" es el comando que ejecutará AutoCAD al iniciarse para restaurar la paleta 
                //(en algunas versiones no es necesario este comando).
                myPaletteSet = new Autodesk.AutoCAD.Windows.PaletteSet("IPALETA", new Guid("20101213-a945-b131-c372-000000000007"));
                
                //myPaletteSet.Size = new System.Drawing.Size(consCtl.ContentMinHeight.Width + 6, consCtl.ContentMinHeight.Height + 7);
                //myPaletteSet.MinimumSize = new System.Drawing.Size(consCtl.ContentMinHeight.Width + 6, consCtl.ContentMinHeight.Height + 7);
                myPaletteSet.KeepFocus = true;
                myPaletteSet.Name = "GMLC";
                myPaletteSet.PaletteActivated += new Autodesk.AutoCAD.Windows.PaletteActivatedEventHandler(myPaletteSet_PaletteActivated);
                
                //add it to the paletteset
                palSelElem = myPaletteSet.Add("SeleccionElemento", selElemCtl);
                palSelElem.Name = COMUN.Properties.Resources.SeleccionarEntidad;
                

                //add it to the paletteset
                palCons = myPaletteSet.Add("ConsultaElemento", consCtl);
                palCons.Name = COMUN.Properties.Resources.ConsultarEditar;

                //Al ocultarse la paleta, se desactiva el checkbox.
                myPaletteSet.StateChanged += new Autodesk.AutoCAD.Windows.PaletteSetStateEventHandler(MyPaletteSetStateChanged);

            }
            ActualizarIdiomaInterfaz();
            //turn it on
            myPaletteSet.Visible = true;
        }

        static void myPaletteSet_PaletteActivated(object sender, Autodesk.AutoCAD.Windows.PaletteActivatedEventArgs e)
        {
            if (palCons != null) GMLCPaleteSet.myPaletteSet.KeepFocus = e.Activated.Name.Equals(palCons.Name);
        }

        static void MyPaletteSetStateChanged(object sender, Autodesk.AutoCAD.Windows.PaletteSetStateEventArgs e)
        {
            //Al ocultarse la paleta, se desactiva el checkbox.
            if (e.NewState == Autodesk.AutoCAD.Windows.StateEventIndex.Hide)
            {
                consCtl.cbEnable.Checked = false;
            }
        }

        internal static void ActualizarIdiomaInterfaz()
        {
            palSelElem.Name = COMUN.Properties.Resources.SeleccionarEntidad;
            palCons.Name = COMUN.Properties.Resources.ConsultarEditar;
            selElemCtl.ActualizarIdiomaInterfaz();
            consCtl.ActualizarIdiomaInterfaz();
        }

    }
}
