//---------------------------------------------------------------------------
#include <sstream>
#include "hub/err.h"
#include "kiss_import.h"

using namespace hub;
// --------------------------------------------------------------------------------------
void KissImpLang::enregister (void) {
    registry_.push_back((new KissImpLang ("KISS"))->addSuffix (".kiss")->addSuffix (".kiss2"));
}
// --------------------------------------------------------------------------------------
void KissImpLang::getDefaults (hub::Hub* h, const std::string& fn, Import::Destinations& dflt) {
    dflt.clear();                                      
    dflt.push_back (Location (h, "Destination"));       // Destination #0: Model
    dflt[0][Location::lib].oblig  (true); 
    dflt[0][Location::lib].content  ("kiss_library"); 
    dflt[0][Location::ent].locked (true);               // keep the entity name empty & locked
                                                        // we dont know what CreateUnique returns
    dflt[0][Location::arch].oblig (true); 
    dflt[0][Location::arch].content ("kiss");
}
//---------------------------------------------------------------------------
void KissImport::operator() (std::istream* istr, const std::string& fn, Import::Destinations& dests) {
    dest_ = &(dests[0]);
    (*dest_) [Location::ent].content (ImportLang::bareName(fn));
    parser_.run (*istr, fn);
}
//---------------------------------------------------------------------------
void KissImport::haveDescription(int inpN, int outN, std::list<StateTableRow>& stateTable,
              std::string& resetState, int stateCount, int productCount) {

    Name portName;
    BehavAgent *agent;
    BehavArch* behavArch;

    agent = new FsmAgent(inpN,outN,stateTable, resetState, stateCount, productCount);
    dest_->theArch (dest_->theLib()->FindArch(*agent, "KISS mealy"));

    if (dest_->theArch()) {      // found by behaviour
        dest_->theEntity (dest_->theArch()->Parent());
    } else {                    //this sequential circuit is not in library, so we have to create a new entity
                                //create new entity and architecture
        dest_->CreateUniqueEntity <BehavArch> ();
        behavArch = dest_->theArch()->Impl <BehavArch> ();

                        // behavioral decription
        behavArch->description (agent->CreateDescr());

                        //create inputs
        for (int i = 1; i <= inpN; ++i) {
            portName.cons ("i", i);
            dest_->theEntity()->CreateNextPort (portName, PortProto::inDir);
        }

                        //create outputs
        for (int i = 1; i <= outN; ++i) {
            portName.cons ("o", i);
            dest_->theEntity()->CreateNextPort(portName, PortProto::outDir);
        }
    }
}
//---------------------------------------------------------------------------
