import java.awt.Dimension;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BoxLayout;
import javax.swing.JCheckBox;
import javax.swing.JFormattedTextField;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;


public class Question {
	private String description;
	private int type;

	List<String> repTab; //tableau où on stocke toutes les réponses
	
	public Question(String description, int type, List<String> rep){
		this.description = description;
		this.type = type;
		this.repTab = rep;
	}

	public Question(){
		this.description= JOptionPane.showInputDialog(null, "Posez votre question", "Question", JOptionPane.QUESTION_MESSAGE);
		this.type = 4; //rep txt par défaut
		this.repTab = new ArrayList<String>(); //tableau vide, sans réponses
	}
	


	
	//-------------------------------------------

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public List<String> getRepTab() { //get tableau contenant toutes les réponses
		return this.repTab;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}


	
	//--------------- CHOISIR TYPE DE REPONSE DE LA QUESTION ------------------
	
		// Demander quel type de réponses (1. ouverte (texte), 2. checkbox, 3. radio (1choix), 4. numérique)
		
		public int typeReponse(String[] choices){
			int choix;

			 do{
			    String s = (String) JOptionPane.showInputDialog(
					null,
					"Choisissez un type de réponse :",
					"Type de réponse",
					JOptionPane.PLAIN_MESSAGE,
					null,
					choices,
					choices[0]);
					
			    
				if (s == "Question à réponses multiples"){
					choix = 1;
					this.setType(1);
					
					
				} else if (s == "Question à réponse unique") {
					choix = 2;
					this.setType(2);
				
				} else if (s == "Question à réponse numérique"){
					choix = 3;
					this.setType(3);
					
				} else if (s == "Question à réponse texte"){
					choix = 4;
					this.setType(4); //Réponse texte par défaut
				} 
				else {
					choix = 0; // on ne fait rien si on appuie sur autre chose
					this.setType(0);
				};
				
				System.out.println("Vous avez choisi la réponse"+ choix);
				return choix;
		    }
		    while ((choix != 1) || (choix != 2) || (choix != 3) || (choix != 4) || (choix != 0));
			
		}
		
	
	//---------------- AFFICHAGE REPONSES SELON LE TYPE DE LA QUESTION -------------------
	
	public void afficherReponse(Fenetre fenetre, JPanel panel_QR, JPanel panel_R){
		JRadioButton[] tabRadioButton = new JRadioButton[this.getRepTab().size()]; //Tableau de RadioButton
		JCheckBox[] tabCheckBox = new JCheckBox[this.getRepTab().size()]; //Tableau de CheckBox
		
		
		
		switch(this.getType())
		{
			case 1 : //JCheckBox - QCM
				
				for (int i=0; i< this.getRepTab().size(); i++){ //on parcoure le tableau de réponses
					tabCheckBox[i] = new JCheckBox(this.getRepTab().get(i)); 
					panel_R.add(tabCheckBox[i]);
					
		}
				break;
				
			case 2 : //RadioButton - Question à choix unique
				
				for (int i=0; i< this.getRepTab().size(); i++){ //on parcoure le tableau de réponses
					
					tabRadioButton[i] = new JRadioButton(this.getRepTab().get(i)); 
					panel_R.add(tabRadioButton[i]);	
					
				}
				break;
		}
		
		//panel_R.setLayout(new BoxLayout(panel_R, BoxLayout.Y_AXIS));
		
		panel_QR.add(panel_R);
		fenetre.setPan(panel_QR);
		fenetre.setVisible(true);
		
	}
	
	

		
	public JFormattedTextField reponseNumerique(){
			
		JFormattedTextField numerique = new JFormattedTextField(NumberFormat.getIntegerInstance());
		numerique.setPreferredSize(new Dimension(100,25));
			
		return numerique; //il faut récupérer l'input de l'utilisateur (= sa réponse)
		
	}
	
	
	public JTextField texte(){
		
		JTextField txt = new JTextField(30); 
		
		return txt;  //il faut récupérer l'input de l'utilisateur (= sa réponse)

	}
	
	
	
}
