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

public class MemberDAO {


	public void insert_member(String nom, String prenom, String pseudo, String mdp, String libelle_office){


		Connection connection=DBConnexion.getConnection();
		PreparedStatement ps=null;

		try {

		String query="insert into member(nom_member,prenom_member, pseudo_member, mdp_member, libelle_office) values(?,?,?,?,?)";
		 ps=connection.prepareStatement(query);
		 ps.setString(1, nom);
		 ps.setString(2, prenom);
		 ps.setString(3, pseudo);
		 ps.setString(4, mdp);
		 ps.setString(5, libelle_office);

		 ps.executeUpdate();


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




	public static void main(String[] args) {
		MemberDAO obj_ReadValues=new MemberDAO();

		obj_ReadValues.get_values();
	}



	public List<Member> get_values(){

		Connection connection= DBConnexion.getConnection();

		PreparedStatement ps=null;
		ResultSet rs=null;
		 List<Member> list=new ArrayList<Member>();

		try {
			String querry="SELECT * FROM member";
			ps=connection.prepareStatement(querry);

			rs=ps.executeQuery();

			while(rs.next()){

				Member obj_Member=new Member();
				System.out.println(rs.getInt("id_member"));
				System.out.println(rs.getString("nom_member"));
				System.out.println(rs.getString("prenom_member"));
				System.out.println(rs.getString("libelle_office"));


				obj_Member.setId_member(rs.getInt("id_member"));
				obj_Member.setNom_member(rs.getString("nom_member"));
				obj_Member.setPrenom_member(rs.getString("prenom_member"));
				obj_Member.setLibelle_office(rs.getString("libelle_office"));

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


	public List<Member> get_MemberInMeeting(String id_meeting){

		Connection connection= DBConnexion.getConnection();

		PreparedStatement ps=null;
		ResultSet rs=null;
		 List<Member> list=new ArrayList<Member>();

		try {
			String querry="SELECT member.id_member, nom_member, prenom_member FROM member, meeting, affiliate WHERE meeting.id_meeting = affiliate.id_meeting and affiliate.id_member = member.id_member and meeting.id_meeting = ? ";
			ps=connection.prepareStatement(querry);
			 ps.setString(1, id_meeting);

			rs=ps.executeQuery();

			while(rs.next()){

				Member obj_Member=new Member();
				System.out.println(rs.getInt("id_member"));
				System.out.println(rs.getString("nom_member"));
				System.out.println(rs.getString("prenom_member"));


				obj_Member.setId_member(rs.getInt("id_member"));
				obj_Member.setNom_member(rs.getString("nom_member"));
				obj_Member.setPrenom_member(rs.getString("prenom_member"));

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




	public List<Member> get_MemberNOTInMeeting(String id_meeting){

		Connection connection= DBConnexion.getConnection();

		PreparedStatement ps=null;
		ResultSet rs=null;
		 List<Member> list=new ArrayList<Member>();

		try {
			String querry="SELECT member.id_member, nom_member, prenom_member FROM member where id_member not in (select id_member from affiliate where id_meeting = ? )";
			ps=connection.prepareStatement(querry);
			 ps.setString(1, id_meeting);

			rs=ps.executeQuery();

			while(rs.next()){

				Member obj_Member=new Member();
				System.out.println(rs.getInt("id_member"));
				System.out.println(rs.getString("nom_member"));
				System.out.println(rs.getString("prenom_member"));


				obj_Member.setId_member(rs.getInt("id_member"));
				obj_Member.setNom_member(rs.getString("nom_member"));
				obj_Member.setPrenom_member(rs.getString("prenom_member"));

				list.add(obj_Member);
			}
		} catch (Exception e) {
				System.out.println(e);
		}
		return list;
	}
	
	
	
	public List<Member> get_MemberByID(String id_member){

		Connection connection= DBConnexion.getConnection();

		PreparedStatement ps=null;
		ResultSet rs=null;
		 List<Member> list=new ArrayList<Member>();

		try {
			String querry="SELECT * FROM member where id_member = ?";
			ps=connection.prepareStatement(querry);
			 ps.setString(1, id_member);

			rs=ps.executeQuery();

			while(rs.next()){

				Member obj_Member=new Member();
				System.out.println(rs.getInt("id_member"));
				System.out.println(rs.getString("nom_member"));
				System.out.println(rs.getString("prenom_member"));


				obj_Member.setId_member(rs.getInt("id_member"));
				obj_Member.setNom_member(rs.getString("nom_member"));
				obj_Member.setPrenom_member(rs.getString("prenom_member"));
				obj_Member.setPseudo(rs.getString("pseudo_member"));
				obj_Member.setMdp(rs.getString("mdp_member"));
				obj_Member.setLibelle_office(rs.getString("libelle_office"));

				

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

	public List<Member> memberConnexion(String pseudo_member, String mdp_member){

		Connection connection= DBConnexion.getConnection();

		PreparedStatement ps=null;
		ResultSet rs=null;
		 List<Member> list=new ArrayList<Member>();

		try {
			String querry="SELECT * FROM member WHERE pseudo_member = ? AND mdp_member = ?";
			ps=connection.prepareStatement(querry);
			 ps.setString(1, pseudo_member);
			 ps.setString(2, mdp_member);

			rs=ps.executeQuery();

			while(rs.next()){

				Member obj_Member=new Member();
				System.out.println(rs.getInt("id_member"));
				System.out.println(rs.getString("nom_member"));
				System.out.println(rs.getString("prenom_member"));
				System.out.println(rs.getString("pseudo_member"));
				System.out.println(rs.getString("mdp_member"));
				System.out.println(rs.getString("libelle_office"));


				obj_Member.setId_member(rs.getInt("id_member"));
				obj_Member.setNom_member(rs.getString("nom_member"));
				obj_Member.setPrenom_member(rs.getString("prenom_member"));
				obj_Member.setPseudo(rs.getString("pseudo_member"));
				obj_Member.setMdp(rs.getString("mdp_member"));
				obj_Member.setLibelle_office(rs.getString("libelle_office"));

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

}
