package tutorialJava.capitulo9_AWT_SWING.ejemplos.ejemplo01_Fabricante.controlador;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import tutorialJava.capitulo9_AWT_SWING.ejemplos.ejemplo01_Fabricante.modelo.Fabricante;

public class ControladorFabricante {


	public static Fabricante getPrimero() {
		return getFabricante("Select * from fabricante order by id asc limit 1");
	}

	public static Fabricante getUltimo() {
		return getFabricante("Select * from fabricante order by id desc limit 1");
	}

	
	public static Fabricante getSiguiente(int idActual) {
		return getFabricante("Select * from fabricante "
			+ "where id > " + idActual + " order by id asc limit 1");
	}
	
	public static Fabricante getAnterior(int idActual) {
		return getFabricante("Select * from fabricante "
			+ "where id < " + idActual + " order by id desc limit 1");
	}
	
	
	public static Fabricante getFabricante(String sql) {
		try {
			Connection conn = ConnectionManager.getConnection();
			
			Statement s = conn.createStatement();
			ResultSet rs = 
					s.executeQuery(sql);
			
			if (rs.next()) {
				Fabricante f = new Fabricante();
				f.setId(rs.getInt("id"));
				f.setNombre(rs.getString("nombre"));
				f.setCif(rs.getString("cif"));
				return f;
			}
		}
		catch (Exception ex) {
			ex.printStackTrace();
		}
		return null;
	}
	

	
	public static List<Fabricante> findAllFabricante() {
		List<Fabricante> lista = new ArrayList<Fabricante>();
		try {
			Connection conn = ConnectionManager.getConnection();
			
			Statement s = conn.createStatement();
			ResultSet rs = 
					s.executeQuery("select * from fabricante");
			
			while (rs.next()) {
				Fabricante f = new Fabricante();
				f.setId(rs.getInt("id"));
				f.setNombre(rs.getString("nombre"));
				f.setCif(rs.getString("cif"));
				lista.add(f);
			}
		}
		catch (Exception ex) {
			ex.printStackTrace();
		}
		return lista;
	}
	
	
	
	
	
	public static int modificaFabricante(Fabricante f) {
		try {
			Connection conn = ConnectionManager.getConnection();
			String sql = "update tutorialjavacoches.fabricante "
					+ "set nombre = ?, cif = ? "
					+ "where id = ?";
			
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setString(1, f.getNombre());
			ps.setString(2, f.getCif());
			ps.setInt(3, f.getId());
			
			return ps.executeUpdate();
		}
		catch (Exception ex) {
			ex.printStackTrace();
		}
		return 0;
	}

	
	
	/**
	 * 
	 * @return
	 * @throws SQLException 
	 */
	private static int getSiguienteIdValido(Connection conn) throws SQLException {
		Statement s = conn.createStatement();
		ResultSet rs = s.executeQuery("select max(id) as maximoId "
				+ "from fabricante");
	
		if (rs.next()) {
			return rs.getInt(1) + 1;
		}
		
		return 1;
	}

	/**
	 * 
	 * @param f
	 * @return
	 */
	public static int insertaFabricante(Fabricante f) {
		try {
			Connection conn = ConnectionManager.getConnection();
			String sql = "insert into fabricante (id, nombre, cif) "
					+ "values (?, ?, ?)";
			
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setInt(1, getSiguienteIdValido(conn));
			ps.setString(2, f.getNombre());
			ps.setString(3, f.getCif());
			
			return ps.executeUpdate();
		}
		catch (Exception ex) {
			ex.printStackTrace();
		}
		return 0;
	}

	
	/**
	 * 
	 * @param f
	 * @return
	 */
	public static int eliminaFabricante(int idFabricante) {
		try {
			Connection conn = ConnectionManager.getConnection();
			String sql = "delete from fabricante where id = ?";
			
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setInt(1, idFabricante);
			
			return ps.executeUpdate();
		}
		catch (Exception ex) {
			ex.printStackTrace();
		}
		return 0;
	}

}










