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

namespace Exferia_Formularios
{
    public partial class PE_Mensajes_3Respuestas : Form
    {
        #region Librerias Externas
        [DllImport("dwmapi.dll")]
        public static extern int DwmSetWindowAttribute(IntPtr hwnd, int attr, ref int attrValue, int attrSize);

        [DllImport("dwmapi.dll")]
        public static extern int DwmIsCompositionEnabled(ref int pfEnabled);

        [DllImport("dwmapi.dll")]
        public static extern int DwmExtendFrameIntoClientArea(IntPtr hWnd, ref MARGINS pMarInset);
        #endregion

        private int g_int_BotonPulsado = 3;
        public int PE_Mensajes_3Respuestas_Respuesta
        {
            set
            {
                g_int_BotonPulsado = value;
            }
            get
            {
                return g_int_BotonPulsado;
            }
        }


        //Propiedad que define el color del titulo y los bordes dde la pantalla 
        public Color PE_Mensajes_3Respuestas_ColorTituloyBordes
        {
            set
            {
                lbl_Titulo.BackColor = value;
                pnl_Titulo.BackColor = value;
                pnl_BordeDerecho.BackColor = value;
                pnl_BordeInferior.BackColor = value;
                pnl_BordeIzquierdo.BackColor = value;
            }
            get
            {
                return lbl_Titulo.BackColor;
            }
        }

        public PE_Mensajes_3Respuestas(string _str_Mensaje, string _str_Titulo, string _str_Boton1, string _str_Boton2, string _str_Boton3)
        {
            InitializeComponent();

            Repintar.Empezar(this);

            //Carga Datos ..............................
            lbl_Mensaje.Text = _str_Mensaje;
            if (_str_Titulo.Trim().Length > 0)
            {
                lbl_Titulo.Text = _str_Titulo;
            }


            ex_btn_Boton1.Text = _str_Boton1;
            ex_btn_Boton2.Text = _str_Boton2;
            ex_btn_Boton3.Text = _str_Boton3;
            pct_Imagen.Image = Exferia_Aplicacion.General.Imagenes.G_IMG_PREGUNTA;

            int int_AnchoMinimo = 300;
            int int_AltoMinimo = 194;

            Size desktopSize;
            desktopSize = System.Windows.Forms.SystemInformation.PrimaryMonitorSize;
            int int_AnchoMaximo = (desktopSize.Width / 4) * 2;
            int int_AltoMaximo = (desktopSize.Height / 4) * 2;

            //Calculo tamaño del Formulario
            //8pixeles por letra
            //Hcer el calculo teniendo en cuenta los saltos de linea
            string[] TotalSaltosLineas = _str_Mensaje.Split('\n');

            //Ancho del label ...............................
            int int_AnchoCalculadoSegunTexto = 0;
            foreach (string str_linea in TotalSaltosLineas)
            {
                if ((str_linea.Length * 8) + lbl_Mensaje.Location.X + 12 > int_AnchoCalculadoSegunTexto)
                {
                    int_AnchoCalculadoSegunTexto = (str_linea.Length * 8) + lbl_Mensaje.Location.X + 12;//12 es el margen derecho
                }
            }
            if (int_AnchoMinimo < int_AnchoCalculadoSegunTexto)
            {
                if (int_AnchoMaximo > int_AnchoCalculadoSegunTexto)
                {
                    int_AnchoMaximo = int_AnchoCalculadoSegunTexto;
                }
            }
            else
            {
                int_AnchoMaximo = int_AnchoMinimo;
            }
            //....................................................... 

            //Calcular el Alto 
            int int_LabelMaximo = int_AnchoMaximo - 12 - lbl_Mensaje.Location.X;
            //Cuantas letras caben aqui
            int int_TotalFilas = 0;

            //Sumarle los Saltos de linea si los hubiera
            foreach (string str_linea in TotalSaltosLineas)
            {
                if ((str_linea.Length * 8) / int_LabelMaximo >= 1)
                {
                    int_TotalFilas += (str_linea.Length * 8) / int_LabelMaximo;
                }
                else
                {
                    int_TotalFilas += 1;
                }
            }

            int int_AltoTotalLabel = 68;
            if (int_TotalFilas > 4)
            {
                int_AltoTotalLabel = int_TotalFilas * 17;
            }

            int_AltoMinimo = (int_AltoTotalLabel * int_AltoMinimo) / 68;


            Size = new Size(int_AnchoMaximo, int_AltoMinimo);


            ex_btn_Boton1.Focus();

        }


        #region Botones
        private void ex_btn_Boton1_Click(object sender, EventArgs e)
        {
            Boton_1();
        }
        private void Boton_1()
        {
            g_int_BotonPulsado = 1;
            Close();
        }

        private void ex_btn_Boton2_Click(object sender, EventArgs e)
        {
            Boton_2();
        }
        private void Boton_2()
        {
            g_int_BotonPulsado = 2;
            Close();
        }

        private void ex_btn_Boton3_Click(object sender, EventArgs e)
        {
            Boton_3();
        }
        private void Boton_3()
        {
            g_int_BotonPulsado = 3;
            Close();
        }

        #endregion

        // Teclas de Acceso Rapido
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            // Boton 1
            if (keyData == Keys.D1 || keyData == Keys.NumPad1)
            {
                Boton_1();
                return true;
            }
            // Boton 2
            else if (keyData == Keys.D2 || keyData == Keys.NumPad2)
            {
                Boton_2();
                return true;
            }
            // Boton 3
            else if (keyData == Keys.D3 || keyData == Keys.NumPad3 || keyData == Keys.N || keyData == Keys.F10)
            {
                Boton_3();
                return true;
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }

        #region Control Grafico de la Pantalla

        protected override void WndProc(ref Message m)
        {
            //Sombra de la pantalla ..................................................
            if (m.Msg == WM_NCPAINT)// box shadow
            {
                if (g_bol_HabilitarSombra)
                {
                    var v = 2;
                    DwmSetWindowAttribute(Handle, 2, ref v, 4);
                    MARGINS margins = new MARGINS()
                    {
                        bottomHeight = 1,
                        leftWidth = 1,
                        rightWidth = 1,
                        topHeight = 1
                    };
                    DwmExtendFrameIntoClientArea(Handle, ref margins);
                }
            }

            base.WndProc(ref m);

            if (m.Msg == WM_NCHITTEST && (int)m.Result == HTCLIENT)     // drag the form
                m.Result = (IntPtr)HTCAPTION;
        }

        private void PE_Mensajes_3Respuestas_Resize(object sender, EventArgs e)
        {
            pnl_BordeDerecho.Location = new Point(Width - 3, pnl_BordeDerecho.Location.Y);
            pnl_BordeDerecho.Size = new Size(2, Height - 3);

            pnl_BordeInferior.Location = new Point(pnl_BordeInferior.Location.X, Height - 3);
            pnl_BordeInferior.Size = new Size(Width - 4, 2);

            pnl_Titulo.Size = new Size(Width - 4, pnl_Titulo.Height);
        }

        #endregion

        #region Crear Sombra de la pantalla
        [DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")]
        private static extern IntPtr CreateRoundRectRgn
        (
            int nLeftRect, // x-coordinate of upper-left corner
            int nTopRect, // y-coordinate of upper-left corner
            int nRightRect, // x-coordinate of lower-right corner
            int nBottomRect, // y-coordinate of lower-right corner
            int nWidthEllipse, // height of ellipse
            int nHeightEllipse // width of ellipse
         );

        private bool g_bol_HabilitarSombra;                     // variables for box shadow
        private const int CS_DROPSHADOW = 0x00020000;
        private const int WM_NCPAINT = 0x0085;
        private const int WM_ACTIVATEAPP = 0x001C;

        public struct MARGINS                           // struct for box shadow
        {
            public int leftWidth;
            public int rightWidth;
            public int topHeight;
            public int bottomHeight;
        }

        private const int WM_NCHITTEST = 0x84;          // variables for dragging the form
        private const int HTCLIENT = 0x1;
        private const int HTCAPTION = 0x2;

        protected override CreateParams CreateParams
        {
            get
            {
                g_bol_HabilitarSombra = CheckAeroEnabled();

                CreateParams cp = base.CreateParams;
                if (!g_bol_HabilitarSombra)
                    cp.ClassStyle |= CS_DROPSHADOW;

                return cp;
            }
        }

        private bool CheckAeroEnabled()
        {
            if (Environment.OSVersion.Version.Major >= 6)
            {
                int enabled = 0;
                DwmIsCompositionEnabled(ref enabled);
                return (enabled == 1) ? true : false;
            }
            return false;
        }

        #endregion

        
    }
}
