using System.Data;

namespace GBDD
{
    public partial class AddViolation : Form
    {
        private int violationId = 0;
        private int selectedVehicleId = 0;

        public AddViolation()
        {
            InitializeComponent();
            Load += AddViolation_Load;
            comboBox1.SelectedIndexChanged += comboBox1_SelectedIndexChanged;
            button1.Click += button1_Click;
            button2.Click += button2_Click;
        }

        public AddViolation(int id) : this()
        {
            violationId = id;
        }

        private void AddViolation_Load(object? sender, EventArgs e)
        {
            comboBox1.DataSource = Db.Select("""
                select id, last_name || ' ' || first_name as name
                from driver
                order by id
                """);
            comboBox1.DisplayMember = "name";
            comboBox1.ValueMember = "id";

            comboBox2.DisplayMember = "name";
            comboBox2.ValueMember = "id";
            comboBox2.Enabled = false;

            comboBox3.DataSource = Db.Select("""
                select id, name
                from violation_type
                order by id
                """);
            comboBox3.DisplayMember = "name";
            comboBox3.ValueMember = "id";

            if (violationId != 0)
            {
                var table = Db.Select($"""
                    select v.driver_id, v.vehicle_id, v.violation_type_id, f.uin, f.amount
                    from violation v
                    join fine f on f.violation_id = v.id
                    where v.id = {violationId}
                    """);

                var row = table.Rows[0];
                selectedVehicleId = Convert.ToInt32(row["vehicle_id"]);
                comboBox1.SelectedValue = row["driver_id"];
                comboBox3.SelectedValue = row["violation_type_id"];
                textBox1.Text = row["uin"].ToString();
                textBox2.Text = row["amount"].ToString();
            }
            else if (comboBox1.SelectedValue != null)
            {
                LoadVehicles();
            }
        }

        private void comboBox1_SelectedIndexChanged(object? sender, EventArgs e)
        {
            LoadVehicles();
        }

        private void LoadVehicles()
        {
            if (comboBox1.SelectedValue == null || comboBox1.SelectedValue is DataRowView)
            {
                return;
            }

            int driverId = Convert.ToInt32(comboBox1.SelectedValue);

            comboBox2.DataSource = Db.Select($"""
                select v.id, v.plate_number as name
                from ownership o
                join vehicle v on v.id = o.vehicle_id
                where o.driver_id = {driverId}
                order by v.id
                """);

            comboBox2.DisplayMember = "name";
            comboBox2.ValueMember = "id";
            comboBox2.Enabled = comboBox2.Items.Count > 0;

            if (selectedVehicleId != 0)
            {
                comboBox2.SelectedValue = selectedVehicleId;
                selectedVehicleId = 0;
            }
        }

        private void button1_Click(object? sender, EventArgs e)
        {
            int driverId = Convert.ToInt32(comboBox1.SelectedValue);
            int vehicleId = Convert.ToInt32(comboBox2.SelectedValue);
            int typeId = Convert.ToInt32(comboBox3.SelectedValue);

            if (violationId == 0)
            {
                Db.Execute(SqlQueries.AddViolation(
                    driverId,
                    vehicleId,
                    typeId,
                    textBox1.Text,
                    textBox2.Text));
            }
            else
            {
                Db.Execute($"""
                    select update_violation(
                        {violationId},
                        {driverId},
                        {vehicleId},
                        {typeId},
                        '{textBox1.Text}',
                        {textBox2.Text}
                    )
                    """);
            }

            Close();
        }

        private void button2_Click(object? sender, EventArgs e)
        {
            Close();
        }
    }
}
