/*******************************************************************************
 * 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 "AltaricaInteractive.hh"
#include "AltaricaState.hh"
#include "AltaricaModel.hh"
#include "MetaEvent.hh"
#include <sstream>
#include <iostream>

using namespace epoch::altarica;

void ::epoch::altarica::interactiveExecute(AltaricaState *cstate, AltaricaModel * amodel, bool printFlow, bool printNULL, bool printDomain) {
  int choice;
  while (true) {
    std::vector<epoch::altarica::MetaEvent*> * mev =  amodel->getEnabledMetaEventsAll(cstate);
    std::cout<<"\n # metaEvents:  "<< mev->size() << std::endl;
    for (int i=0; i<mev->size(); ++i) {
      std::cout << " MEV " << i << ": " << (*mev)[i]->toString();
    }
    do {
      std::cout << "\n\ninput MEV number (-1 exits): ";
      std::string l;
      std::getline(std::cin,l);
      std::stringstream ss(l);
      ss >> choice;
      if (ss.fail()) {
        if (std::cin.eof()){
          std::cout << std::endl;
          exit(0);
        }
        choice = std::numeric_limits<int>::max();
      }
    } while (choice > ((int)mev->size() - 1));
    
    if (choice <= -1)
      exit(0);
    
    std::cout << "Executing MEV: "<< (*mev)[choice]->toString() << std::endl;
    std::cout << "state reached:" << std::endl;
    cstate = amodel->getNextState(cstate, (*mev)[choice]);
    if (printFlow)
      amodel->evaluateFlowVars(cstate);
    std::cout << cstate->toString(true,printFlow,printNULL,printDomain);
  }

}
