﻿package view.vueRechercheChambre;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.Calendar;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import language.Francais;
import language.Language;
import view.Formulaire;
import view.tool.dateField.DateField;
import view.tool.jEasyTable.JEasyScrollTable;
import controler.Message;

/**
 * Interface graphique destiné à la recherche des chambres
* @author Alfredo Toyo
*
*/
public class VueRechercheChambre extends Formulaire{
	private static final long serialVersionUID = -3359375662982987681L;
	
	private GridBagConstraints constraint;
	
	private Language lang;
	
	private JPanel panelSearchFields;
	private JScrollPane panelScrollSearch;
	
	private JLabel labelDateArrivee
              	  ,labelDateDepart
              	  ,labelEtage
              	  ,labelLits
                  ,labelAvantArriere;
	
	private DateField dateArrivee
					 ,dateDepart;
	
	private JComboBox<String> comboBoxEtage
							 ,comboBoxLits
							 ,comboBoxAvantArriere;
	
	private JEasyScrollTable table;

	/**
	 * Constructeur
	 * @param lang
	 * @throws Exception 
	 */
	public VueRechercheChambre(Language lang, Message messageDataChanged) throws Exception{
		super(messageDataChanged);
		initVariable(lang);
		initComponents();
		setMnemonic();
	}

	/**
	 * Initialisation des attributs de la classe
	 * @throws Exception 
	 */
	private void initVariable(Language lang) throws Exception{
		this.lang = lang;
		
		panelSearchFields     = new JPanel(new FlowLayout(FlowLayout.LEFT));
		panelScrollSearch     = new JScrollPane();
		
		constraint = new GridBagConstraints();
		
		table = new JEasyScrollTable(lang.getRechercheChambreColonne());
		table.setSelectable(JEasyScrollTable.SINGLE_SELECTION);
		
		Calendar calendarPreviousYear = Calendar.getInstance();
		calendarPreviousYear.roll(Calendar.YEAR,-1);
		dateArrivee = DateField.fromCalendar(calendarPreviousYear);
		dateDepart  = DateField.fromCalendar(Calendar.getInstance());
		
		comboBoxEtage 		 = new JComboBox<String>();
		comboBoxLits 		 = new JComboBox<String>();
		comboBoxAvantArriere = new JComboBox<String>();
		
		labelDateArrivee    = new JLabel(lang.getRechercheChambre_DateArrivee().replaceAll("%", ""));
		labelDateDepart     = new JLabel(lang.getRechercheChambre_DateDepart().replaceAll("%", ""));
		labelEtage 			= new JLabel(lang.getRechercheChambre_Etage().replaceAll("%", ""));
		labelLits 			= new JLabel(lang.getRechercheChambre_Lits().replaceAll("%", ""));
		labelAvantArriere 	= new JLabel(lang.getRechercheChambre_AvantArriere().replaceAll("%", ""));
	}
	
	/**
	 * Initialisation des composants du formulaire
	 * @throws Exception 
	 */
	private void initComponents() throws Exception{
		panelScrollSearch.setViewportView(panelSearchFields);
		panelScrollSearch.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		panelScrollSearch.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
		panelScrollSearch.setPreferredSize(new Dimension (1, 50));
		panelScrollSearch.setSize(new Dimension (1, 50));
		panelScrollSearch.setMinimumSize(new Dimension (1, 50));
		
		constraint.anchor  = GridBagConstraints.NORTHWEST;
		constraint.fill    = GridBagConstraints.HORIZONTAL;
		constraint.weightx = 1.0;
		constraint.weighty = 0.0;
		constraint.gridx   = 0;
		constraint.gridy   = 0;
		constraint.insets  = new Insets(0, 5, 5, 5);
		add(panelScrollSearch,constraint);
		
		panelSearchFields.add(labelDateArrivee);
		panelSearchFields.add(dateArrivee);
		panelSearchFields.add(labelDateDepart);
		panelSearchFields.add(dateDepart);
		panelSearchFields.add(labelEtage);
		panelSearchFields.add(comboBoxEtage);
		panelSearchFields.add(labelLits);
		panelSearchFields.add(comboBoxLits);
		panelSearchFields.add(labelAvantArriere);
		panelSearchFields.add(comboBoxAvantArriere);
		
		constraint.fill    = GridBagConstraints.BOTH;
		constraint.insets  = new Insets(0, 5, 0, 5);
		constraint.weightx = 1.0;
		constraint.weighty = 1.0;
		constraint.gridx   = 0;
		constraint.gridy   = 1;
		add(table,constraint);

		comboBoxAvantArriere.addItem("");
		comboBoxAvantArriere.addItem(lang.getAnswerOui());
		comboBoxAvantArriere.addItem(lang.getAnswerNon());
	}
	
	/**
	 * Initialisation des mnémoniques
	 */
	private void setMnemonic(){
		labelDateArrivee.setDisplayedMnemonic(lang.getRechercheChambre_DateArrivee().charAt(lang.getRechercheChambre_DateArrivee().indexOf("%")+1));
		labelDateArrivee.setLabelFor(dateArrivee);
		
		labelDateDepart.setDisplayedMnemonic(lang.getRechercheChambre_DateDepart().charAt(lang.getRechercheChambre_DateDepart().indexOf("%")+1));
		labelDateDepart.setLabelFor(dateDepart);
		
		labelEtage.setDisplayedMnemonic(lang.getRechercheChambre_Etage().charAt(lang.getRechercheChambre_Etage().indexOf("%")+1));
		labelEtage.setLabelFor(comboBoxEtage);
		
		labelLits.setDisplayedMnemonic(lang.getRechercheChambre_Lits().charAt(lang.getRechercheChambre_Lits().indexOf("%")+1));
		labelLits.setLabelFor(comboBoxLits);
		
		labelAvantArriere.setDisplayedMnemonic(lang.getRechercheChambre_AvantArriere().charAt(lang.getRechercheChambre_AvantArriere().indexOf("%")+1));
		labelAvantArriere.setLabelFor(comboBoxAvantArriere);
	}
	
	/*getter*/
	/**
	 * Retourne la valeur de l'item selectionné du champ étage
	 * @return étage selectionné
	 */
	public String getSelectedEtage() {
		if(comboBoxEtage.getItemCount() == 0)
		{
			return "";
		}		
		return comboBoxEtage.getSelectedItem().toString();
	}
	
	/**
	 * Retourne la valeur de l'item selectionné du champ lits
	 * @return lits selectionné
	 */
	public String getSelectedLits() {
		if(comboBoxLits.getItemCount() == 0)
		{
			return "";
		}			
		return comboBoxLits.getSelectedItem().toString();
	}
	
	/**
	 * Retourne la valeur de l'item selectionné pour l'emplacement de la chambre
	 * dans l'hotel
	 * @return emplacement de la chambre
	 */
	public String getSelectedAvantArriere() {
		if (comboBoxAvantArriere.getSelectedItem().equals(lang.getAnswerOui()))
			return "V";
		if (comboBoxAvantArriere.getSelectedItem().equals(lang.getAnswerNon()))
			return "F";
		if (comboBoxAvantArriere.getSelectedItem().equals(""))
			return "";
		return comboBoxAvantArriere.getSelectedItem().toString();
	}
	
	/**
	 * Retourne la date d'arrivée du client
	 * @return la date selectionnée d'arrivée
	 */
	public String getDateArrivee(){
		return dateArrivee.getDate().toString();
	}

	/**
	 * Retourne la date de départ du client
	 * @return la date selectionnée de départ
	 */
	public String getDateDepart(){
		return dateDepart.getDate().toString();
	}
	

	/*setter*/
	/**
	 * Affectation d'une valeur pour l'ajout d'un item dans le champ étage
	 * @param comboCategorie
	 */
	public void addEtage(String selection) {
		this.comboBoxEtage.addItem(selection);
	}
	
	/**
	 * Affectation d'une valeur pour l'ajout d'un item dans le champ lits
	 * @param comboBoxLits
	 */
	public void addNombredeLits(String selection) {
		this.comboBoxLits.addItem(selection);
	}
	
	/**
	 * vider la liste d'etage
	 */
	public void clearEtage(){
		this.comboBoxEtage.removeAllItems();
	}
	
	/**
	 * vide la liste de nombre de lit
	 */
	public void clearNombredeLits(){
		this.comboBoxLits.removeAllItems();
	}
	
	/**
	 * Affectation d'une valeur pour l'ajout d'un item dans le champ étage
	 * @param selection
	 */
	public void setSelectedEtage(String selection){
		comboBoxEtage.setSelectedItem(selection);
	}
	
	/**
	 * Affectation d'une valeur pour l'ajout d'un item dans le champ lits
	 * @param selection
	 */
	public void setSelectedLits(String selection){
		comboBoxLits.setSelectedItem(selection);
	}
	
	/**
	 * 
	 * @param numero
	 * @param etage
	 * @param devant
	 * @param nbLit
	 * @param description
	 */
	public void addLineToTableChambre(int numero, int etage, boolean devant, int nbLit, String description){
		try {
			table.addRow(new JLabel[]{new JLabel(""+numero), 
					                  new JLabel(""+etage),
					                  new JLabel(devant?lang.getAnswerOui():lang.getAnswerNon()),
					                  new JLabel(""+nbLit),
					                  new JLabel(description)});
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("JEasyTable.addRow mal utilisé");
		}
	}
	
	public int getSelectedChambre(){
		return Integer.valueOf(((JLabel)table.getSelectedRow()[0][0]).getText());
	}
	
	/**
	 * Vider la table
	 */
	public void clearChambre(){
		table.clear();
	}
	
	/**
	 * Le code du main est pour les fins de test d'affichage et ils peuvent être modifiés où effacés
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
		JFrame a = new JFrame("window");

		VueRechercheChambre fer = new VueRechercheChambre(Francais.getInstance(),null);
		a.setLayout(new GridBagLayout());
		
		GridBagConstraints contraint = new GridBagConstraints();
		contraint.fill = GridBagConstraints.BOTH;
		contraint.weightx = 1.0;
		contraint.weighty = 1.0;
		contraint.gridx = 0;
		contraint.gridy = 0;
		
		a.add(fer,contraint);
		a.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		a.pack();
		a.setVisible(true);
		a.setExtendedState(JFrame.MAXIMIZED_BOTH);
	}	
}
