package tutorialJava.examenes.examen20250616.controlador;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import tutorialJava.examenes.examen20250616.modelo.Equipo;
import tutorialJava.examenes.examen20250616.modelo.Socio;

public class ControladorSocio {

	/**
	 * 
	 * @return
	 */
	public static List<Socio> findAllByEquipoAndOrder (int idEquipo, 
			String campoOrdenacion) {
		
		List<Socio> list = new ArrayList<Socio>();
		
		try {
			Connection conn = ConnectionManager.getConnection();
			PreparedStatement ps = conn.prepareStatement(
					"select * from socio where idEquipo = ? "
					+ "order by " + campoOrdenacion);
			ps.setInt(1, idEquipo);
			
			ResultSet rs = ps.executeQuery();
			while (rs.next()) {
				list.add(getSocioFromResultSet(rs));
			}
		}
		catch (Exception ex) {
			ex.printStackTrace();
		}
		
		return list;
	}

	
	public static Socio getPrimero() {
		return getSocioFromSql("select * from socio order by id limit 1");
	}
	
	public static Socio getUltimo() {
		return getSocioFromSql("select * from socio order by id desc limit 1");
	}

	public static Socio getSiguiente(int idActual) {
		return getSocioFromSql("select * from socio "
				+ "where id > " + idActual + 
				" order by id limit 1");
	}
	
	public static Socio getAnterior(int idActual) {
		return getSocioFromSql("select * from socio "
				+ "where id < " + idActual + 
				" order by id desc limit 1");
	}
	

	/**
	 * 
	 * @return
	 */
	public static Socio getSocioFromSql (String sql) {
		Socio s = null;
		
		try {
			Connection conn = ConnectionManager.getConnection();
			PreparedStatement ps = conn.prepareStatement(sql);
			ResultSet rs =  ps.executeQuery();
			if (rs.next()) {
				s = getSocioFromResultSet(rs);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return s;
	}
	
	
	/**
	 * 
	 * @return
	 */
	private static Socio getSocioFromResultSet (ResultSet rs) throws Exception {
		Socio s = new Socio();
		s.setId(rs.getInt("id"));
		s.setNombre(rs.getString("nombre"));
		s.setApellido1(rs.getString("apellido1"));
		s.setApellido2(rs.getString("apellido2"));
		s.setFechaNacimiento(rs.getDate("fechaNacimiento"));
		s.setAntiguedadAnios(rs.getInt("antiguedadAnios"));
		s.setActivo(rs.getBoolean("activo")); 
		s.setIdEquipo(rs.getInt("idEquipo"));
		return s;
	}
	
	/**
	 * 
	 * @param s
	 * @return
	 */
	public static int guardar (Socio s) {
		if (s.getId() == 0) {
			return insertar(s);
		}
		else {
			return modificar(s);
		}
	}
	
	
	
	public static int modificar (Socio s) {
		try {
			Connection conn = ConnectionManager.getConnection();
			PreparedStatement ps = conn.prepareStatement(
					"update socio set nombre = ?, apellido1 = ?, "
					+ "apellido2 = ?, fechaNacimiento = ?, "
					+ "antiguedadAnios = ?, activo = ?, "
					+ "idEquipo = ? "
					+ "where id = ?");
			ps.setString(1, s.getNombre());
			ps.setString(2, s.getApellido1());
			ps.setString(3, s.getApellido2());
			ps.setDate(4, new java.sql.Date(s.getFechaNacimiento().getTime()));
			ps.setInt(5, s.getAntiguedadAnios());
			ps.setBoolean(6, s.isActivo());
			ps.setInt(7, s.getIdEquipo());
			ps.setInt(8, s.getId());
			return ps.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return 0;
	}
	
	
	public static int insertar (Socio s) {
		try {
			Connection conn = ConnectionManager.getConnection();
			PreparedStatement ps = conn.prepareStatement(
					"insert into socio (id, nombre, apellido1, apellido2,"
					+ "fechaNacimiento, antiguedadAnios, activo, idEquipo)"
					+ " values (?, ?, ?, ?, ?, ?, ?, ?)");
			ps.setInt(1, nuevoId());
			ps.setString(2, s.getNombre());
			ps.setString(3, s.getApellido1());
			ps.setString(4, s.getApellido2());
			ps.setDate(5, new java.sql.Date(s.getFechaNacimiento().getTime()));
			ps.setInt(6, s.getAntiguedadAnios());
			ps.setBoolean(7, s.isActivo());
			ps.setInt(8, s.getIdEquipo());
			return ps.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}
	
	
	/**
	 * 
	 * @return
	 */
	private static int nuevoId () {
		try {
			Connection conn = ConnectionManager.getConnection();
			PreparedStatement ps = conn.prepareStatement(
					"select max(id) from socio");
			ResultSet rs = ps.executeQuery();
			if (rs.next()) {
				return rs.getInt(1) + 1;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return -1;
	}
	
	/**
	 * 
	 * @return
	 */
	public static int eliminar (int id) {
		try {
			Connection conn = ConnectionManager.getConnection();
			PreparedStatement ps = conn.prepareStatement(
					"delete from socio where id = ?");
			ps.setInt(1, id);
			return ps.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}

}
















