package Modèle;
import java.util.Scanner;
import java.util.Vector;

//
//
//  Generated by StarUML(tm) Java Add-In
//
//  @ Project : Untitled
//  @ File Name : Contrainte.java
//  @ Date : 25/04/2011
//  @ Author : 
//
//

public class Contrainte {
	protected String contrainte;
	protected Vector<Integer> coeffs;
	protected Vector<Variable> variables;
	protected String operateur;
	protected int sndMembre;

	public Contrainte(String c) {
		contrainte = c;
		coeffs = new Vector<Integer>();
		variables = new Vector<Variable>();
		sndMembre=0;
		while (!parser()) {
			System.out.println(contrainte + " : Erreur");
			System.out.println("La contrainte doit être de la forme 3*x1-9*x2<=120 ou -32x1-58>=10 ou encore avec un signe d'égalité simplement.");
			System.out.println("Redonnez la contrainte");
			Scanner sc = new Scanner(System.in);
			contrainte = sc.nextLine();
		}
	}
	public void setOperateur(String op){
		operateur=op;
	}
	public boolean parser() {
		variables=new Vector<Variable>();
		sndMembre=0;
		try {
			if (!contrainte.contains("="))
				return false;
			int index = -1;
			if (contrainte.contains("<"))
				index = contrainte.indexOf("<");
			else {
				if (contrainte.contains(">"))
					index = contrainte.indexOf(">");
				else
					index = contrainte.indexOf("=");
			}
			String f = contrainte.substring(0, index);
			if (f.equals(""))
				return false;
			Vector<String> elements = new Vector<String>();
			String[] tabPlus;
			String[] tabMoins = f.split("-");
			for (int i = 1; i < tabMoins.length; i++)
				tabMoins[i] = "-" + tabMoins[i];
			for (int i = 0; i < tabMoins.length; i++)
				if (!tabMoins[i].equals("")) {
					if (tabMoins[i].contains("+")) {
						tabPlus = tabMoins[i].split("\\+");
						for (int j = 0; j < tabPlus.length; j++)
							elements.add(tabPlus[j]);
					}
					else
						elements.add(tabMoins[i]);
				}
			String[] tab;
			for (int i = 0; i < elements.size(); i++) {
				if (elements.get(i).contains("*")) { // fonction de la forme
					// 3*x1-5*x2
					tab = elements.get(i).split("\\*");
					if (tab.length == 1) {
						coeffs.add(1);
						variables.add(new Variable(tab[0], null));
					}
					else {
						coeffs.add(Integer.parseInt(tab[0]));
						variables.add(new Variable(tab[1], null));
					}
				}
				else {// fonction de la forme 3x1-5x2
					if (elements.get(i).charAt(0) == '-') {
						if (Character.isLetter(elements.get(i).charAt(1))) {
							coeffs.add(-1);
							variables.add(new Variable(elements.get(i).substring(1, elements.get(i).length()), null));
						}
						else {
							int indexVar = 1;
							while (indexVar < elements.get(i).length() && Character.isDigit(elements.get(i).charAt(indexVar)))
								indexVar++;
							if (indexVar == elements.get(i).length()) {
								sndMembre+=-Integer.parseInt(elements.get(i));
							}
							else {
								coeffs.add(Integer.parseInt(elements.get(i).substring(0, indexVar)));
								variables.add(new Variable(elements.get(i).substring(indexVar, elements.get(i).length()), null));
							}
						}
					}
					else {
						if (Character.isLetter(elements.get(i).charAt(0))) {
							coeffs.add(1);
							variables.add(new Variable(elements.get(i), null));
						}
						else {
							int indexVar = 0;
							while (indexVar < elements.get(i).length() && Character.isDigit(elements.get(i).charAt(indexVar)))
								indexVar++;
							if (indexVar == elements.get(i).length()) {
								sndMembre+=-Integer.parseInt(elements.get(i));
							}
							else {
								coeffs.add(Integer.parseInt(elements.get(i).substring(0, indexVar)));
								variables.add(new Variable(elements.get(i).substring(indexVar, elements.get(i).length()), null));
							}
						}
					}
				}
			}
			if(contrainte.contains("<")||contrainte.contains(">"))
				index++;
			String sndMbr=contrainte.substring(index+1, contrainte.length());
			if(sndMbr.equals(""))
				return false;
			sndMembre+=Integer.parseInt(sndMbr);
			return true;
		} catch (Exception e) {
			System.out.println(e);
			return false;
		}
	}

	public Vector<Integer> getCoeffs() {
		return coeffs;
	}

	public Vector<Variable> getVariables() {
		return variables;
	}

	public String getOperateur() {
		return operateur;
	}

	public String getContrainte() {
		return contrainte;
	}

	public void setContrainte(String c) {
		contrainte = c;
	}

	public void ajoutVar(String v) {
		int index = contrainte.indexOf("<");
		if (index == -1) {
			index = contrainte.indexOf(">");
			if (index == -1) {
				index = contrainte.indexOf("=");
				contrainte=contrainte.replace("=", v + "=");
			}
			else
				contrainte=contrainte.replace(">", v + ">");
		}
		else
			contrainte=contrainte.replace("<", (v + "<"));
	}
	public int getSndMembre(){
		return sndMembre;
	}
}
