using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Design;
using System.Windows.Forms.Design;

namespace OliviaAddIn
{
    public partial class EditTxtPlant : Form
    {
        //////////////////////////////////////////////////////////////
        //variables para los botones
        int top_buttons;
        int bottom_props;
        int right_props;
        public EditTxtPlantProps props;
        public EditTxtPlant()
        {
            props = new EditTxtPlantProps();
            InitializeComponent();

            top_buttons = ClientSize.Height - button1.Top;
            bottom_props = ClientSize.Height - propertyGrid_txtplant.Bottom;
            right_props = ClientSize.Width - propertyGrid_txtplant.Right;

            // Assign the form to the propertygrid
            propertyGrid_txtplant.SetParent(this);
            propertyGrid_txtplant.SelectedObject = props;           

        }

        private void button1_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.OK;
            Close();
        }

        private void EditTxtPlant_Resize(object sender, EventArgs e)
        {
            //mover button_aceptar, button_reset, button_guardar, bottom del property
            button1.Top = ClientSize.Height - top_buttons;
            propertyGrid_txtplant.Size = new Size(ClientSize.Width - right_props, ClientSize.Height - bottom_props);
        }        
    }

    /*
     * Clase para los campos multilínea
     */
    public class MultiLineTextEditor : UITypeEditor
    {
        private IWindowsFormsEditorService _editorService;

        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.DropDown;
        }

        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            _editorService = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));

            TextBox textEditorBox = new TextBox();
            textEditorBox.Multiline = true;
            textEditorBox.ScrollBars = ScrollBars.Vertical;
            textEditorBox.Width = 250;
            textEditorBox.Height = 150;
            textEditorBox.BorderStyle = BorderStyle.None;
            textEditorBox.AcceptsReturn = true;

            textEditorBox.Text = value as string;

            _editorService.DropDownControl(textEditorBox);

            return textEditorBox.Text;
        }
    }

    /**
        * Clase para la estructura de propiedades
        */
    public class EditTxtPlantProps
    {
        public const int N_CAMPOS_PLANT = 7;
        public String[] campos_plant = new String[N_CAMPOS_PLANT];
        public Font[] fonts = new Font[N_CAMPOS_PLANT];

        public EditTxtPlantProps()
        {
            int[] ss = {10,14,12,8,8,8,7};

            for (int i = 0; i < N_CAMPOS_PLANT; i++)
            {
                campos_plant[i]="";
                fonts[i] = new Font("Arial Narrow", ss[i], FontStyle.Regular);
            }
        }

        [CategoryAttribute("01 Proyecto")]
        [DisplayNameAttribute("Texto del Proyecto")]
        [DescriptionAttribute("Título del proyecto")]
        [Editor(typeof(MultiLineTextEditor), typeof(UITypeEditor))]
        public string titulo
        {
            get { return campos_plant[0]; }
            set { campos_plant[0] = value; }
        }
        [CategoryAttribute("01 Proyecto")]
        [DisplayNameAttribute("Fuente del Proyecto")]
        [DescriptionAttribute("Fuente para el Título del proyecto")]
        public Font titulo_font
        {
            get { return fonts[0]; }
            set { fonts[0] = value; }
        }
        /// <summary>
        /// ///////////////////////////////////
        /// </summary>
        [CategoryAttribute("02 Título")]
        [DisplayNameAttribute("Texto del título")]
        [DescriptionAttribute("Título del servicio")]
        [Editor(typeof(MultiLineTextEditor), typeof(UITypeEditor))]
        public string titulo_serv
        {
            get { return campos_plant[1]; }
            set { campos_plant[1] = value; }
        }
        [CategoryAttribute("02 Título")]
        [DisplayNameAttribute("Fuente del título")]
        [DescriptionAttribute("Fuente para el Título del servicio")]
        public Font titulo_serv_font
        {
            get { return fonts[1]; }
            set { fonts[1] = value; }
        }
        /// <summary>
        /// ///////////////////////////////////
        /// </summary>
        [CategoryAttribute("03 Tipología")]
        [DisplayNameAttribute("Texto de la Tipología")]
        [DescriptionAttribute("Tipología del servicio: Recogida de residuos, Limpieza viaria, Mantenimiento zonas verdes, etc.")]
        [Editor(typeof(MultiLineTextEditor), typeof(UITypeEditor))]
        public string tipo_serv
        {
            get { return campos_plant[2]; }
            set { campos_plant[2] = value; }
        }
        [CategoryAttribute("03 Tipología")]
        [DisplayNameAttribute("Fuente de la Tipología")]
        [DescriptionAttribute("Fuente para la Tipología del servicio")]
        public Font tipo_serv_font
        {
            get { return fonts[2]; }
            set { fonts[2] = value; }
        }
        /// <summary>
        /// ///////////////////////////////////
        /// </summary>       
        [CategoryAttribute("04 Frecuencia")] 
        [DisplayNameAttribute("Texto de la frecuencia")]
        [DescriptionAttribute("Frecuencia del servicio")]
        [Editor(typeof(MultiLineTextEditor), typeof(UITypeEditor))]
        public string frec_serv
        {
            get { return campos_plant[3]; }
            set { campos_plant[3] = value; }
        }
        [CategoryAttribute("04 Frecuencia")]
        [DisplayNameAttribute("Fuente de la frecuencia")]
        [DescriptionAttribute("Fuente para la Frecuencia del servicio")]
        public Font frec_serv_font
        {
            get { return fonts[3]; }
            set { fonts[3] = value; }
        }
        /// <summary>
        /// ///////////////////////////////////
        /// </summary>  
        [CategoryAttribute("05 Turno")]      
        [DisplayNameAttribute("Texto del Turno")]
        [DescriptionAttribute("Turno del servicio")]
        [Editor(typeof(MultiLineTextEditor), typeof(UITypeEditor))]
        public string turno_serv
        {
            get { return campos_plant[4]; }
            set { campos_plant[4] = value; }
        }
        [CategoryAttribute("05 Turno")]
        [DisplayNameAttribute("Fuente del Turno")]
        [DescriptionAttribute("Fuente para el Turno del servicio")]
        public Font turno_serv_font
        {
            get { return fonts[4]; }
            set { fonts[4] = value; }
        }
        /// <summary>
        /// ///////////////////////////////////
        /// </summary>
        [CategoryAttribute("06 Equipo")]
        [DisplayNameAttribute("Texto del Equipo")]
        [DescriptionAttribute("Equipo del servicio: Conductor, Peón Conductor, Peón, Vehículo")]
        [Editor(typeof(MultiLineTextEditor), typeof(UITypeEditor))]
        public string equipo_serv
        {
            get { return campos_plant[5]; }
            set { campos_plant[5] = value; }
        }
        [CategoryAttribute("06 Equipo")]
        [DisplayNameAttribute("Fuente del Equipo")]
        [DescriptionAttribute("Fuente para el Equipo del servicio")]
        public Font equipo_serv_font
        {
            get { return fonts[5]; }
            set { fonts[5] = value; }
        }
        /// <summary>
        /// ///////////////////////////////////
        /// </summary>
        [CategoryAttribute("07 Información adicional")]
        [DisplayNameAttribute("Texto para Información adicional")]
        [DescriptionAttribute("Otra información para el proyecto")]
        [Editor(typeof(MultiLineTextEditor), typeof(UITypeEditor))]
        public string info_adi
        {
            get { return campos_plant[6]; }
            set { campos_plant[6] = value; }
        }
        [CategoryAttribute("07 Información adicional")]
        [DisplayNameAttribute("Fuente para Información adicional")]
        [DescriptionAttribute("Fuente para la Información adicional")]
        public Font info_adi_font
        {
            get { return fonts[6]; }
            set { fonts[6] = value; }
        }
        /// <summary>
        /// ///////////////////////////////////
        /// </summary>
    }


    /**
        * Para usar el Tabulador para cambiar de propiedad
        */
    public class TabbedPropertyGrid : PropertyGrid
    {
        Form parent;
        public TabbedPropertyGrid() : base() { }
        public void SetParent(Form form)
        {
            parent = form;
            // Catch null arguments
            if (form == null)
            {
                throw new ArgumentNullException("form");
            }

            // Set this property to intercept all events
            form.KeyPreview = true;

            // Listen for keydown event
            form.KeyDown += new KeyEventHandler(this.Form_KeyDown);

        }
        private void Form_KeyDown(object sender, KeyEventArgs e)
        {
            // Exit if cursor not in control
            if (!this.RectangleToScreen(this.ClientRectangle).Contains(Cursor.Position))
            {
                return;
            }

            // Handle tab key
            if (e.KeyCode != Keys.Tab) { return; }
            e.Handled = true;
            e.SuppressKeyPress = true;

            // Get selected griditem
            GridItem gridItem = this.SelectedGridItem;
            if (gridItem == null) { return; }

            // Create a collection all visible child griditems in propertygrid
            GridItem root = gridItem;
            while (root.GridItemType != GridItemType.Root)
            {
                root = root.Parent;
            }
            List<GridItem> gridItems = new List<GridItem>();
            this.FindItems(root, gridItems);

            // Get position of selected griditem in collection
            int index = gridItems.IndexOf(gridItem);

            int nextIndex = index + 1;
            if (nextIndex >= gridItems.Count) 
            { 
                //ha terminado, pasa al siguiente
                parent.GetNextControl(this, true).Focus();
                return; 
            }
            // Select next griditem in collection
            this.SelectedGridItem = gridItems[nextIndex];
        }
        private void FindItems(GridItem item, List<GridItem> gridItems)
        {
            switch (item.GridItemType)
            {
                case GridItemType.Root:
                case GridItemType.Category:
                    foreach (GridItem i in item.GridItems)
                    {
                        this.FindItems(i, gridItems);
                    }
                    break;
                case GridItemType.Property:
                    gridItems.Add(item);
                    if (item.Expanded)
                    {
                        foreach (GridItem i in item.GridItems)
                        {
                            this.FindItems(i, gridItems);
                        }
                    }
                    break;
                case GridItemType.ArrayValue:
                    break;
            }
        }
    }
        
    

    

    
}
