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.Meeting;

public class MeetingDAO {

public void insert_meeting(String libelle_sprint, String id_moderator){
		

		Connection connection= DBConnexion.getConnection();
		PreparedStatement ps=null;
		
		try {	
		
		String query="insert into meeting(libelle_sprint,id_moderator) values(?,?)";
		 ps=connection.prepareStatement(query);
		 ps.setString(1, libelle_sprint);
		 ps.setString(2, id_moderator);

		 ps.executeUpdate();
		 		 
		 
		} catch (Exception e) {
			System.err.println(e);
		}
	}
	
	
	
	
	public static void main(String[] args) {
		MeetingDAO obj_ReadValues=new MeetingDAO();
		
		obj_ReadValues.get_values();
	}
	
	
	
	public List<Meeting> get_values(){

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

			rs=ps.executeQuery();		
			
			while(rs.next()){
				
				Meeting obj_Meeting=new Meeting();
				System.out.println(rs.getInt("id_meeting"));
				System.out.println(rs.getString("libelle_sprint"));
				System.out.println(rs.getString("date_meeting"));
				System.out.println(rs.getInt("is_done"));

				
				obj_Meeting.setId_meeting(rs.getInt("id_meeting"));
				obj_Meeting.setLibelle_sprint(rs.getString("libelle_sprint"));
				obj_Meeting.setDate_meeting(rs.getString("date_meeting"));
				obj_Meeting.setIs_done(rs.getInt("is_done"));

				list.add(obj_Meeting);				
			}						
		} catch (Exception e) {
				System.out.println(e);
		}
		return list;
	}
	
	public List<Meeting> get_LastMeeting(){

		Connection connection= DBConnexion.getConnection();
		
		PreparedStatement ps=null;
		ResultSet rs=null;
		 List<Meeting> list=new ArrayList<Meeting>();
		
		try {
			String querry="SELECT * FROM meeting ORDER BY id_meeting DESC LIMIT 1";
			ps=connection.prepareStatement(querry);	

			rs=ps.executeQuery();		
			
			while(rs.next()){
				
				Meeting obj_Meeting=new Meeting();
				System.out.println(rs.getInt("id_meeting"));
				System.out.println(rs.getString("libelle_sprint"));
				System.out.println(rs.getString("date_meeting"));
				System.out.println(rs.getInt("is_done"));

				
				obj_Meeting.setId_meeting(rs.getInt("id_meeting"));
				obj_Meeting.setLibelle_sprint(rs.getString("libelle_sprint"));
				obj_Meeting.setDate_meeting(rs.getString("date_meeting"));
				obj_Meeting.setIs_done(rs.getInt("is_done"));

				list.add(obj_Meeting);				
			}						
		} catch (Exception e) {
				System.out.println(e);
		}
		return list;
	}
	
	
	
	
	public List<Meeting> get_MeetingByIDmember(String id_member){

		Connection connection= DBConnexion.getConnection();
		
		PreparedStatement ps=null;
		ResultSet rs=null;
		 List<Meeting> list=new ArrayList<Meeting>();
		
		try {
			String querry="SELECT meeting.id_meeting, libelle_sprint, date_meeting, is_done, id_moderator FROM meeting, affiliate, member WHERE meeting.id_meeting = affiliate.id_meeting AND affiliate.id_member = member.id_member AND member.id_member = ? order by id_meeting desc";
			ps=connection.prepareStatement(querry);	
			 ps.setString(1, id_member);

			rs=ps.executeQuery();		
			
			while(rs.next()){
				
				Meeting obj_Meeting=new Meeting();
				System.out.println(rs.getInt("id_meeting"));
				System.out.println(rs.getString("libelle_sprint"));
				System.out.println(rs.getString("date_meeting"));
				System.out.println(rs.getInt("is_done"));

				
				obj_Meeting.setId_meeting(rs.getInt("id_meeting"));
				obj_Meeting.setLibelle_sprint(rs.getString("libelle_sprint"));
				obj_Meeting.setDate_meeting(rs.getString("date_meeting"));
				obj_Meeting.setIs_done(rs.getInt("is_done"));

				list.add(obj_Meeting);				
			}						
		} catch (Exception e) {
				System.out.println(e);
		}
		return list;
	}
	
	public List<Meeting> get_MeetingMAXByIDmember(String id_member){

		Connection connection= DBConnexion.getConnection();
		
		PreparedStatement ps=null;
		ResultSet rs=null;
		 List<Meeting> list=new ArrayList<Meeting>();
		
		try {
			String querry="SELECT meeting.id_meeting, libelle_sprint, date_meeting, is_done, id_moderator FROM meeting, affiliate, member WHERE meeting.id_meeting = affiliate.id_meeting AND affiliate.id_member = member.id_member AND member.id_member = ? order by id_meeting desc limit 1";
			ps=connection.prepareStatement(querry);	
			 ps.setString(1, id_member);

			rs=ps.executeQuery();		
			
			while(rs.next()){
				
				Meeting obj_Meeting=new Meeting();
				System.out.println(rs.getInt("id_meeting"));
				System.out.println(rs.getString("libelle_sprint"));
				System.out.println(rs.getString("date_meeting"));
				System.out.println(rs.getInt("is_done"));

				
				obj_Meeting.setId_meeting(rs.getInt("id_meeting"));
				obj_Meeting.setLibelle_sprint(rs.getString("libelle_sprint"));
				obj_Meeting.setDate_meeting(rs.getString("date_meeting"));
				obj_Meeting.setIs_done(rs.getInt("is_done"));

				list.add(obj_Meeting);				
			}						
		} catch (Exception e) {
				System.out.println(e);
		}
		return list;
	}
	
	
	
	
public void update_to_validate(String id_meeting){
		
		Connection connection= DBConnexion.getConnection();
		PreparedStatement ps=null;
		
		try {	
		
		String query="update meeting set is_done = 1 where id_meeting = ?";
		 ps=connection.prepareStatement(query);
		 ps.setString(1, id_meeting);

		 ps.executeUpdate();
		 		 
		 
		} catch (Exception e) {
			System.err.println(e);
		}
	}
	
public void update_to_unvalidate(String id_meeting){
	
	Connection connection= DBConnexion.getConnection();
	PreparedStatement ps=null;
	
	try {	
	
	String query="update meeting set is_done = 0 where id_meeting = ?";
	 ps=connection.prepareStatement(query);
	 ps.setString(1, id_meeting);

	 ps.executeUpdate();
	 		 
	 
	} catch (Exception e) {
		System.err.println(e);
	}
}
	
public List<Meeting> get_MeetingByIDmeeting(String id_meeting){

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

		rs=ps.executeQuery();		
		
		while(rs.next()){
			
			Meeting obj_Meeting=new Meeting();
			System.out.println(rs.getInt("id_meeting"));
			System.out.println(rs.getString("libelle_sprint"));
			System.out.println(rs.getString("date_meeting"));
			System.out.println(rs.getInt("is_done"));

			
			obj_Meeting.setId_meeting(rs.getInt("id_meeting"));
			obj_Meeting.setLibelle_sprint(rs.getString("libelle_sprint"));
			obj_Meeting.setDate_meeting(rs.getString("date_meeting"));
			obj_Meeting.setIs_done(rs.getInt("is_done"));

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

}
