package table.dashboard;

import org.sig.fw.db.corrida.dao.Facture_DBAO;
import org.sig.fw.db.corrida.dao.Payment_DBAO;
import org.sig.fw.db.corrida.dao.Rfp_DBAO;
import org.sig.fw.db.corrida.model.Commande;
import org.sig.fw.db.corrida.model.Facture;
import org.sig.fw.db.corrida.model.Rfp;
import org.sig.fw.utility.date.SDate;
import org.sig.fw.utility.model.Devise;

import javax.swing.table.AbstractTableModel;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class Facture_Paiement_Wait_Table extends AbstractTableModel 
{
	private static final long serialVersionUID = 4393988485779673713L;
	
	private final String[] entetes = {"Class" ,"N°Dossier", "Localisation", "Nom", "Client 1", "Client 2", "Montant Net", "Montant Restant", "Echéance", "N°Facture", "Resp. Fact.", "Factor ?", "Société", "Relance à faire", "Relance"};
	
	private List<Facture> factures = new ArrayList<Facture>();
	private HashMap<Facture, List<Rfp>> rfps = new HashMap<Facture, List<Rfp>>();
	private HashMap<Facture, Double> payments = new HashMap<Facture, Double>();
	
	public Facture_Paiement_Wait_Table()
	{
		super();
		this.factures = new Facture_DBAO("fact").getAllFacturePaiementWait();
		this.rfps = new HashMap<Facture, List<Rfp>>();
		for(Facture f : factures)
		{
			rfps.put(f, new Rfp_DBAO("rfp").getWithFacture(f));
			payments.put(f, new Payment_DBAO("pay").getSumWithFacture(f));
		}
	}

	@Override
	public int getColumnCount() 
	{
		return this.entetes.length;
	}

	@Override
	public String getColumnName(int columnIndex) 
	{
		return this.entetes[columnIndex];
	}
	
	@Override
	public int getRowCount() 
	{
		return this.factures.size();
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex)
	{
		Facture f = (Facture) factures.get(rowIndex);	
		List<Rfp> rfps = this.rfps.get(f) == null ? new ArrayList<Rfp>() : this.rfps.get(f);
		switch (columnIndex) {

		case 0:
			return f;
		case 1:
			return f.getCommande().getDossier().getReferenceNumber();
	
		case 2:
			return f.getCommande().getDossier().getLocalisation();

		case 3:
			return f.getCommande().getDossier().getName();
			
		case 4:
			if(rfps.size() > 0)
				return rfps.get(0).getPayer().getBddcClient().getCompany();
			else
				return "";
			
		case 5:
			if(rfps.size() > 1)
				return rfps.get(1).getPayer().getBddcClient().getCompany();
			else
				return "";
			
		case 6:
			return new Devise(f.getAmountNet(), Devise.TYPE_UE);
			
		case 7:
			return new Devise(f.getAmountNet() - payments.get(f), Devise.TYPE_UE);

			case 8:
				return f.getEcheance();
			case 9:
				return f.getFactureNumber();
			case 10:
				return f.getResponsible().getAbreviation();
			case 11:
				return f.getPaymentMode().getShortValue().contains("FACTOR") ? "Oui" : "Non";
			case 12:
				return f.getCommande().getSociety().getName();
			case 13:
				return f.getRelance().getType().getName();
			case 14:
				return f.getRelance().getEcheanceDate();

		default:
			throw new IllegalArgumentException();
		}
	}
	
	@Override
	public Class<?> getColumnClass(int columnIndex) {
		switch (columnIndex) {

		case 0:
			return Commande.class;
		case 1:
		case 2:
		case 3:
		case 4:
		case 5:
			return String.class;
		case 6:
		case 7:
			return Devise.class;
		case 8:
			return SDate.class;
		case 9:
		case 10:
		case 11:
		case 12:
			return String.class;
		case 13:
			return String.class;
		case 14:
			return SDate.class;

		default:
			return Object.class;
		}
	}
}
