package net.h4ckx0r.controlador;

import net.h4ckx0r.modelo.Curso;
import net.h4ckx0r.modelo.Materia;

import java.sql.*;

public class ControladorMateria {
    public static Materia getPrimeraMateria() {
        Connection conn = GestorConexiones.getConexion();

        try {
            Statement st = conn.createStatement();
            ResultSet rs = st.executeQuery("SELECT * FROM materia LIMIT 1");

            if (rs.next()) {
                return new Materia(rs.getInt("id"), rs.getString("nombre"), rs.getString("acronimo"), rs.getInt("curso_id"));
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        return null;
    }

    public static Materia getUltimaMateria() {
        Connection conn = GestorConexiones.getConexion();

        try {
            Statement st = conn.createStatement();
            ResultSet rs = st.executeQuery("SELECT * FROM materia ORDER BY id DESC LIMIT 1");

            if (rs.next()) {
                return new Materia(rs.getInt("id"), rs.getString("nombre"), rs.getString("acronimo"), rs.getInt("curso_id"));
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        return null;
    }

    public static Materia getSiguienteMateria(Materia materiaActual) {
        Connection conn = GestorConexiones.getConexion();

        try {
            PreparedStatement pst = conn.prepareStatement("SELECT * FROM materia WHERE id > ? LIMIT 1");
            pst.setInt(1, materiaActual.getId());
            ResultSet rs = pst.executeQuery();
            if (rs.next()) {
                return new Materia(rs.getInt("id"), rs.getString("nombre"), rs.getString("acronimo"), rs.getInt("curso_id"));
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        return null;

    }

    public static Materia getAnteriorMateria(Materia materiaActual) {
        Connection conn = GestorConexiones.getConexion();

        try {
            PreparedStatement pst = conn.prepareStatement("SELECT * FROM materia WHERE id < ? ORDER BY id DESC LIMIT 1");
            pst.setInt(1, materiaActual.getId());
            ResultSet rs = pst.executeQuery();
            if (rs.next()) {
                return new Materia(rs.getInt("id"), rs.getString("nombre"), rs.getString("acronimo"), rs.getInt("curso_id"));
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        return null;

    }

    public static int borrarMateria(Materia materiaActual) {
        Connection conn = GestorConexiones.getConexion();
        try {
            PreparedStatement pst = conn.prepareStatement("DELETE FROM materia WHERE id = ? LIMIT 1");
            pst.setInt(1, materiaActual.getId());
            return pst.executeUpdate();
        } catch (SQLException e) {
            return 0;
            //throw new RuntimeException(e);
        }
    }

    public static int guardarMateria(Materia materia) {
        if (materia.getId() == -1) {
            materia.setId(getUltimaMateria().getId() + 1);
            return nuevaMateria(materia);
        }
        Connection conn = GestorConexiones.getConexion();
        try {
            PreparedStatement pst = conn.prepareStatement("UPDATE materia SET nombre = ?, acronimo = ?, curso_id = ? WHERE id = ?");
            pst.setString(1, materia.getNombre());
            pst.setString(2, materia.getAcronimo());
            pst.setInt(3, materia.getCurso_id());
            pst.setInt(4, materia.getId());
            return pst.executeUpdate();
        } catch (SQLException e) {
            return 0;
            //throw new RuntimeException(e);
        }
    }

    public static int nuevaMateria(Materia materia) {
        Connection conn = GestorConexiones.getConexion();
        try {
            PreparedStatement pst = conn.prepareStatement("INSERT INTO materia (id,nombre,acronimo,curso_id) VALUES (?,?,?,?)");
            pst.setInt(1, materia.getId());
            pst.setString(2, materia.getNombre());
            pst.setString(3, materia.getAcronimo());
            pst.setInt(4, materia.getCurso_id());
            return pst.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public static Materia getMateria(int id) {
        Connection conn = GestorConexiones.getConexion();

        try {
            PreparedStatement pst = conn.prepareStatement("SELECT * FROM materia WHERE id = ? LIMIT 1");
            pst.setInt(1, id);
            ResultSet rs = pst.executeQuery();

            if (rs.next()) {
                return new Materia(rs.getInt("id"), rs.getString("nombre"), rs.getString("acronimo"), rs.getInt("curso_id"));
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        return null;
    }
}
