package Model;

import java.io.Serializable;
import java.util.ArrayList;

public class PDC implements Serializable
{
	
	private static final long serialVersionUID = -8834575324009904903L;
	
	private double prodtot;		// production en une journée
	private String nom;
	private boolean busy;
	private ArrayList<Machine> machine;
	private ArrayList<Employe> employe;

	public PDC(String nom) 
	{
		this.nom = nom;
		this.busy = false;
	}
	
	public void setBusy(boolean b)
	{
		busy = b;
	}
	
	public boolean getBusy()
	{
		return busy;
	}
	
	public String getNom() 
	{
		return nom;
	}

	public void setNom(String nom) 
	{
		this.nom = nom;
	}
	
	public void ajouterM(Machine m)
	{
		boolean present = false;
		int i = -1;
		
		while(present == false)
		{
			i++;
			if(m.getIdentifiant() == machine.get(i).getIdentifiant())
			{
				present= true;
			}
		}
		if(present == false)
		{
			machine.add(m);
			calculProdJour();
		}
		else
		{
			System.out.println("Machine deja présente dans le poste de charge !");
		}
	}

	public void ajouterE(Employe e)
	{
		boolean present = false;
		int i = -1;
		
		while(present == false)
		{
			i++;
			if(e.getIdentifiant() == employe.get(i).getIdentifiant())
			{
				present= true;
			}
		}
		if(present == false)
		{
			employe.add(e);
			calculProdJour();
		}
		else
		{
			System.out.println("Employe deja présent dans le poste de charge !");
		}
	}
	
	public void supprimerE(Employe e)
	{
		boolean present = false;
		int i = -1;
		
		while(present == false)
		{
			i++;
			if(e.getIdentifiant() == employe.get(i).getIdentifiant())
			{
				present= true;
			}
		}
		if(present == false)
		{
			System.out.println("Cet employe n'appartient pas au poste de charge choisi.");
		}
		else
		{
			employe.remove(i);
			calculProdJour();
		}
	}
	
	public void supprimerM(Machine m)
	{
		boolean present = false;
		int i = -1;
		
		while(present == false)
		{
			i++;
			if(m.getIdentifiant() == machine.get(i).getIdentifiant())
			{
				present= true;
			}
		}
		if(present == false)
		{
			System.out.println("Cette machine n'appartient pas au poste de charge choisi.");
		}
		else
		{
			machine.remove(i);
			calculProdJour();
		}
	}
	
	public void afficherPDC()
	{
		System.out.println("Le poste de Charge : " + nom);
		for(int i = 0; i < machine.size(); i++) System.out.println(machine.get(i).toString());
		for(int j = 0; j < employe.size(); j++) System.out.println(employe.get(j).toString());
		System.out.println("");
		System.out.println("Production Totale : " + prodtot);
	}
	
	public double calculTempsProd(int nbProduit)  // retourne le nombre de jour necessaire pour fabriquer tout ces produits.
	{
		return (nbProduit/prodtot);
	}
	
	private void calculProdJour()
	{
		double prod = 0;
		for(int i = 0; i < machine.size(); i++) prod += machine.get(i).getProduction()*24;
		for(int j = 0; j < employe.size(); j++) prod += employe.get(j).getProduction()*employe.get(j).getHeure();
		this.prodtot = prod;
	}
}
