import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.swing.AbstractButton;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;

public class Questionnaire {
	private String nom;
	private Date dateCreation;
	private boolean estComplet;
	
	List<Question> questionTab; //tableau où on stocke toutes les questions
	List<JButton> tabJButtonRep;
	
	
// --------- CONSTRUCTEUR ------------- //
	
	public Questionnaire(String name, int nbQuestion, Date Creation, boolean estComplet, List<Question> questionTab, List<JButton> tabJButtonRep){
		this.nom = name;
		this.dateCreation = Creation;
		this.estComplet = estComplet;
		this.questionTab = questionTab;
		this.tabJButtonRep = tabJButtonRep;
	}
	

	public Questionnaire(){
		this.nom = "Questionnaire";
	// this.dateCreation = ;
		this.estComplet = false;
		this.questionTab =  new ArrayList<Question>();
		this.tabJButtonRep = new ArrayList<JButton>();
	}
	
//---------- GETTERS & SETTERS ---------- //	

	public String getNom() {
		return nom;
	}
	
	public void setNom(String nom) {
		this.nom = nom;
	}
	
	public Date getDateCreation() {
		return dateCreation;
	}
	
	public void setDateCreation(Date dateCreation) {
		this.dateCreation = dateCreation;
	}
	
	public boolean isEstComplet() {
		return estComplet;
	}
	
	public void setEstComplet(boolean estComplet) {
		this.estComplet = estComplet;
	}
	
	public List<Question> getQuestionTab() {
		return questionTab;
	}

	public void setQuestionTab(List<Question> questionTab) {
		this.questionTab = questionTab;
	}
	

	public List<JButton> getTabJButtonRep() {
	return tabJButtonRep;
}


	public void setTabJPanelRep(List<JButton> tabJButtonRep) {
		this.tabJButtonRep = tabJButtonRep;
}

	

	//-----------------------------------------//
	
	public String demanderTitre(){
		
		String title = JOptionPane.showInputDialog(null, "Entrez le nom de votre questionnaire", "Titre", JOptionPane.QUESTION_MESSAGE);
		this.setNom(title);
		return title;
		
	}
	
//Boucle pour afficher les questions 
		
	public void affichageQuestions(Fenetre fenetre, JPanel pan){  // /!\/!\/!\
				
		
		for (int i=0; i< this.getQuestionTab().size(); i++){ 
			
			
			//Panel contenant les questions + le panel de réponses
			JPanel panel_QR = new JPanel();
			panel_QR.setBorder(BorderFactory.createTitledBorder("Q"+i));
			
			
			//Panel contenant les réponses
			JPanel panel_R = new JPanel();
		    panel_R.setLayout(new BoxLayout(panel_R, BoxLayout.Y_AXIS));
			
		    
		    
			Question question_i = this.getQuestionTab().get(i);
			JButton button_rep_i = this.getTabJButtonRep().get(i);
			
			JLabel afficherQuestion = new JLabel(question_i.getDescription()); //Intitulé de la question i
			panel_QR.add(afficherQuestion); 
			
			
			if ((question_i.getType() == 1) || (question_i.getType() == 2)){ //QCM ou Choix unique
				
				
				question_i.afficherReponse(fenetre, panel_QR, panel_R); //On affiche les réponses
				panel_QR.add(button_rep_i); //Un seul bouton AddReponse par question
				button_rep_i.addActionListener(new ButtonReponseListener(question_i, fenetre, panel_QR, panel_R));
				
			}
			
			else if (question_i.getType() == 3) { 
				panel_QR.add(this.getQuestionTab().get(i).reponseNumerique());
			}
			
			else if (question_i.getType() == 4){ 
				panel_QR.add(this.getQuestionTab().get(i).texte());
			}
			
			
			panel_QR.setLayout(new BoxLayout(panel_QR, BoxLayout.Y_AXIS));
		
			pan.add(panel_QR);
			pan.add(Box.createVerticalStrut(20)); //distance de 20 entre chaque sous composantes
			
			
		}
		
		fenetre.setPan(pan);
		fenetre.setVisible(true);
	}
	
	
	//Action quand on clique sur le bouton AddReponse pour les réponses fermées (type 1 ou 2)
	//Permet de créer des réponses que l'on stocke dans le tableau de réponses
	
	

		private class ButtonReponseListener implements ActionListener { // /!\/!\/!\
		
		private Question question;
		private Fenetre fenetre;
		private JPanel panel_QR;
		private JPanel panel_R;
		
        public ButtonReponseListener(Question question, Fenetre fenetre, JPanel panel_QR, JPanel panel_R) {
        	this.question = question;
        	this.fenetre = fenetre;
        	this.panel_QR = panel_QR;
        	this.panel_R = panel_R;
        }
        
        @Override
        public void actionPerformed(ActionEvent e){
        	Reponse answer = new Reponse(); //On crée une réponse
    
	        answer.creerReponse(question); // on ajoute dans le tableau de réponse
	        
	        panel_R.removeAll();
			
	       	question.afficherReponse(fenetre, panel_QR, panel_R);  		
        
        }
	}

}
