//****************Les symboles LOGO***************************
//F -> aller tout droit en traçant                           *
//f -> aller tout droit sans tracer                          *
//+ -> tourner à droite de la valeur de l'angle du l_systeme *
//- -> tourner à gauche de la valeur de l'angle              *
//| -> tourner de 180°                                       *
//[ -> mémoriser les coordonnées actuelles                   *
//] -> revenir aux coordonnées mémorisées                    *
//************************************************************


#include "interpreteGraphique.h"
double pi = 3.14159;

//constructeur qui initialise representable à true 
//si le symbole est un symbole logo
interpreteGraphique::interpreteGraphique(string s){
  lettre = s;
  if((s == "F")||(s == "f")||(s == "-")||(s == "+")||(s == "[")||(s == "]")){
  representable = true;
  }
  else{
    representable = false;
  }
}

bool interpreteGraphique::estRepresentable(){
  return representable;
}

string interpreteGraphique::getLettre(){
  return lettre;
}

float interpreteGraphique::getNouvelX(){
  return nouvelX;
}

float interpreteGraphique::getNouvelY(){
  return nouvelY;
}

float interpreteGraphique::getNvAngle(){
  return angleNv;
}

float interpreteGraphique::getMemX(){
  return positionMemX;
}
float interpreteGraphique::getMemY(){
  return positionMemY;
}

//permet de calculer la prochaine coordonnée de x
void interpreteGraphique::calculNouvelX(float x, float angle, int trait){
  tailleTrait = trait;
  double a = (double)angle;
  if((angle>= 0)&&(angle<=90)){
    double a0 = (a * (pi/180)); //transformation en radians
    if(sin(a0)>=0){
      nouvelX = x + (tailleTrait * sin(a0));
    }  
    else{
      nouvelX = x - (tailleTrait * sin(a0));
    }
  }
  
  if((angle> 90)&&(angle<= 180)){
    double a1 = ((180 - a) * (pi/180));
    if(sin(a1)>=0){
      nouvelX = x + (tailleTrait * sin(a1));
    }  
    else{
      nouvelX = x - (tailleTrait * sin(a1));
    }
  }
  
  if((angle> 180)&&(angle<= 270)){
    double a2 = ((a - 180) * (pi/180));
    if(sin(a2)>=0){
      nouvelX = x - (tailleTrait * sin(a2));
    }  
    else{
      nouvelX = x + (tailleTrait * sin(a2));
    }
  }
  
  if(angle> 270){
    double a3 = ((360 - a) * (pi/180));
    if(sin(a3)>=0){
      nouvelX = x - (tailleTrait * sin(a3));
    }  
    else{
      nouvelX = x + (tailleTrait * sin(a3));
    }
  }
}


void interpreteGraphique::calculNouvelY(float y, float angle, int trait){
  tailleTrait = trait;
  double a = (double)angle;
  if((angle>= 0)&&(angle<=90)){
    double a0 = (a * (pi/180));
    if(cos(a0)>=0){
      nouvelY = y - (tailleTrait * cos(a0));
    }  
    else{ 
      nouvelY = y + (tailleTrait * cos(a0));
    }
  }
  
  if((angle> 90)&&(angle<= 180)){
    double a1 = ((180 - a)* (pi/180));
    if(cos(a1)>=0){
      nouvelY = y + (tailleTrait * cos(a1));
    }  
    else{
      nouvelY = y - (tailleTrait * cos(a1));
    }
  }
  
  if((angle> 180)&&(angle<= 270)){
    double a2 = ((a - 180)* (pi/180));
    if(cos(a2)>=0){
      nouvelY = y + (tailleTrait * cos(a2));
    }  
    else{
      nouvelY = y - (tailleTrait * cos(a2));
    }
  }
  
  if(angle> 270){
    double a3 = ((360 - a) * (pi/180));
    if(cos(a3)>=0){
      nouvelY = y - (tailleTrait * cos(a3));
    }  
    else{
      nouvelY = y + (tailleTrait * cos(a3));
    }
  }
}


//permet de faire une action si le symbole est un symbole logo
void interpreteGraphique::dessine(zoneDessin &zone, int angle, float angle2, float x, float y, float posMemX, float posMemY, int trait){
  angleNv = angle2;
  positionMemX = posMemX;
  positionMemY = posMemY;
  nouvelX = x;
  nouvelY = y;
  
  //si c'est un symbole logo alors on interprete la signifiaction
  //de ce symbole sinon on ne fait rien
  if(representable){ 
    if(lettre == "F"){
      calculNouvelX(x,angle2,trait);
      calculNouvelY(y,angle2,trait);
      zone.draw_ligne(x,y,nouvelX,nouvelY);
      cout<<"on va a la position en dessinant: ("<<x<<","<<y<<","<<nouvelX<<","<<nouvelY<<")"<<endl;
    }
    
    if(lettre == "f"){
      calculNouvelX(x,angle2,trait);
      calculNouvelY(y,angle2,trait);
      cout<<"on va a la position : ("<<x<<","<<y<<","<<nouvelX<<","<<nouvelY<<")"<<endl;
    }
    
    if(lettre == "+"){
      angleNv += angle;
      if(angleNv>360){
	angleNv-=360;
      }
      cout<<"Barre à tribord, cap au "<<angleNv<<" mousaillon!"<<endl;
    }
    
    if(lettre == "-"){
      angleNv -= angle;
      if(angleNv<0){
	angleNv+=360;
      }
      cout<<"Barre à babord, cap au "<<angleNv<<" mousaillon!"<<endl;
    }
    
    if(lettre == "|"){
      if(angle>180){
	angleNv-=180;
      }
      if(angle<180){
	angleNv+=180;
      }
      cout<<"Demi tour toute, cap au "<<angleNv<<endl;
    }
    
    if(lettre == "["){
      positionMemX = x;
      positionMemY = y;
      cout<<"Retiens la position, il va falloir revenir : ("<<x<<","<<y<<")"<<endl;
    }
    if(lettre == "]"){
      nouvelX = positionMemX;
      nouvelY = positionMemY;
      cout<<"Reviens a la position retenue mousaillon! Oui capitaine: ("<<nouvelX<<","<<nouvelY<<")"<<endl;
    }
  }
}
