#include "lecteur.h"
//les iterateurs
vector<regle>::iterator iterRegle;
vector<symbole>::iterator iterSymbole;

//constructeur qui lit le fichier texte et construit l'axiome,
// les règles du l_systeme
lecteur::lecteur(string nomfichier){  //lit un fichier
  ifstream fichier;
  string nom_fichier(nomfichier);
  fichier.open(nom_fichier.c_str());
  if(! fichier.fail()){
    int lon = 0;  //longueur du mot lu
    string mot;  
    int cpt = -1;
    
    while(! fichier.eof()){
      getline(fichier, mot,';');
      cpt++;
      if(! fichier.fail()){
	char* chaine2 = (char*)mot.c_str();
	string chaine = mot;	
	lon = mot.length(); 
	if(cpt == 0){             //angle
	  angle = atoi(chaine2);
	}
	
	if(cpt == 1){  //on se trouve sur la ligne de l'axiome
	  for (int i=0; i<lon; i++){
	    string c = chaine.substr(i, 1);
	    interpreteGraphique inter = interpreteGraphique(c);
	    symbole s = symbole(c, inter);
	    collectionSymbAxiome.push_back(s);
	  }
	  axio = axiome(collectionSymbAxiome);
	}
	  
	if(cpt>1){   //on se trouve sur les lignes de regles
	  string sy = chaine.substr(0,1); //le symbole de la règle
	  interpreteGraphique inter1 = interpreteGraphique(sy);
	  symbole symboleR = symbole(sy, inter1);
	  vector<symbole> collectionSymbRegle;
	  //on passe le mot "devient" et on a la règle
	  //on stocke les symbole d'une regle dans un vecteur de symbole
	  for (int i = 8; i< lon; i++){  
	    string ca = chaine.substr(i,1);
	    interpreteGraphique inter2 = interpreteGraphique(ca);
	    symbole s = symbole(ca, inter2);
	    collectionSymbRegle.push_back(s);
	  }
	  
	  //une fois la lecture de la règle terminée, on insère la règle
	  //dans un vecteur de règle puis on passe à la ligne suivante
	  for(iterSymbole = collectionSymbRegle.begin(); iterSymbole<collectionSymbRegle.end(); iterSymbole++){
	    symbole s = *iterSymbole;
	    //    s.lireSymbole();
	  }
	  //cout<<"fin "<<cpt-1<<" regle************"<<endl;
	  regle r = regle(symboleR, collectionSymbRegle);
	  collectionRegle.push_back(r);
	}
      }
    }
    fichier.close();
    cout<<"*******************fin de la lecture du fichier*************"<<endl;
  } 
  else{
    cout<<"Ouverture du fichier"<<nom_fichier<<"impossible"<<endl;
  }
}
	
//on applique les règles à l'axiome. On parcourt et on regarde si un symbole 
//correspond à une règle. Si c'est le cas on applique cette règle au symbole
void lecteur::appliqueRegleAxiome(){
  //on parcours la collection de règles  
  for (iterRegle = collectionRegle.begin(); iterRegle<collectionRegle.end(); iterRegle++){
    regle uneRegle = *iterRegle;
    symbole symboleDeLaRegle = uneRegle.getSymbole();
    //on parcours les symboles de l'axiome et si on rencontre le même
    //symbole que celui de la règle courante alors on applique la règle
    for (iterSymbole = collectionSymbAxiome.begin(); iterSymbole<collectionSymbAxiome.end(); iterSymbole++){
      symbole unSymbole = *iterSymbole;
      if(unSymbole.getNom() == symboleDeLaRegle.getNom()){
	unSymbole.affecteRegle(&uneRegle);
      }  
    }
  }
}

void lecteur::creeLSysteme(){
  l_syst = l_systeme(angle, axio, collectionRegle);
}

l_systeme lecteur::getL_systeme(){
  return l_syst;
}

void lecteur::affiche(){
  for(iterSymbole = collectionSymbAxiome.begin(); iterSymbole<collectionSymbAxiome.end(); iterSymbole++){
    symbole s = *iterSymbole;
    cout<<"le symbole axiome est ";
    s.lireSymbole();
    regle *tmp = s.getRegle();
    regle r = *tmp;
    r.affiche();
  }
}

//  int main(){
//    lecteur l = lecteur("test");
//    l.appliqueRegleAxiome();
//    l.creeLSysteme();
//  }

