package projet;

import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.time.LocalDate;

public class Bloc extends ParamBD {
    private int id;
    private Type type;
    private String contenu;
    private LocalDate dateCreation;
    private LocalDate dateModification;
    private int ordre;
    private String metadata;
    
	public enum Type {
	    TEXTE,
	    LISTE,
	    TITRE,
	    CODE,
	    PAGE
	}
	
	public Bloc() {
		
	}
	
	public Bloc(int id, Type type, String contenu, int ordre, String metadata) {
	    this.id = id;
	    this.type = type;
	    this.contenu = contenu;
	    this.ordre = ordre;
	    this.dateCreation = LocalDate.now();
	    this.dateModification = LocalDate.now();
	    this.metadata = metadata;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public Type getType() {
		return type;
	}

	public void setType(Type type) {
		this.type = type;
	}

	public String getContenu() {
		return contenu;
	}

	public void setContenu(String contenu) {
		this.contenu = contenu;
	}

	public LocalDate getDateCreation() {
		return dateCreation;
	}

	public void setDateCreation(LocalDate dateCreation) {
		this.dateCreation = dateCreation;
	}

	public LocalDate getDateModification() {
		return dateModification;
	}

	public void setDateModification(LocalDate dateModification) {
		this.dateModification = dateModification;
	}

	public int getOrdre() {
		return ordre;
	}

	public void setOrdre(int ordre) {
		this.ordre = ordre;
	}

	public String getMetadata() {
		return metadata;
	}

	public void setMetadata(String metadata) {
		this.metadata = metadata;
	}
	
    public String toString() {
        return "Bloc{id=" + id + ", type=" + type + ", contenu='" + contenu + "', dateCreation=" + dateCreation + ", dateModification=" + dateModification + ", ordre=" + ordre + "}";
    }
    
	protected static int ajouterBloc(int idU,  int idP, String contenu, int ordre, Type type, LocalDate dl, String metadata) {
		int idGenere = -1 ;
		try {
			Connection connexion = DriverManager.getConnection(bdURL, bdLogin, bdPassword);
			String sql = " INSERT INTO bloc(type, contenu, date_creation, date_modification, page_id, ordre, auteur_id, metadata)"
					+ " VALUES (?, ?, ?, ?, ?, ?, ?, ?)"
					+ ";";
			PreparedStatement pst = connexion.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
			pst.setString(1, type.name());
			pst.setString(2, contenu);
			pst.setDate(3, Date.valueOf(dl));
			pst.setDate(4, Date.valueOf(dl));
			pst.setInt(5, idP);
			pst.setInt(6, ordre);
			pst.setInt(7, idU);
			pst.setString(8, metadata);
			pst.executeUpdate();
			
			ResultSet rs = pst.getGeneratedKeys();
	        if (rs.next()) {
	            idGenere = rs.getInt(1);
	        }
	        
	        rs.close();
			pst.close();
			connexion.close();
        } catch (SQLException e) {
			e.printStackTrace();
		}	
		
		return idGenere;
	}
	
	protected static void ajouterBlocVide(int idU, int idP) {
	    try {
	        Connection connexion = DriverManager.getConnection(bdURL, bdLogin, bdPassword);
	        String sql = "INSERT INTO bloc(type, page_id, ordre, auteur_id, contenu, metadata)"
	                   + " VALUES (?, ?, ?, ?, '', '{}');";
	        PreparedStatement pst = connexion.prepareStatement(sql);

	        Type type = Type.TEXTE;
	        int ordre = 0;

	        pst.setString(1, type.name());
	        pst.setInt(2, idP);
	        pst.setInt(3, ordre);
	        pst.setInt(4, idU);

	        pst.executeUpdate();
	        pst.close();
	        connexion.close();
	    } catch (SQLException e) {
	        e.printStackTrace();
	    }
	}

	
	public static void updateBloc(int idBloc, String nouveauContenu, String type, String metadata) {
	    try {
	    	Connection connexion = DriverManager.getConnection(bdURL, bdLogin, bdPassword);
    	    String sql = " UPDATE bloc"
    	    		+ " SET contenu = ?"
    	    		+ ", type = ?"
    	    		+ ", metadata = ?"
    	    		+ ", date_modification = ?"
    	    		+ " WHERE id = ?"
    	    		+ ";";
	    	PreparedStatement pst = connexion.prepareStatement(sql);
	    	pst.setString(1, nouveauContenu);
	    	pst.setString(2, type);
	    	pst.setString(3, metadata);
	        pst.setDate(4, Date.valueOf(LocalDate.now()));
	        pst.setInt(5, idBloc);
	        pst.executeUpdate();
	        
	        pst.close();
	        connexion.close();
	    } catch (SQLException e) {
	        e.printStackTrace();
	    }
	}
	
	public static void supprimerBloc(int idBloc, int idU) {
        try {
        	Connection connexion = DriverManager.getConnection(bdURL, bdLogin, bdPassword);
        	String sql = " DELETE FROM bloc"
        			+ " WHERE id = ? and auteur_id = ?"
        			+ ";";
        	PreparedStatement pst = connexion.prepareStatement(sql);

            pst.setInt(1, idBloc);
            pst.setInt(2, idU);
            pst.executeUpdate();
            
        } catch (SQLException e) {
	        e.printStackTrace();
	    }
	}
}
