package view.vueRapportFacture;

import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.sql.Date;
import java.util.Calendar;

import javax.swing.DefaultListModel;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import language.Francais;
import language.Language;
import view.Formulaire;
import view.tool.JScrollList.JScrollList;
import view.tool.dateField.DateField;
import view.tool.jEasyTable.JEasyScrollTable;

public class VueRapportFacture extends Formulaire
{
	/**
	 * 
	 */
	private static final long serialVersionUID = -7727032469282804250L;

	private JPanel panelSearchFields,
				   panelSearchGrid;
//				   panelButtons;
	
	private JEasyScrollTable table;
	
//	private JButton buttonImprimer,
//					buttonRechercher;
	
	private GridBagConstraints constraint;
	
	private JLabel labelDate,
						labelPromotion;
	
	private DateField dateInput;
	
	DefaultListModel<String> listModel;
	private JScrollList<String> listPromotion;
	
	private Language lang;
	
	public VueRapportFacture(Language lang) throws Exception
	{
		super();
		initVariable(lang);
		initComponents();
		initListener();
		setMnemonic();
	}
	
	/**
	* @throws Exception 
	* 
	*/
	private void initVariable(Language lang) throws Exception
	{
		this.lang = lang;
		
		panelSearchFields     = new JPanel(new FlowLayout(FlowLayout.LEFT));
		panelSearchGrid       = new JPanel(new GridBagLayout());
//		panelButtons          = new JPanel();
		
		constraint = new GridBagConstraints();
		
		table  = new JEasyScrollTable(lang.getRapportFactureColumn());
		
		labelDate 		  = new JLabel(lang.getRapportFactureDate().replaceAll("%", ""));
		labelPromotion    = new JLabel(lang.getRapportFacturePromotion().replaceAll("%", ""));
		
		dateInput 		  = DateField.fromCalendar(Calendar.getInstance());
		
		listModel = new DefaultListModel<String>();
		
		listPromotion     = new JScrollList<String>(listModel);
		listPromotion.setVisibleRowCount(3);
		
//		buttonImprimer = new JButton(lang.getRapportFactureImprimer().replaceAll("%", ""));
//		buttonRechercher = new JButton(lang.getRapportFactureRechercher().replaceAll("%", ""));
//		connecterUneAction(buttonImprimer, ControlerRapportFacture.imprimer);
//		connecterUneAction(buttonRechercher, ControlerRapportFacture.rechercher);
	}
	
	/**
	* 
	*/
	private void initComponents()
	{		
		panelSearchFields.add(labelDate);
		panelSearchFields.add(dateInput);
		panelSearchFields.add(labelPromotion);
		panelSearchFields.add(listPromotion);
		
		constraint.fill = GridBagConstraints.BOTH;
		constraint.weightx = 1.0;
		constraint.weighty = 1.0;
		constraint.gridx = 0;
		constraint.gridy = 0;
		panelSearchGrid.add(table,constraint);
		
//		panelButtons.add(buttonRechercher);		
//		panelButtons.add(buttonImprimer);
		
		constraint.fill    = GridBagConstraints.HORIZONTAL;
		constraint.anchor  = GridBagConstraints.NORTHWEST;
		constraint.insets  = new Insets(10, 3, 10, 3);
		constraint.weightx = 1.0;
		constraint.weighty = 0;
		constraint.gridx   = 0;
		constraint.gridy   = 0;
		add(panelSearchFields,constraint);
		
		constraint.fill    = GridBagConstraints.BOTH;
		constraint.insets  = new Insets(10, 3, 10, 3);
		constraint.weightx = 1.0;
		constraint.weighty = 1.0;
		constraint.gridx   = 0;
		constraint.gridy   = 1;
		add(panelSearchGrid,constraint);
		
		constraint.fill = GridBagConstraints.NONE;
		constraint.anchor = GridBagConstraints.SOUTHEAST;
		constraint.insets  = new Insets(10, 3, 10, 3);
		constraint.weightx = 1.0;
		constraint.weighty = 0.0;
		constraint.gridx = 0;
		constraint.gridy = 2;
//		add(panelButtons,constraint);
	}
	
	/**
	* 
	*/
	private void initListener()
	{		
//		buttonImprimer.addActionListener(this);
//		buttonRechercher.addActionListener(this);
	}
	
	/**
	* 
	*/	
	private void setMnemonic()
	{
		labelDate.setDisplayedMnemonic(lang.getRapportFactureDate().charAt(lang.getRapportFactureDate().indexOf('%')+1));
		labelDate.setLabelFor(dateInput);
		
		labelPromotion.setDisplayedMnemonic(lang.getRapportFacturePromotion().charAt(lang.getRapportFacturePromotion().indexOf('%')+1));
		labelPromotion.setLabelFor(listPromotion);
		
//		buttonImprimer.setMnemonic(lang.getRapportFactureImprimer().charAt(lang.getRapportFactureImprimer().indexOf('%')+1));
//		buttonRechercher.setMnemonic(lang.getRapportFactureRechercher().charAt(lang.getRapportFactureRechercher().indexOf('%')+1));
	}
	
	/**
	* 
	* @param content
	*/
	public void setContent(JComponent[][] content)
	{
		table.setContent(content);
	}
	
	/**
	* 
	*/
	public void clear()
	{
		table.clear();
	}
	
	public String getListPromotion() {
		return listPromotion.getSelectedValuesList().toString();
	}
	
	public String getDate()
	{
		return dateInput.getDate().toString();
	}
	
	public void setListPromotion(String selection) {
		listModel.addElement(selection);
		listPromotion.setModel(listModel);
	}
	
	public void setDate(Date date)
	{
		dateInput.setDate(date);
	}
	
	
	/**
	* @param args
	* @throws Exception 
	*/
	public static void main(String[] args) throws Exception 
	{
		JFrame a = new JFrame("window");
		JLabel[][] labels = {};
		
		VueRapportFacture test = new VueRapportFacture(Francais.getInstance());
		test.setContent(labels);
		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(test,contraint);
		a.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		a.pack();
		a.setVisible(true);
	}
}
