namespace Generator_Class_BD
{
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Windows.Forms;
    using System.Linq;
    using System.Collections.Generic;
    using Generator_Class_BD.Clases.Consultas;

    public partial class FormCamposFront : Form
    {
        private FormSelector _form2;

        public string cadenaTablas { get; set; }
        public string cadenaConexion { get; set; }
        public string baseDatos { get; set; }
        public string NombreObjeto { get; set; }

        public FormCamposFront()
        {
            InitializeComponent();
        }

        public FormCamposFront(FormSelector form2)
        {
            InitializeComponent();
            _form2 = form2;
        }

        private void FormCamposFront_Load(object sender, EventArgs e)
        {
            this.dgvCamposFront.DataSource = this.CargarCamposTablas().Tables[0];
        }

        private DataSet CargarCamposTablas()
        {
            SqlCommand cmd = new SqlCommand();
            SqlDataAdapter da = new SqlDataAdapter();
            DataSet ds = new DataSet();

            try
            {
                SqlConnection con = new SqlConnection(cadenaConexion);
                con.Open();
                cmd.Connection = con;
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = $"Use { baseDatos} {string.Format(Recursos.RecursosBD.QueryCamposPorTablas, cadenaTablas)}";
                da.SelectCommand = cmd;
                da.Fill(ds);
            }
            catch (SqlException x)
            {
                MessageBox.Show("Error de SQL :" + x.Message);
            }
            catch (Exception y)
            {
                MessageBox.Show("Error :" + y.Message);
            }
            return ds;
        }

        private void dgvCamposFront_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                DataGridViewRow filaSeleccionada = this.dgvCamposFront.Rows[e.RowIndex];
                if (filaSeleccionada.Cells["colSelectFront"].Value == null)
                {
                    filaSeleccionada.Cells["colSelectFront"].Value = true;
                    List<DataGridViewRow> dgvList = this.dgvCamposFront.Rows.Cast<DataGridViewRow>().ToList();
                    string nombreTabla = filaSeleccionada.Cells["colTablaFront"].Value.ToString();
                    dgvList.Where(dgv => dgv.Cells["colSelectFront"].Value != null && dgv.Cells["colCampoFront"].Value != filaSeleccionada.Cells["colCampoFront"].Value)
                    .ToList()
                    .ForEach((dgv) =>
                    {
                        if (dgv.Cells["colTablaFront"].Value.ToString() == filaSeleccionada.Cells["colTablaFront"].Value.ToString() && bool.Parse(dgv.Cells["colSelectFront"].Value.ToString()) == true)
                            dgv.Cells["colSelectFront"].Value = null;
                    });
                }
                else if (bool.Parse(filaSeleccionada.Cells["colSelectFront"].Value.ToString()) == true)
                    filaSeleccionada.Cells["colSelectFront"].Value = null;
                else if (bool.Parse(filaSeleccionada.Cells["colSelectFront"].Value.ToString()) == false)
                    filaSeleccionada.Cells["colSelectFront"].Value = true;
            }
        }

        private void guardarCampos_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void cancelar_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}