package capitulo08.bloque01y02.operaciones;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Select {
    public static void mostrarTabla(Connection conn, String tabla) throws SQLException {
        Statement st = conn.createStatement();
        ResultSet rs = st.executeQuery("select * from tutorialjavacoches." + tabla + ";");

        switch (tabla) {
            case "fabricante": {
                System.out.println("ID\t | CIF\t\t\t | Nombre");
                while (rs.next()) {
                    System.out.println(rs.getInt(1) + "\t | " + rs.getString(2) + "\t | " + rs.getString(3));
                }
                break;
            }
            case "coche": {
                System.out.println("ID\t | ID F\t | Nº Bastidor\t | Modelo\t | Color");
                while (rs.next()) {
                    System.out.println(rs.getInt(1) + "\t | " + rs.getInt(2) + "\t | " + rs.getString(3) + "\t | " + rs.getString(4) + "\t | " + rs.getString(5));
                }
                break;
            }
            case "concesionario": {
                System.out.println("ID\t | CIF\t\t\t | Nombre\t\t\t\t | Localidad");
                while (rs.next()) {
                    System.out.println(rs.getInt(1) + "\t | " + rs.getString(2) + "\t | " + rs.getString(3) + "\t | " + rs.getString(4));
                }
                break;
            }
            case "cliente": {
                System.out.println("ID\t | Nombre\t\t | Apellidos\t | Localidad\t | DNI\t | Fecha Nacimiento\t | Activo");
                while (rs.next()) {
                    System.out.println(rs.getInt(1) + "\t | " + rs.getString(2) + "\t\t | " + rs.getString(3) + "\t | " + rs.getString(4) + "\t | " + rs.getString(5) + "\t | " + rs.getDate(6) + "\t | " + rs.getInt(7));
                }
            }
            case "venta": {
                System.out.println("ID\t | ID Cliente\t | ID Concesionario\t | ID Coche\t | Fecha\t | Precio Venta");
                while (rs.next()) {
                    System.out.println(rs.getInt(1) + "\t | " + rs.getInt(2) + "\t | " + rs.getInt(3) + "\t | " + rs.getInt(4) + "\t | " + rs.getDate(5) + "\t | " + rs.getInt(6));
                }
            }
        }
    }

    public static int getNextId(Connection conn, String tabla) throws SQLException {
        Statement st = conn.createStatement();
        ResultSet rs = st.executeQuery("select max(id) from tutorialjavacoches." + tabla + ";");
        rs.next();
        return (rs.getInt(1) + 1);
    }

    public static boolean isIdValid(Connection conn, String tabla, int id) throws SQLException {
        Statement st = conn.createStatement();
        ResultSet rs = st.executeQuery("select id from tutorialjavacoches." + tabla + " where id=" + id + ";");
        if (rs.next()) {
            return rs.getInt(1) == id;
        } else {
            return false;
        }
    }

}