package ApplicationClient;



import java.util.ArrayList;

import javax.swing.*;

import Commande.Livraison;
import Commande.Produit;

public class ApplicationClient implements Cloneable {
	
	private String identifiant;
	private String motDePasse;
	private boolean estLoggé;
	private Livraison bonDeCommande ;
	private ArrayList<String> array ;
	
	public ApplicationClient(String identifiant, String motDePasse) {
		this.identifiant = identifiant;
		this.motDePasse = motDePasse;
		array = new ArrayList<String>();
	}
	
	public boolean logIn(String identifiant, String motDePasse)
	{
		if(estLoggé)
		{
			JOptionPane.showMessageDialog(null, this, "Vous devez deja loggé ! ", JOptionPane.WARNING_MESSAGE);
		}
		else
		{
			if(this.identifiant.equals(identifiant) && this.motDePasse.equals(motDePasse))
			{
				estLoggé = true;
			}
		}
		return estLoggé;
	}

	public void remplirBonDeCommande()
	{
		if(estLoggé){
			
			// On récupère la destination 
			String nomClient;
			nomClient = JOptionPane.showInputDialog(null,"Veuillez saisir votre nom :","Création du bon de commande",JOptionPane.INFORMATION_MESSAGE);
			
			
			// On récupère la destination 
			String destination;
			destination = JOptionPane.showInputDialog(null,"Veuillez saisir votre destination de livraison :","Création du bon de commande",JOptionPane.INFORMATION_MESSAGE);
			
			// On peut donc créer le nouveau bon de commande
			this.bonDeCommande = new Livraison(nomClient,destination);
			
			boolean nouveauProduit = true;
			do{
				int reponse = JOptionPane.showConfirmDialog(null,
					       "Voulez vous saisir un nouveau produit ?","Remplissage du bon de commande",
					        JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);
				if (reponse == JOptionPane.YES_OPTION) 
				{
					// On recupere le nom du produit
					String libele;
					libele = JOptionPane.showInputDialog(null,"Veuillez saisir le nom du produit : ","Création d'un nouveau produit",JOptionPane.INFORMATION_MESSAGE);
					
					// On crée le nouveau produit
					Produit produitTmp = new Produit(libele);
					
					// On ajoute le nouveau produit sans le bonDeCommande
					if(this.bonDeCommande.ajouterProduit((Produit)produitTmp.clone()))
					{
						array.add(libele);
					}
			 	}
				else
				{
					if (reponse == JOptionPane.NO_OPTION)
			  		{
						nouveauProduit = false;
						JOptionPane.showMessageDialog(null,"Produits enregistrés ! ", "Sauvegarde" ,JOptionPane.INFORMATION_MESSAGE);
						this.bonDeCommande.afficher();
			  		}
					else
					{
						JOptionPane.showMessageDialog(null, "Choix invalide ! ", "Attention" , JOptionPane.WARNING_MESSAGE);
					}
	
				}
			}
			while(nouveauProduit);
			
			boolean nouvelleIncompatibilite = true;
			do{
				int reponse = JOptionPane.showConfirmDialog(null,
					       "Voulez vous saisir une nouvelle incompatibilité ?","Remplissage du bon de commande",
					        JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);
				if (reponse == JOptionPane.YES_OPTION) 
				{
					
					Object[] choix1 = array.toArray();
					String res1 = (String)JOptionPane.showInputDialog(null, "Choisissez votre premier produit ", "Nouvelle Incompatibilité (1/2)", JOptionPane.INFORMATION_MESSAGE, null, choix1 , choix1[choix1.length-1]);
			        
					ArrayList<String> clone = (ArrayList<String>)array.clone();
					ArrayList<String> tmp = clone;
					tmp.remove(res1);
					Object[] choix2 = tmp.toArray();
					String res2 = (String)JOptionPane.showInputDialog(null, "Choisissez votre second produit ", "Nouvelle Incompatibilité (2/2)", JOptionPane.INFORMATION_MESSAGE, null, choix2 , choix2[choix2.length-1]);
					
					// On recupére les deux produits
					int i1 = array.indexOf(res1);
					int i2 = array.indexOf(res2);
					
					Produit p1 = (Produit) this.bonDeCommande.recProduits().elementAt(i1).clone();
					Produit p2 = (Produit) this.bonDeCommande.recProduits().elementAt(i2).clone();
					this.bonDeCommande.ajouterIncompatibilite(p1,p2);
			 	}
				else
				{
					if (reponse == JOptionPane.NO_OPTION)
			  		{
						nouvelleIncompatibilite = false;
						JOptionPane.showMessageDialog(null, "Incompatibilités enregistrées ! ","Enregistrement" ,JOptionPane.INFORMATION_MESSAGE);
			  		}
					else
					{
						JOptionPane.showMessageDialog(null, "Choix invalide ! ","Attention", JOptionPane.WARNING_MESSAGE);
					}
	
				}
			}
			while(nouvelleIncompatibilite);
			
			envoyerBonDeCommande();
		}
		else
		{
			JOptionPane.showMessageDialog(null, this, "Vous devez être loggé auparavant ! ", JOptionPane.ERROR_MESSAGE);
		}
	}
	
	public void envoyerBonDeCommande()
	{
		if(estLoggé /* && (this.bonDeCommande!=null)*/){
			int reponse = JOptionPane.showConfirmDialog(null,
				       "Voulez-vous valider votre commande ?","Confirmer commande",
				        JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);
			if (reponse == JOptionPane.YES_OPTION) 
			{
				// On recupere le nom du produit
				String nom;
				nom = JOptionPane.showInputDialog(null,"Veuillez saisir le nom du fichier : ","Envoie de la commande",JOptionPane.INFORMATION_MESSAGE);
				
				this.bonDeCommande.sauvegarderLivraison(nom);
		 	}
			else
			{
				if (reponse == JOptionPane.NO_OPTION)
		  		{
//					modifierCommande();
		  		}
				else
				{
					JOptionPane.showMessageDialog(null, this, "Choix invalide ! ", JOptionPane.WARNING_MESSAGE);
					envoyerBonDeCommande();
				}
	
			}
		}
		
	}

	private void modifierCommande() {
		if(estLoggé /*&& (this.bonDeCommande!=null)*/){
			
			boolean nouveauProduit = true;
			do{
				int reponse = JOptionPane.showConfirmDialog(null,
					       "Voulez vous saisir un nouveau produit ?","Remplissage du bon de commande",
					        JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);
				if (reponse == JOptionPane.YES_OPTION) 
				{
					// On recupere le nom du produit
					String libele;
					libele = JOptionPane.showInputDialog(null,"Veuillez saisir le nom du produit : ","Création d'un nouveau produit",JOptionPane.INFORMATION_MESSAGE);
					
					// On crée le nouveau produit
					Produit produitTmp = new Produit(libele);
					
					// On ajoute le nouveau produit sans le bonDeCommande
					if(this.bonDeCommande.ajouterProduit(produitTmp))
					{
						array.add(libele);
					}
					array.add(libele);
			 	}
				else
				{
					if (reponse == JOptionPane.NO_OPTION)
			  		{
						nouveauProduit = false;
						JOptionPane.showMessageDialog(null,"Produits enregistrés ! ", "Sauvegarde" ,JOptionPane.INFORMATION_MESSAGE);
			  		}
					else
					{
						JOptionPane.showMessageDialog(null, "Choix invalide ! ", "Attention" , JOptionPane.WARNING_MESSAGE);
					}
	
				}
			}
			while(nouveauProduit);
			
			boolean nouvelleIncompatibilite = true;
			do{
				int reponse = JOptionPane.showConfirmDialog(null,
					       "Voulez vous saisir une nouvelle incompatibilité ?","Remplissage du bon de commande",
					        JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);
				if (reponse == JOptionPane.YES_OPTION) 
				{
					
					Object[] choix1 = array.toArray();
					String res1 = (String)JOptionPane.showInputDialog(null, "Choisissez votre premier produit ", "Nouvelle Incompatibilité (1/2)", JOptionPane.INFORMATION_MESSAGE, null, choix1 , choix1[choix1.length-1]);
			        
					ArrayList<String> tmp = (ArrayList<String>)array.clone();
					tmp.remove(res1);
					Object[] choix2 = tmp.toArray();
					String res2 = (String)JOptionPane.showInputDialog(null, "Choisissez votre second produit ", "Nouvelle Incompatibilité (2/2)", JOptionPane.INFORMATION_MESSAGE, null, choix2 , choix2[choix2.length-1]);
					
					// On recupére les deux produits
					int i1 = array.indexOf(res1);
					int i2 = array.indexOf(res2);
					Produit p1 = (Produit) this.bonDeCommande.recProduits().elementAt(i1).clone();
					Produit p2 = (Produit) this.bonDeCommande.recProduits().elementAt(i1).clone();
					this.bonDeCommande.ajouterIncompatibilite(p1,p2);
			 	}
				else
				{
					if (reponse == JOptionPane.NO_OPTION)
			  		{
						nouvelleIncompatibilite = false;
						JOptionPane.showMessageDialog(null, "Incompatibilités enregistrées ! ","Enregistrement" ,JOptionPane.INFORMATION_MESSAGE);
			  		}
					else
					{
						JOptionPane.showMessageDialog(null, "Choix invalide ! ","Attention", JOptionPane.WARNING_MESSAGE);
					}
	
				}
			}
			while(nouvelleIncompatibilite);
			
			envoyerBonDeCommande();
		}
		else
		{
			JOptionPane.showMessageDialog(null, this, "Vous devez être loggé ! ", JOptionPane.ERROR_MESSAGE);
			modifierCommande();
		}
	}
	
}
