package view.vueRapportDepot;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

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.jEasyTable.JEasyScrollTable;
import controler.Message;

public class VueRapportDepot extends Formulaire{
	/**
	 * 
	 */
	private static final long serialVersionUID = 5518588357446096090L;

	private JPanel panelSearchGrid;

	private JEasyScrollTable fermeture,
							 depot,
							 sommaire;

	private GridBagConstraints constraint;
	
	private JLabel labelFactureFermes,
						labelSommaire,
						labelTotal;
	
	private Language lang;
	
	public VueRapportDepot(Language lang, Message messageDataChanged) throws Exception
	{
		super(messageDataChanged);
		initVariable(lang);
		initComponents();
		setMnemonic();
	}
	
	/**
	* @throws Exception 
	* 
	*/
	private void initVariable(Language lang) throws Exception
	{
		this.lang = lang;
		panelSearchGrid       = new JPanel(new GridBagLayout());
		
		constraint = new GridBagConstraints();
		
		fermeture  = new JEasyScrollTable(lang.getRapportDepotColumn1());
		fermeture.setSelectable(JEasyScrollTable.UNLIMITED_SELECTION);
		depot  = new JEasyScrollTable(lang.getRapportDepotColumn2());
		depot.setSelectable(JEasyScrollTable.SINGLE_SELECTION);
		sommaire  = new JEasyScrollTable(lang.getRapportDepotColumn3());
		
		labelFactureFermes   = new JLabel(lang.getRapportDepotFermetureFermees());
		labelSommaire        = new JLabel(lang.getRapportDepotSommaire());
		labelTotal		     = new JLabel(lang.getRapportDepotTotal());
	}
	
	/**
	* 
	*/
	private void initComponents()
	{	
		constraint.fill = GridBagConstraints.BOTH;
		constraint.anchor = GridBagConstraints.NORTH;
		constraint.insets  = new Insets(0, 5, 5, 5);
		constraint.weightx = 1.0;
		constraint.weighty = 1.0;
		constraint.gridx = 0;
		constraint.gridy = 1;
		panelSearchGrid.add(fermeture, constraint);	
		
		constraint.fill = GridBagConstraints.HORIZONTAL;
		constraint.weightx = 1.0;
		constraint.weighty = 0;
		constraint.gridx = 1;
		constraint.gridy = 0;
		panelSearchGrid.add(labelFactureFermes, constraint);
		
		constraint.fill = GridBagConstraints.BOTH;
		constraint.anchor = GridBagConstraints.NORTH;
		constraint.insets  = new Insets(0, 5, 5, 5);
		constraint.weightx = 1.0;
		constraint.weighty = 1.0;
		constraint.gridx = 1;
		constraint.gridy = 1;
		panelSearchGrid.add(depot, constraint);
		
		constraint.fill = GridBagConstraints.HORIZONTAL;
		constraint.weightx = 1.0;
		constraint.weighty = 0;
		constraint.gridx = 2;
		constraint.gridy = 0;
		panelSearchGrid.add(labelSommaire, constraint);
		
		constraint.fill = GridBagConstraints.BOTH;
		constraint.anchor = GridBagConstraints.NORTH;
		constraint.insets  = new Insets(0, 5, 5, 5);
		constraint.weightx = 1.0;
		constraint.weighty = 1.0;
		constraint.gridx = 2;
		constraint.gridy = 1;
		panelSearchGrid.add(sommaire, constraint);
		
		constraint.fill = GridBagConstraints.HORIZONTAL;
		constraint.weightx = 1.0;
		constraint.weighty = 0;
		constraint.gridx = 2;
		constraint.gridy = 2;
		panelSearchGrid.add(labelTotal, constraint);
		
		constraint.fill    = GridBagConstraints.BOTH;
		constraint.anchor  = GridBagConstraints.NORTH;
		constraint.insets  = new Insets(0, 5, 0, 5);
		constraint.weightx = 1.0;
		constraint.weighty = 1;
		constraint.gridx   = 0;
		constraint.gridy   = 0;
		add(panelSearchGrid,constraint);
	}
	
	/**
	* 
	*/	
	private void setMnemonic()
	{

	}
	
	
	public List<Integer> getSelectedFermeture(){
		List<Integer> resultat = new ArrayList<>();
		for (JComponent[] comp :fermeture.getSelectedRow())
			resultat.add(Integer.valueOf(((JLabel)comp[0]).getText()));
		return resultat;
	}
	
	public void unSelectedFermeture() throws Exception{
		fermeture.unselectAll();
	}

	public void addFermeture(Object numeroFermeture, Object status) throws Exception
	{
		if (numeroFermeture==null)
			numeroFermeture="";
		if(status==null)
			status="";
		fermeture.addRow(new JLabel[]{new JLabel(numeroFermeture.toString()),new JLabel(status.toString())});
	}
	
	public Integer getSelectedDepot(){
		for (JComponent[] comp : depot.getSelectedRow()){
			return Integer.valueOf(((JLabel)comp[0]).getText());
		}
		return null;
	}
	
	public void unSelectedDepot() throws Exception{
		depot.unselectAll();
	}

	public void addDepot(Object numeroDepot, Object status) throws Exception
	{
		if (numeroDepot==null)
			numeroDepot="";
		if(status==null)
			status="";
		depot.addRow(new JLabel[]{new JLabel(numeroDepot.toString()),new JLabel(status.toString())});
	}
	

	public void addToSommaire(String methodeDePaiement, BigDecimal montant) throws Exception
	{
		sommaire.addRow(new JLabel[]{new JLabel(methodeDePaiement),new JLabel(montant.toPlainString())});
	}
	
	public void setTotal(Double tot)
	{
		labelTotal.setText("Total : " + tot + "$");
	}
	
	/**
	* 
	*/
	public void clearFermeture()
	{
		fermeture.clear();
	}
	
	/**
	* 
	*/
	public void clearDepot()
	{
		depot.clear();
	}
	
	/**
	* 
	*/
	public void clearSommaire()
	{
		sommaire.clear();
	}

	/**
	* @param args
	* @throws Exception 
	*/
	public static void main(String[] args) throws Exception 
	{
		JFrame a = new JFrame("window");
		JLabel[][] labels  = {{new JLabel("0"), new JLabel("1")},
							  {new JLabel("0"), new JLabel("1")}},
				   labels2 = {{new JLabel("0"), new JLabel("1"), new JLabel("2")},
				  			  {new JLabel("0"), new JLabel("1"), new JLabel("2")}},
				   labels3 = {{new JLabel("0"), new JLabel("1")},
				  			  {new JLabel("0"), new JLabel("1")}};
		
		VueRapportDepot test = new VueRapportDepot(Francais.getInstance(),null);
		//test.addFermetureToOuvert(labels);
//		test.addFermetureToFermer(labels2);
//		test.addToSommaire(labels3);
		test.setTotal(12.35);
		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);
	}
}
