package net.h4ckx0r.controlador;

import net.h4ckx0r.modelo.Curso;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class ControladorCurso {
    public static List<Curso> getAllCursos() {
        Connection conn = GestorConexiones.getConexion();
        List<Curso> cursos = new ArrayList<>();

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

            while (rs.next()) {
                cursos.add(new Curso(rs.getInt("id"), rs.getString("descripcion")));
            }

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

        return cursos;
    }

    public static Curso getCurso(int id) {
        Connection conn = GestorConexiones.getConexion();

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


            ResultSet rs = pst.executeQuery();

            if (rs.next()) {
                return new Curso(rs.getInt("id"), rs.getString("descripcion"));
            }

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

        return null;
    }

    public static Curso getPrimerCurso() {
        Connection conn = GestorConexiones.getConexion();

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

            if (rs.next()) {
                return new Curso(rs.getInt("id"), rs.getString("descripcion"));
            }

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

        return null;
    }

    public static Curso getUltimoCurso() {
        Connection conn = GestorConexiones.getConexion();

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

            if (rs.next()) {
                return new Curso(rs.getInt("id"), rs.getString("descripcion"));
            }

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

        return null;
    }

    public static Curso getSiguienteCurso(Curso cursoActual) {
        Connection conn = GestorConexiones.getConexion();

        try {
            PreparedStatement pst = conn.prepareStatement("SELECT * FROM curso WHERE id > ? LIMIT 1");
            pst.setInt(1, cursoActual.getId());
            ResultSet rs = pst.executeQuery();
            if (rs.next()) {
                return new Curso(rs.getInt("id"), rs.getString("descripcion"));
            }

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

        return null;

    }

    public static Curso getAnteriorCurso(Curso cursoActual) {
        Connection conn = GestorConexiones.getConexion();

        try {
            PreparedStatement pst = conn.prepareStatement("SELECT * FROM curso WHERE id < ? ORDER BY id DESC LIMIT 1");
            pst.setInt(1, cursoActual.getId());
            ResultSet rs = pst.executeQuery();
            if (rs.next()) {
                return new Curso(rs.getInt("id"), rs.getString("descripcion"));
            }

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

        return null;

    }

    public static int guardarCurso(Curso curso) {
        if (curso.getId() == -1) {
            curso.setId(getUltimoCurso().getId() + 1);
            return nuevoCurso(curso);
        }
        Connection conn = GestorConexiones.getConexion();
        try {
            PreparedStatement pst = conn.prepareStatement("UPDATE curso SET descripcion = ? WHERE id = ?");
            pst.setString(1, curso.getDescripcion());
            pst.setInt(2, curso.getId());
            return pst.executeUpdate();
        } catch (SQLException e) {
            return 0;
            //throw new RuntimeException(e);
        }
    }

    public static int nuevoCurso(Curso curso) {
        Connection conn = GestorConexiones.getConexion();
        try {
            PreparedStatement pst = conn.prepareStatement("INSERT INTO curso (id,descripcion) VALUES (?,?)");
            pst.setInt(1, curso.getId());
            pst.setString(2, curso.getDescripcion());
            return pst.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

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