package com.controller;

import java.sql.*;

public class DataBase {
	
	//Connexion serveur GEI
	//login : tp_servlet_018
	//password : Taey2Aje
	//Plus d'infs connexion sur moodle
	
	private static DataBase instance = null;
	
	public Connection connexion;
	
	private DataBase() {
		try {
			Class.forName("com.mysql.jdbc.Driver");
		}catch (ClassNotFoundException e){
			e.printStackTrace();
		}
		try {
			connexion = DriverManager.getConnection("jdbc:mysql://srv-bdens.insa-toulouse.fr:3306/tp_servlet_018?useSSL=false", "tp_servlet_018", "Taey2Aje");
			//connexion = DriverManager.getConnection("jdbc:mysql://localhost/test?characterEncoding=utf8", "root","root");
		}catch(SQLException e) {
			e.printStackTrace();
		}
	}
	
	public static DataBase getInstance() {
		synchronized (DataBase.class) {
			DataBase db = instance;
			if(db == null) {
				db = new DataBase();
			}
			return db;
		}
	}
	
	//retourne l'id associé au user ajouté
	public int addUser(String pseudo, String login) {
		String query = "INSERT INTO users (pseudo, statut, login) VALUES (?, ?, ?)";
		PreparedStatement pStat = null;
		try {
			pStat = connexion.prepareStatement(query);
			pStat.setString(1, pseudo);
			pStat.setString(2, "En ligne");
			pStat.setString(3, login);
			pStat.executeUpdate();
		}catch (SQLException e) {
			e.printStackTrace();
		}
		query = "SELECT * FROM users WHERE pseudo=?";
		int id =0;
		try {
			pStat = connexion.prepareStatement(query);
			pStat.setString(1, pseudo);
			ResultSet resId = pStat.executeQuery();
			if(resId.first()) {
				id = resId.getInt("id");
			}
		}catch(SQLException e) {
			e.printStackTrace();
		}
		return id;
	}

	public String getPseudoFromId(int id) {
		String pseudo = null;
		String query = "SELECT * FROM users WHERE id=?";
		PreparedStatement pStat = null;
		try {
			pStat = connexion.prepareStatement(query);
			pStat.setInt(1, id);
			ResultSet resPseudo = pStat.executeQuery();
			if(resPseudo.first()) {
				pseudo = resPseudo.getString("pseudo");
			}
		}catch(SQLException e) {
			e.printStackTrace();
		}
		return pseudo;
	}
	
	public int getIdFromPseudo(String pseudo) {
		int id = -1;
		String query = "SELECT * FROM users WHERE pseudo='" + pseudo + "' AND statut = 'En ligne'";
		PreparedStatement pStat = null;
		try {
			pStat = connexion.prepareStatement(query);
			//pStat.setString(1, pseudo);
			ResultSet resId = pStat.executeQuery();
			if(resId.next()) {
				id = resId.getInt(1);
			}
		}catch(SQLException e) {
			e.printStackTrace();
		}
		return id;
	}
	
	public boolean loginExiste(String login) {
        String loginAux = null;
        String query = "SELECT * FROM users WHERE login =?";
        PreparedStatement pStat = null;
        try {
			pStat = connexion.prepareStatement(query);
		    pStat.setString(1, login);
	        ResultSet rs = pStat.executeQuery();
	        if (rs.first()) {
	            loginAux = rs.getString("login");
	        }
		} catch (SQLException e) {
			e.printStackTrace();
		}
        //System.out.println("loginAux = "+loginAux);
        return !(loginAux==null);
	}
	
	public String getPseudoFromLogin(String login) {
		String pseudo = null;
		String query = "SELECT * FROM users WHERE login=?";
		PreparedStatement pStat = null;
		try {
			pStat = connexion.prepareStatement(query);
			pStat.setString(1, login);
			ResultSet resPseudo = pStat.executeQuery();
			if(resPseudo.first()) {
				pseudo = resPseudo.getString("pseudo");
			}
		}catch(SQLException e) {
			e.printStackTrace();
		}
		return pseudo;
	}
	
	public int getIdFromLogin(String login) {
		int id = -1;
		String query = "SELECT * FROM users WHERE login=?";
		PreparedStatement pStat = null;
		try {
			pStat = connexion.prepareStatement(query);
			pStat.setString(1, login);
			ResultSet resPseudo = pStat.executeQuery();
			if(resPseudo.first()) {
				id = resPseudo.getInt("id");
			}
		}catch(SQLException e) {
			e.printStackTrace();
		}
		return id;
	}
	
	public void deleteFromId(int id) {
		String query = "DELETE FROM `users` WHERE `id` = ?";
		PreparedStatement pStat = null;
		try {
			pStat = connexion.prepareStatement(query);
			pStat.setInt(1, id);
			pStat.executeUpdate();
		}catch(SQLException e) {
			e.printStackTrace();
		}
		
	}
	
	public void updatePseudo(int id, String pseudo) {
		String query = "UPDATE `users` SET `pseudo`=? WHERE id=?";
		PreparedStatement pStat = null;
		try {
			pStat = connexion.prepareStatement(query);
			pStat.setString(1, pseudo);
			pStat.setInt(2, id);
			pStat.executeUpdate();
		}catch(SQLException e) {
			e.printStackTrace();
		}
	}
	
	public void updateStatus(int id, String status) {
		String query = "UPDATE `users` SET `statut`=? WHERE id=?";
		PreparedStatement pStat = null;
		try {
			pStat = connexion.prepareStatement(query);
			pStat.setString(1, status);
			pStat.setInt(2, id);
			pStat.executeUpdate();
		}catch(SQLException e) {
			e.printStackTrace();
		}
	}
	
	public String getStatus(String login) {
		String query = "SELECT * FROM `users` WHERE login=?"; 
		PreparedStatement pStat = null;
		String status = null;
		try {
			pStat = connexion.prepareStatement(query);
			pStat.setString(1, login);
			ResultSet resPseudo = pStat.executeQuery();
			if(resPseudo.first()) {
				status = resPseudo.getString("statut");
			}
		}catch(SQLException e) {
			e.printStackTrace();
		}
		return status;
	}
	
}