using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Exferia_Aplicacion.General;
using Exferia_Aplicacion.Visualizacion;

namespace Exferia_Controles
{
    public partial class Exferia_RichTextBox : Panel
    {
        private RichTextBox_Principal g_obj_RichTextBox_Principal;

        #region Propiedades Publicas
        public void Prueba()
        { }

        public HorizontalAlignment Exferia_RichTextBox_Aligment
        {    
            set
            {
                g_obj_RichTextBox_Principal.SelectionAlignment = value;
            }
            get
            {
                return g_obj_RichTextBox_Principal.SelectionAlignment;
            }
        }

        public void Exferia_RichTextBox_Copiar()
        {
            g_obj_RichTextBox_Principal.Copy();
        }
        public void Exferia_RichTextBox_Cortar()
        {
            g_obj_RichTextBox_Principal.Cut();
        }
        public void Exferia_RichTextBox_Pegar(DataFormats.Format _valor)
        {    
             g_obj_RichTextBox_Principal.Paste(_valor);
        }

        internal bool g_bol_Obligatorio = false;
        public bool Exferia_RichTextBox_Obligatorio
        {
            set
            {
                g_bol_Obligatorio = value;
                if (g_bol_Obligatorio)
                {

                    Exferia_RichTextBox_Fondo = Colores.G_COLOR_FONDO_OBLIGATORIO;
                }
                else
                {
                    Exferia_RichTextBox_Fondo = Colores.G_COLOR_FONDO_BASE;
                }
            }
            get
            {
                return g_bol_Obligatorio;
            }
        }
        public Color Exferia_RichTextBox_Fondo
        {
            set
            {
                g_obj_RichTextBox_Principal.BackColor = value;
            }
            get
            {
                return g_obj_RichTextBox_Principal.BackColor;
            }
        }
        public void Exferia_RichTextBox_ColorTexto(Color _clr_ColorTexto)
        {
            g_obj_RichTextBox_Principal.ForeColor = _clr_ColorTexto;
        }
        public void Exferia_RichTextBox_FuenteTexto(Font _fnt_Texto)
        {
            g_obj_RichTextBox_Principal.Font = _fnt_Texto;
        }

        public Color g_clr_BordeColorNormal = Color.Black;
        public Color Exferia_RichTextBox_BordeColor_Normal
        {
            get { return g_clr_BordeColorNormal; }
            set { g_clr_BordeColorNormal = value; }
        }

        public Color g_clr_BordeColorFoco = Color.Red;
        public Color Exferia_RichTextBox_BordeColor_Foco
        {
            get { return g_clr_BordeColorFoco; }
            set { g_clr_BordeColorFoco = value; }
        }

        public int Exferia_RichTextBox_MaxLength
        {
            get { return g_obj_RichTextBox_Principal.RTXT_MaxLength; }
            set { g_obj_RichTextBox_Principal.RTXT_MaxLength = value; }
        }
        
        public string Exferia_RichTextBox_SelectedText
        {
            get { return g_obj_RichTextBox_Principal.RTXT_SelectedText; }
            set { g_obj_RichTextBox_Principal.RTXT_SelectedText = value; }
        }

        public int Exferia_RichTextBox_SelectionStart
        {
            set
            {
                g_obj_RichTextBox_Principal.RTXT_SelectionStart = value;
            }
            get
            {
                return g_obj_RichTextBox_Principal.RTXT_SelectionStart;
            }

        }

        public int Exferia_RichTextBox_SelectionLength
        {
            set
            {
                g_obj_RichTextBox_Principal.RTXT_SelectionLength = value;
            }
            get
            {
                return g_obj_RichTextBox_Principal.RTXT_SelectionLength;
            }

        }

        public Font Exferia_RichTextBox_SelectionFont
        {
            set
            {
                g_obj_RichTextBox_Principal.SelectionFont = value;
            }
            get
            {
                return g_obj_RichTextBox_Principal.SelectionFont;
            }
        }

        public int Exferia_RichTextBox_SelectionIndent
        {
            set
            {
                g_obj_RichTextBox_Principal.SelectionIndent = value;
            }
            get
            {
                return g_obj_RichTextBox_Principal.SelectionIndent;
            }
        }

        public bool Exferia_RichTextBox_SelectionBullet
        {
            set
            {
                g_obj_RichTextBox_Principal.SelectionBullet = value;
            }
            get
            {
                return g_obj_RichTextBox_Principal.SelectionBullet;
            }
        }

        public Color Exferia_RichTextBox_SelectionColor
        {
            set
            {
                g_obj_RichTextBox_Principal.SelectionColor = value;
            }
            get
            {
                return g_obj_RichTextBox_Principal.SelectionColor;
            }
        }

        public Color Exferia_RichTextBox_SelectionBackColor
        {
            set
            {
                g_obj_RichTextBox_Principal.SelectionBackColor = value;
            }
            get
            {
                return g_obj_RichTextBox_Principal.SelectionBackColor;
            }
        }

        public string[] Exferia_RichTextBox_Lines
        {
            set
            {
                g_obj_RichTextBox_Principal.Lines = value;
            }
            get
            {
                return g_obj_RichTextBox_Principal.Lines;
            }
        }

        public int Exferia_RichTextBox_GetFirstCharIndexOfCurrentLine
        {
            get
            {
                return g_obj_RichTextBox_Principal.GetFirstCharIndexOfCurrentLine();
            }
        }

        public int Exferia_RichTextBox_GetLineFromCharIndex(int int_indice)
        {
          return g_obj_RichTextBox_Principal.GetLineFromCharIndex(int_indice);
        }

        public void Exferia_RichTexBox_Select(int int_start, int int_Tamanio)
        {
            g_obj_RichTextBox_Principal.Select(int_start, int_Tamanio);
        }

        public int Exferia_RichTexBox_CharOffset
        {
            set
            {
                g_obj_RichTextBox_Principal.SelectionCharOffset = value;
            }
            get
            {
                return g_obj_RichTextBox_Principal.SelectionCharOffset;
            }
        }

        public bool Exferia_RichTextBox_Multiline
        {
            get { return g_obj_RichTextBox_Principal.RTXT_Multiline; }
            set { g_obj_RichTextBox_Principal.RTXT_Multiline = value; }
        }

        public override string Text
        {
            get { return g_obj_RichTextBox_Principal.RTXT_Text; }
            set { g_obj_RichTextBox_Principal.RTXT_Text = value; }
        }

        public string Exferia_RichTextBox_RTF
        {
            get { return g_obj_RichTextBox_Principal.Rtf; }
            set { g_obj_RichTextBox_Principal.Rtf = value; }
        }

        public string Exferia_RichTextBox_Texto_Inicial
        {
            get { return g_obj_RichTextBox_Principal.RTXT_Texto_Inicial; }
            set { g_obj_RichTextBox_Principal.RTXT_Texto_Inicial = value; }
        }

        public bool Exferia_RichTextBox_TabStop_Txt
        {
            get { return g_obj_RichTextBox_Principal.RTXT_TabStop; }
            set { g_obj_RichTextBox_Principal.RTXT_TabStop = value; }
        }

        public bool Exferia_RichTextBox_NoBloquear
        {
            get { return g_obj_RichTextBox_Principal.RTXT_NoBloquear; }
            set
            {
                Focus();
                g_obj_RichTextBox_Principal.RTXT_NoBloquear = value;
            }
        }

        public void Exferia_RichTextBox_Foco()
        {
            g_obj_RichTextBox_Principal.RTXT_Foco();
            Invalidate();
        }

        public RichTextBoxScrollBars Exferia_RichTextBox_ScrollBars
        {
            get { return g_obj_RichTextBox_Principal.RTXT_ScrollBars; }
            set { g_obj_RichTextBox_Principal.RTXT_ScrollBars = value; }
        }

        public bool Exferia_RichTextBox_ReadOnly
        {
            get { return g_obj_RichTextBox_Principal.TXT_ReadOnly; }
            set { g_obj_RichTextBox_Principal.TXT_ReadOnly = value; }
        }

        public void Exferia_RichTextBox_AutoCompletar_Mayor(INTERNO_ValoresGenerales_Modelo _mdl_INTERNO_ValoresGenerales_Modelo)
        {
            g_obj_RichTextBox_Principal.TXT_AutoCompletar_Mayor = _mdl_INTERNO_ValoresGenerales_Modelo;
        }

        public bool Exferia_RichTextBox_TieneFoco
        {
            get { return g_obj_RichTextBox_Principal.g_bol_TieneFoco; }
        }


        //Se utiliza para copiar lo mismo del textbox actual a otro texto box cuando pieda el foco
        internal Exferia_TextBox g_ex_txt_paraCopiado = null;
        public Exferia_TextBox Exferia_TextBox_Objeto_CopiarInformacion
        {
            set
            {
                g_ex_txt_paraCopiado = value;
            }
        }

        #endregion

        public Exferia_RichTextBox()
        {
            DoubleBuffered = true;
            Padding = new Padding(2);
            Anchor = AnchorStyles.Top | AnchorStyles.Left;
            Height = 22;

            g_obj_RichTextBox_Principal = new RichTextBox_Principal();
            g_obj_RichTextBox_Principal.RichTextBox_Inicial_Refresh += new EventHandler(RichTextBox_Principal_Refresh);
            g_obj_RichTextBox_Principal.RichTextBox_Inicial_KeyPress += new KeyPressEventHandler(RichTextBox_Principal_KeyPress);
            g_obj_RichTextBox_Principal.RichTextBox_Inicial_KeyDown += new KeyEventHandler(RichTextBox_Principal_KeyDown);
            g_obj_RichTextBox_Principal.RichTextBox_Inicial_KeyUp += new KeyEventHandler(RichTextBox_Principal_KeyUp);
            g_obj_RichTextBox_Principal.RichTextBox_Inicial_TextChanged += new EventHandler(RichTextBox_Principal_TextChanged);
            g_obj_RichTextBox_Principal.RichTextBox_Inicial_GotFocus += new EventHandler(RichTextBox_Principal_GotFocus);
            g_obj_RichTextBox_Principal.RichTextBox_Inicial_LostFocus += new EventHandler(RichTextBox_Principal_LostFocus);

            Controls.Add(g_obj_RichTextBox_Principal);
        }

        #region Eventos Publicos 

        private void RichTextBox_Principal_Refresh(object sender, EventArgs e)
        {
            Invalidate();
        }

        public void Exferia_RichTextBox_Paste()
        {
            g_obj_RichTextBox_Principal.Paste();
        }
        public event KeyPressEventHandler Exferia_RichTextBox_Evento_KeyPress;
        private void RichTextBox_Principal_KeyPress(object sender, KeyPressEventArgs e)
        {
            //Para que salte el evento en el otro lado
            Exferia_RichTextBox_Evento_KeyPress?.Invoke(this, e);
        }

        public event KeyEventHandler Exferia_RichTextBox_Evento_KeyDown;
        private void RichTextBox_Principal_KeyDown(object sender, KeyEventArgs e)
        {
            if (!g_obj_RichTextBox_Principal.TXT_ReadOnly)
            {
                //Para que salte el evento en el otro lado
                Exferia_RichTextBox_Evento_KeyDown?.Invoke(this, e);
            }
        }

        public event KeyEventHandler Exferia_RichTextBox_Evento_KeyUp;
        private void RichTextBox_Principal_KeyUp(object sender, KeyEventArgs e)
        {
            //Para que salte el evento en el otro lado
            Exferia_RichTextBox_Evento_KeyUp?.Invoke(this, e);
        }

        public event EventHandler Exferia_RichTextBox_Evento_TextChanged;
        private void RichTextBox_Principal_TextChanged(object sender, EventArgs e)
        {
            //Para que salte el evento en el otro lado
            Exferia_RichTextBox_Evento_TextChanged?.Invoke(this, e);
        }

        public event EventHandler Exferia_RichTextBox_Evento_GotFocus;
        private void RichTextBox_Principal_GotFocus(object sender, EventArgs e)
        {
            //Para que salte el evento en el otro lado
            Exferia_RichTextBox_Evento_GotFocus?.Invoke(this, e);
        }

        public event EventHandler Exferia_RichTextBox_Evento_LostFocus;
        private void RichTextBox_Principal_LostFocus(object sender, EventArgs e)
        {
            if (g_ex_txt_paraCopiado != null)
            {
                if (g_ex_txt_paraCopiado.Text.Trim().Length == 0)
                {
                    g_ex_txt_paraCopiado.Text = g_obj_RichTextBox_Principal.RTXT_Text;
                }
            }

            //Para que salte el evento en el otro lado
            Exferia_RichTextBox_Evento_LostFocus?.Invoke(this, e);
        }
        #endregion

        #region Eventos Privados
        protected override void OnPaint(PaintEventArgs e)
        {
            e.Graphics.Clear(SystemColors.Window);
            using (Pen borderPen = new Pen(g_obj_RichTextBox_Principal.Focused ? g_clr_BordeColorFoco : g_clr_BordeColorNormal))
            {
                e.Graphics.DrawRectangle(borderPen, new Rectangle(0, 0, ClientSize.Width - 1, ClientSize.Height - 1));
            }

            base.OnPaint(e);
        }
        #endregion

        internal class RichTextBox_Principal : RichTextBox
        {
            public event KeyPressEventHandler RichTextBox_Inicial_KeyPress;
            public event KeyEventHandler RichTextBox_Inicial_KeyDown;
            public event KeyEventHandler RichTextBox_Inicial_KeyUp;
            public event EventHandler RichTextBox_Inicial_TextChanged;
            public event EventHandler RichTextBox_Inicial_GotFocus;
            public event EventHandler RichTextBox_Inicial_LostFocus;
            public event EventHandler RichTextBox_Inicial_Refresh;

            public bool g_bol_TieneFoco = false;

            #region Propiedades
            INTERNO_ValoresGenerales_Modelo g_mdl_INTERNO_ValoresGenerales_Modelo = null;
            public INTERNO_ValoresGenerales_Modelo TXT_AutoCompletar_Mayor
            {
                set
                {
                    g_mdl_INTERNO_ValoresGenerales_Modelo = value;

                    if (g_mdl_INTERNO_ValoresGenerales_Modelo != null && g_mdl_INTERNO_ValoresGenerales_Modelo.Control_Contabilidad != null && g_mdl_INTERNO_ValoresGenerales_Modelo.Control_Contabilidad.numeroDigitosContabilidad > 4)
                    {
                        MaxLength = g_mdl_INTERNO_ValoresGenerales_Modelo.Control_Contabilidad.numeroDigitosContabilidad;
                    }
                }
            }

            public int RTXT_MaxLength
            {
                get { return MaxLength; }
                set { MaxLength = value; }
            }

            public string RTXT_SelectedText
            {
                get { return SelectedText; }
                set { SelectedText = value; }
            }

            public int RTXT_SelectionStart
            {
                set { SelectionStart = value; }
                get { return SelectionStart; }
            }

            public int RTXT_SelectionLength
            {
                set { SelectionLength = value; }
                get { return SelectionLength; }
            }

            public bool RTXT_Multiline
            {
                get { return Multiline; }
                set { Multiline = value; }
            }

            public string RTXT_Text
            {
                get { return Text; }
                set { Text = value; }
            }

            public string RTXT_Texto_Inicial
            {
                get { return Text; }
                set { Text = value; }
            }

            public bool RTXT_TabStop
            {
                get { return TabStop; }
                set { TabStop = value; }
            }

            public bool RTXT_NoBloquear
            {
                get { return Enabled; }
                set
                {
                    Focus();
                    Enabled = value;
                }
            }

            public void RTXT_Foco()
            {
                Focus();
                Invalidate();
            }

            public RichTextBoxScrollBars RTXT_ScrollBars
            {
                get { return ScrollBars; }
                set { ScrollBars = value; }
            }

            public bool TXT_ReadOnly
            {
                get { return ReadOnly; }
                set { ReadOnly = value; }
            }
            #endregion

            public RichTextBox_Principal()
            {
                AutoSize = false;
                BorderStyle = BorderStyle.None;
                Dock = DockStyle.Fill;

                Enter += new EventHandler(rtxt_Refresh);
                Leave += new EventHandler(rtxt_Refresh);
                Resize += new EventHandler(rtxt_Refresh);
                EnabledChanged += new EventHandler(rtxt_Refresh);

                KeyPress += new KeyPressEventHandler(rtxt_KeyPress);
                KeyDown += new KeyEventHandler(rtxt_KeyDown);
                KeyUp += new KeyEventHandler(rtxt_KeyUp);
                TextChanged += new EventHandler(rtxt_TextChanged);
                GotFocus += new EventHandler(rtxt_GotFocus);
                LostFocus += new EventHandler(rtxt_LostFocus);

            }

            #region Eventos 
            private void rtxt_Refresh(object sender, EventArgs e)
            {
                RichTextBox_Inicial_Refresh?.Invoke(this, e);
            }

            private void rtxt_KeyPress(object sender, KeyPressEventArgs e)
            {
                //Para que salte el evento en el otro lado
                RichTextBox_Inicial_KeyPress?.Invoke(this, e);
            }

            private void rtxt_KeyDown(object sender, KeyEventArgs e)
            {
                //Para que salte el evento en el otro lado
                RichTextBox_Inicial_KeyDown?.Invoke(this, e);
            }

            private void rtxt_KeyUp(object sender, KeyEventArgs e)
            {
                //Para que salte el evento en el otro lado
                RichTextBox_Inicial_KeyUp?.Invoke(this, e);
            }

            private void rtxt_TextChanged(object sender, EventArgs e)
            {
                //Para que salte el evento en el otro lado
                RichTextBox_Inicial_TextChanged?.Invoke(this, e);
            }

            private void rtxt_GotFocus(object sender, EventArgs e)
            {
                g_bol_TieneFoco = true;

                if (!string.IsNullOrEmpty(Text))
                {
                    SelectionStart = 0;
                    SelectionLength = Text.Length;
                }

                //Para que salte el evento en el otro lado
                RichTextBox_Inicial_GotFocus?.Invoke(this, e);
            }

            private void rtxt_LostFocus(object sender, EventArgs e)
            {

                g_bol_TieneFoco = false;

                string str_Texto = Text;
                try
                {
                    if (g_mdl_INTERNO_ValoresGenerales_Modelo != null)
                    {
                        //Numero de digitos de contabilidad
                        int int_NumeroDigitosContabilidad = 4;
                        if (g_mdl_INTERNO_ValoresGenerales_Modelo.Control_Contabilidad != null && g_mdl_INTERNO_ValoresGenerales_Modelo.Control_Contabilidad.numeroDigitosContabilidad > 4)
                        {
                            int_NumeroDigitosContabilidad = g_mdl_INTERNO_ValoresGenerales_Modelo.Control_Contabilidad.numeroDigitosContabilidad;
                        }

                        if (str_Texto.Trim().Length > 0)
                        {
                            str_Texto = str_Texto.Trim();
                            int int_PosicionPunto = str_Texto.IndexOf('.');
                            if (int_PosicionPunto > -1)
                            {
                                //Buscar digitos antes del punto
                                string str_TextoAntesPunto = str_Texto.Substring(0, int_PosicionPunto);

                                //Buscar digitos despues del punto
                                string str_TextoDespuesPunto = "";
                                if (int_PosicionPunto > -1 && int_PosicionPunto != (str_Texto.Length - 1))
                                {
                                    str_TextoDespuesPunto = str_Texto.Substring(int_PosicionPunto + 1, str_Texto.Length - (int_PosicionPunto + 1));
                                }

                                int int_TotalCaracteres = str_TextoAntesPunto.Length + str_TextoDespuesPunto.Length;

                                //Ceros intermedios
                                if (int_NumeroDigitosContabilidad > int_TotalCaracteres)
                                {
                                    int int_CantidadCeros = int_NumeroDigitosContabilidad - int_TotalCaracteres;
                                    Text = str_TextoAntesPunto + "".PadRight(int_CantidadCeros, '0') + str_TextoDespuesPunto;
                                }
                                else
                                {
                                    Text = str_TextoAntesPunto + str_TextoDespuesPunto;
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Control_Errores.Errores_Log("", ex, nameof(Exferia_TextBox) + "/" + nameof(rtxt_LostFocus));
                }

                //Para que salte el evento en el otro lado
                RichTextBox_Inicial_LostFocus?.Invoke(this, e);
            }

            #endregion

        }


    }
}
