/*******************************************************************************
 * Copyright (c) 2015-2017 ONERA and IRT AESE (IRT Saint Exupéry).
 * Ce logiciel est la propriété de l’ONERA (the French Aerospace Lab) et de
 * l'IRT AESE (IRT Saint Exupéry).
 * Tous droits réservés.
 *
 * Ce programme et les éléments qui l'accompagnent sont mis à disposition
 * aux conditions définies par le contrat de licence logiciel CeCILL-C soumise
 * au droit français et respectant les principes de diffusion des logiciels libres.
 * Vous pouvez utiliser, modifier et/ou redistribuer ce programme
 * sous les conditions de la licence CeCILL-C (http://www.cecill.info).

 * This software is property of ONERA (the French Aerospace Lab) and of
 * the IRT AESE (IRT Saint Exupéry).
 * All rights reserved.
 *
 * This program and the accompanying materials are made available under
 * the terms of the CeCILL-C license under French law and
 * abiding by the rules of distribution of free software.
 * You can  use, modify and/ or redistribute the software under the terms of
 * the CeCILL-C license (http://www.cecill.info).
 *
 * Contributeurs/contributors:
 *     Guillaume Infantes (ONERA - Centre de Toulouse) - initial API and implementation
 *     Alexandre Albore (IRT Saint-Exupéry) - Altarica/Fiacre translation
 *
 *******************************************************************************/

#include "ExpressionCASE.hh"


using namespace epoch::altarica;


void ExpressionCASE::addCase(Expression * c, Expression* t) {
    cond_.push_back(c);
    then_.push_back(t);
}

void ExpressionCASE::addElse(Expression * t) {
    else_ = t;
}



ExpressionCASE * ExpressionCASE::clone() const {
    ExpressionCASE * nec = new ExpressionCASE();
    typename std::vector<Expression*>::const_iterator i = cond_.begin();
    typename std::vector<Expression*>::const_iterator j = then_.begin();
    for (; i!= cond_.end(); ++i, ++j) 
	nec->addCase((*i)->clone(), (*j)->clone());
    if (else_ != NULL)
	nec->addElse(else_->clone());
    return nec;
}


std::string ExpressionCASE::toString() const {
    std::string s;
    s+= "case\n";
    std::vector<Expression*>::const_iterator ci = cond_.begin();
    std::vector<Expression*>::const_iterator ti = then_.begin();
    
    for (; ci != cond_.end(); ++ci, ++ti) 
	s+= (*ci)->toString() + " -> " + (*ti)->toString() + "\n";
    if (else_ != NULL)
	s+= " else: " + else_->toString();
    s+= " end";
    return s;
}

std::string ExpressionCASE::toString(const Node *n, const AltaricaModel& am) const {
    std::string s;
    s+= "case\n";
    std::vector<Expression*>::const_iterator ci = cond_.begin();
    std::vector<Expression*>::const_iterator ti = then_.begin();

    for (; ci != cond_.end(); ++ci, ++ti) {
	    if (ci == cond_.begin())
		    s += "\n";
	    else
		    s+=  "|\n";

	    s += (*ci)->toString(n, am) + " -> " + (*ti)->toString(n, am) ;
    }
    if (else_ != NULL)
    	s+= "|\n any -> " + else_->toString(n, am);
    s+= " end";
    return s;
}

std::string ExpressionCASE::toString(const Node *n, const std::pair<std::string,std::string> *f, const AltaricaModel& am) const {
    std::string s;
    s+= "case v of\n";
    std::vector<Expression*>::const_iterator ci = cond_.begin();
    std::vector<Expression*>::const_iterator ti = then_.begin();

    for (; ci != cond_.end(); ++ci, ++ti) {
	    if (ci == cond_.begin())
		    s += "\t\t";
	    else
		    s+=  "\n\t\t|";

    	s+=  (*ci)->toString(n, f, am) + " -> " + (*ti)->toString(n, f, am);
    }
    if (else_ != NULL)
    	s+= "\n\t\t| any -> " + else_->toString(n, f, am);

    s+= " end";
    return s;
}

/// Example :  t := case r|q -> 2
///          p -> 5
/// ==> if ( r|q ) then t := 2
///      else if p then t := 5
std::string ExpressionCASE::toFiacre(const Node *n, const std::pair<std::string,std::string> *f, const AltaricaModel& am, std::string head) const {
	    std::string s;
	    std::vector<Expression*>::const_iterator ci = cond_.begin();
	    std::vector<Expression*>::const_iterator ti = then_.begin();

	    for (; ci != cond_.end(); ++ci, ++ti) {
		    if (ci != cond_.begin())
			    s+=  "\n\tels";

	    	s+= "if " + (*ci)->toString(n, f, am) + " then " + head + (*ti)->toString(n, f, am);
	    }
	    if (else_ != NULL)
	    	s+= "\n\telse  " + head + else_->toString(n, f, am);

	    s+= " end";
	    return s;
}

ExpressionCASE::~ExpressionCASE() {
    for (std::vector<Expression*>::iterator i =
	     cond_.begin(); i!= cond_.end(); ++i)
	delete *i;
    for (std::vector<Expression*>::iterator i =
	     then_.begin(); i!= then_.end(); ++i)
	delete *i;
    delete else_;
}

