package renderer.table;

import org.sig.fw.db.corrida.model.Facture;
import org.sig.fw.utility.date.SDate;

import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import java.awt.*;


public class Facture_Paiement_Wait_Renderer extends DefaultTableCellRenderer 
{
	private static final long serialVersionUID = 1L;

	public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int col) 
	{
		super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, col);
		
		int column = -1;
		row = table.convertRowIndexToModel(row);
		for(int i = 0; i < table.getColumnCount(); i++)
		{
			if(table.getColumnName(i).equals("Class"))
				column = i;
		}
	   
		if(column == -1)
			return this;
		Facture model = (Facture)table.getModel().getValueAt(row, column);
		// COLORISATION DU FOND DES CELULLES EN FONCTION DE L'ETAT DE LA FACTURE (SANS LES DEUX DERNIERES COLONNES)
		if(model.getState() != null && col < 13) {
			setBackground(model.getState().getBackground());
			setForeground(model.getState().getForeground());
		}
		else {
			// COLORISATION DE LA DERNIERE COLONNE : ROUGE SI LA DATE DE RELANCE EST DEPASSEE
			if(new SDate().compareTo(model.getRelance().getEcheanceDate()) > 0 && col == 14) {
				setForeground(Color.WHITE);
				setBackground(Color.RED);
			} else {
				setBackground(null);
				setForeground(null);
			}
		}

		if(isSelected)
			setBackground(new Color(57, 105, 138));
		
		return this;
	}
}