package fonctions;

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

import retro.DBConnexion;
import retro.Idea;

public class IdeaDAO {

public void insert_idea(String libelle_idea, String description_idea, String libelle_type_idea, String id_meeting, String id_member){
		

		Connection connection= DBConnexion.getConnection();
		PreparedStatement ps=null;
		
		try {	
		
		String query="insert into idea(libelle_idea, description_idea, libelle_type_idea, id_meeting, id_member) values(?,?,?,?,?)";
		 ps=connection.prepareStatement(query);
		 ps.setString(1, libelle_idea);
		 ps.setString(2, description_idea);
		 ps.setString(3, libelle_type_idea);
		 ps.setString(4, id_meeting);
		 ps.setString(5, id_member);
		 ps.executeUpdate();
		 		 
		 
		} catch (Exception e) {
			System.err.println(e);
		}
	}




public static void main(String[] args) {
	IdeaDAO obj_ReadValues=new IdeaDAO();
	
	obj_ReadValues.get_values();
}



public List<Idea> get_values(){

	Connection connection= DBConnexion.getConnection();
	
	PreparedStatement ps=null;
	ResultSet rs=null;
	 List<Idea> list=new ArrayList<Idea>();
	
	try {
		String querry="SELECT * FROM idea";
		ps=connection.prepareStatement(querry);	

		rs=ps.executeQuery();		
		
		while(rs.next()){
			
			Idea obj_Idea=new Idea();
			System.out.println(rs.getInt("id_idea"));
			System.out.println(rs.getString("libelle_idea"));
			System.out.println(rs.getString("description_idea"));
			System.out.println(rs.getString("libelle_type_idea"));
			System.out.println(rs.getString("id_meeting"));
			System.out.println(rs.getString("id_member"));

			
			obj_Idea.setId_idea(rs.getInt("id_idea"));
			obj_Idea.setLibelle_idea(rs.getString("libelle_idea"));
			obj_Idea.setDescription_idea(rs.getString("description_idea"));
			obj_Idea.setLibelle_type_idea(rs.getString("libelle_type_idea"));
			obj_Idea.setId_meeting(rs.getString("id_meeting"));
			obj_Idea.setId_member(rs.getString("id_member"));

			list.add(obj_Idea);				
		}						
	} catch (Exception e) {
			System.out.println(e);
	}
	return list;
}




public List<Idea> get_IdeaByIDmember(String id_member, String id_meeting){

	Connection connection= DBConnexion.getConnection();
	
	PreparedStatement ps=null;
	ResultSet rs=null;
	 List<Idea> list=new ArrayList<Idea>();
	
	try {
		String querry="SELECT * from idea where id_member = ? and id_meeting = ?";
		ps=connection.prepareStatement(querry);	
		 ps.setString(1, id_member);
		 ps.setString(2, id_meeting);

		rs=ps.executeQuery();		
		
		while(rs.next()){
			
			Idea obj_Idea=new Idea();
			System.out.println(rs.getInt("id_idea"));
			System.out.println(rs.getString("libelle_idea"));
			System.out.println(rs.getString("description_idea"));
			System.out.println(rs.getString("libelle_type_idea"));
			System.out.println(rs.getString("id_meeting"));
			System.out.println(rs.getString("id_member"));

			
			obj_Idea.setId_idea(rs.getInt("id_idea"));
			obj_Idea.setLibelle_idea(rs.getString("libelle_idea"));
			obj_Idea.setDescription_idea(rs.getString("description_idea"));
			obj_Idea.setLibelle_type_idea(rs.getString("libelle_type_idea"));
			obj_Idea.setId_meeting(rs.getString("id_meeting"));
			obj_Idea.setId_member(rs.getString("id_member"));

			list.add(obj_Idea);								
		}						
	} catch (Exception e) {
			System.out.println(e);
	}
	return list;
}




public void delete_idea(String id_idea){

	Connection connection= DBConnexion.getConnection();
	
	PreparedStatement ps=null;
 
	 
	 
	try {
		String querry="delete from idea where id_idea=?";
		ps=connection.prepareStatement(querry);		
		ps.setString(1, id_idea);;
		ps.executeUpdate();
		
		
		 
		
		
	} catch (Exception e) {
			System.out.println(e);
	}
	 	
}


public List<Idea> get_IdeaLikedByIDmeeting(String id_meeting){

	Connection connection= DBConnexion.getConnection();
	
	PreparedStatement ps=null;
	ResultSet rs=null;
	 List<Idea> list=new ArrayList<Idea>();
	
	try {
		String querry="SELECT * from idea where libelle_type_idea = 'Liked' and id_meeting = ?";
		ps=connection.prepareStatement(querry);	
		 ps.setString(1, id_meeting);

		rs=ps.executeQuery();		
		
		while(rs.next()){
			
			Idea obj_Idea=new Idea();
			System.out.println(rs.getInt("id_idea"));
			System.out.println(rs.getString("libelle_idea"));
			System.out.println(rs.getString("description_idea"));
			System.out.println(rs.getString("libelle_type_idea"));
			System.out.println(rs.getString("id_meeting"));
			System.out.println(rs.getString("id_member"));
			System.out.println(rs.getInt("nb_vote"));

			
			obj_Idea.setId_idea(rs.getInt("id_idea"));
			obj_Idea.setLibelle_idea(rs.getString("libelle_idea"));
			obj_Idea.setDescription_idea(rs.getString("description_idea"));
			obj_Idea.setLibelle_type_idea(rs.getString("libelle_type_idea"));
			obj_Idea.setId_meeting(rs.getString("id_meeting"));
			obj_Idea.setId_member(rs.getString("id_member"));
			obj_Idea.setNb_vote(rs.getInt("nb_vote"));

			list.add(obj_Idea);								
		}						
	} catch (Exception e) {
			System.out.println(e);
	}
	return list;
}



public List<Idea> get_IdeaLearnedByIDmeeting(String id_meeting){

	Connection connection= DBConnexion.getConnection();
	
	PreparedStatement ps=null;
	ResultSet rs=null;
	 List<Idea> list=new ArrayList<Idea>();
	
	try {
		String querry="SELECT * from idea where libelle_type_idea = 'Learned' and id_meeting = ?";
		ps=connection.prepareStatement(querry);	
		 ps.setString(1, id_meeting);

		rs=ps.executeQuery();		
		
		while(rs.next()){
			
			Idea obj_Idea=new Idea();
			System.out.println(rs.getInt("id_idea"));
			System.out.println(rs.getString("libelle_idea"));
			System.out.println(rs.getString("description_idea"));
			System.out.println(rs.getString("libelle_type_idea"));
			System.out.println(rs.getString("id_meeting"));
			System.out.println(rs.getString("id_member"));
			System.out.println(rs.getInt("nb_vote"));


			
			obj_Idea.setId_idea(rs.getInt("id_idea"));
			obj_Idea.setLibelle_idea(rs.getString("libelle_idea"));
			obj_Idea.setDescription_idea(rs.getString("description_idea"));
			obj_Idea.setLibelle_type_idea(rs.getString("libelle_type_idea"));
			obj_Idea.setId_meeting(rs.getString("id_meeting"));
			obj_Idea.setId_member(rs.getString("id_member"));
			obj_Idea.setNb_vote(rs.getInt("nb_vote"));

			list.add(obj_Idea);								
		}						
	} catch (Exception e) {
			System.out.println(e);
	}
	return list;
}



public List<Idea> get_IdeaLongedForByIDmeeting(String id_meeting){

	Connection connection= DBConnexion.getConnection();
	
	PreparedStatement ps=null;
	ResultSet rs=null;
	 List<Idea> list=new ArrayList<Idea>();
	
	try {
		String querry="SELECT * from idea where libelle_type_idea = 'Longed_For' and id_meeting = ?";
		ps=connection.prepareStatement(querry);	
		 ps.setString(1, id_meeting);

		rs=ps.executeQuery();		
		
		while(rs.next()){
			
			Idea obj_Idea=new Idea();
			System.out.println(rs.getInt("id_idea"));
			System.out.println(rs.getString("libelle_idea"));
			System.out.println(rs.getString("description_idea"));
			System.out.println(rs.getString("libelle_type_idea"));
			System.out.println(rs.getString("id_meeting"));
			System.out.println(rs.getString("id_member"));
			System.out.println(rs.getInt("nb_vote"));


			
			obj_Idea.setId_idea(rs.getInt("id_idea"));
			obj_Idea.setLibelle_idea(rs.getString("libelle_idea"));
			obj_Idea.setDescription_idea(rs.getString("description_idea"));
			obj_Idea.setLibelle_type_idea(rs.getString("libelle_type_idea"));
			obj_Idea.setId_meeting(rs.getString("id_meeting"));
			obj_Idea.setId_member(rs.getString("id_member"));
			obj_Idea.setNb_vote(rs.getInt("nb_vote"));

			list.add(obj_Idea);								
		}						
	} catch (Exception e) {
			System.out.println(e);
	}
	return list;
}


public List<Idea> get_IdeaLackedByIDmeeting(String id_meeting){

	Connection connection= DBConnexion.getConnection();
	
	PreparedStatement ps=null;
	ResultSet rs=null;
	 List<Idea> list=new ArrayList<Idea>();
	
	try {
		String querry="SELECT * from idea where libelle_type_idea = 'Lacked' and id_meeting = ?";
		ps=connection.prepareStatement(querry);	
		 ps.setString(1, id_meeting);

		rs=ps.executeQuery();		
		
		while(rs.next()){
			
			Idea obj_Idea=new Idea();
			System.out.println(rs.getInt("id_idea"));
			System.out.println(rs.getString("libelle_idea"));
			System.out.println(rs.getString("description_idea"));
			System.out.println(rs.getString("libelle_type_idea"));
			System.out.println(rs.getString("id_meeting"));
			System.out.println(rs.getString("id_member"));
			System.out.println(rs.getInt("nb_vote"));

			
			obj_Idea.setId_idea(rs.getInt("id_idea"));
			obj_Idea.setLibelle_idea(rs.getString("libelle_idea"));
			obj_Idea.setDescription_idea(rs.getString("description_idea"));
			obj_Idea.setLibelle_type_idea(rs.getString("libelle_type_idea"));
			obj_Idea.setId_meeting(rs.getString("id_meeting"));
			obj_Idea.setId_member(rs.getString("id_member"));
			obj_Idea.setNb_vote(rs.getInt("nb_vote"));

			list.add(obj_Idea);								
		}						
	} catch (Exception e) {
			System.out.println(e);
	}
	return list;
}

public List<Idea> get_IdeaByIDmeeting(String id_meeting){

	Connection connection= DBConnexion.getConnection();
	
	PreparedStatement ps=null;
	ResultSet rs=null;
	 List<Idea> list=new ArrayList<Idea>();
	
	try {
		String querry="SELECT * from idea where id_meeting = ?";
		ps=connection.prepareStatement(querry);	
		 ps.setString(1, id_meeting);

		rs=ps.executeQuery();		
		
		while(rs.next()){
			
			Idea obj_Idea=new Idea();
			System.out.println(rs.getInt("id_idea"));
			System.out.println(rs.getString("libelle_idea"));
			System.out.println(rs.getString("description_idea"));
			System.out.println(rs.getString("libelle_type_idea"));
			System.out.println(rs.getString("id_meeting"));
			System.out.println(rs.getString("id_member"));

			
			obj_Idea.setId_idea(rs.getInt("id_idea"));
			obj_Idea.setLibelle_idea(rs.getString("libelle_idea"));
			obj_Idea.setDescription_idea(rs.getString("description_idea"));
			obj_Idea.setLibelle_type_idea(rs.getString("libelle_type_idea"));
			obj_Idea.setId_meeting(rs.getString("id_meeting"));
			obj_Idea.setId_member(rs.getString("id_member"));

			list.add(obj_Idea);								
		}						
	} catch (Exception e) {
			System.out.println(e);
	}
	return list;
}





public void update_to_vote(String id_idea){
	
	Connection connection= DBConnexion.getConnection();
	PreparedStatement ps=null;
	
	try {	
	
	String query="UPDATE idea SET nb_vote= nb_vote + 1 WHERE id_idea = ?";
	 ps=connection.prepareStatement(query);
	 ps.setString(1, id_idea);

	 ps.executeUpdate();
	 		 
	 
	} catch (Exception e) {
		System.err.println(e);
	}
}

public void update_to_unvote(String id_idea){
	
	Connection connection= DBConnexion.getConnection();
	PreparedStatement ps=null;
	
	try {	
	
	String query="UPDATE idea SET nb_vote= nb_vote - 1 WHERE id_idea = ?";
	 ps=connection.prepareStatement(query);
	 ps.setString(1, id_idea);

	 ps.executeUpdate();
	 		 
	 
	} catch (Exception e) {
		System.err.println(e);
	}
}



public List<Idea> get_Idea5MaxVote(String id_meeting){

	Connection connection= DBConnexion.getConnection();
	
	PreparedStatement ps=null;
	ResultSet rs=null;
	 List<Idea> list=new ArrayList<Idea>();
	
	try {
		String querry="SELECT * from idea where id_meeting = ? order by nb_vote desc limit 5";
		ps=connection.prepareStatement(querry);	
		 ps.setString(1, id_meeting);

		rs=ps.executeQuery();		
		
		while(rs.next()){
			
			Idea obj_Idea=new Idea();
			System.out.println(rs.getInt("id_idea"));
			System.out.println(rs.getString("libelle_idea"));
			System.out.println(rs.getString("description_idea"));
			System.out.println(rs.getString("libelle_type_idea"));
			System.out.println(rs.getString("id_meeting"));
			System.out.println(rs.getString("id_member"));
			System.out.println(rs.getInt("nb_vote"));


			
			obj_Idea.setId_idea(rs.getInt("id_idea"));
			obj_Idea.setLibelle_idea(rs.getString("libelle_idea"));
			obj_Idea.setDescription_idea(rs.getString("description_idea"));
			obj_Idea.setLibelle_type_idea(rs.getString("libelle_type_idea"));
			obj_Idea.setId_meeting(rs.getString("id_meeting"));
			obj_Idea.setId_member(rs.getString("id_member"));
			obj_Idea.setNb_vote(rs.getInt("nb_vote"));

			list.add(obj_Idea);								
		}						
	} catch (Exception e) {
			System.out.println(e);
	}
	return list;
}






















}
