/*******************************************************************************
 * Copyright (c) 2010-2017, IncQueryLabs Ltd.
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Eclipse Public License v1.0 
 * which accompanies this distribution, and is available at 
 * http://www.eclipse.org/legal/epl-v10.html 
 *
 * Contributors: 
 *     Peter Lunk - initial API and implementation 
 *******************************************************************************/
package hu.bme.mit.massif.simulink.cli;

import java.io.File;

import org.eclipse.viatra.query.runtime.exception.ViatraQueryException;

import br.com.embraer.massif.commandevaluation.commands.MatlabController;
import hu.bme.mit.massif.communication.command.MatlabCommandFactory;
import hu.bme.mit.massif.communication.localscript.LocalScriptEvaluator;
import hu.bme.mit.massif.simulink.SimulinkModel;
import hu.bme.mit.massif.simulink.api.Exporter;
import hu.bme.mit.massif.simulink.api.exception.SimulinkApiException;
import hu.bme.mit.massif.simulink.api.layout.DummyExporterLayoutProvider;
import hu.bme.mit.massif.simulink.api.layout.IExporterLayoutProvider;
import hu.bme.mit.massif.simulink.cli.util.CLIInitializationUtil;
import hu.bme.mit.massif.simulink.cli.util.CLISimulinkAPILogger;

/**
 * This class provides functions to load Massif EMF models into MATLAB
 * 
 * @author Peter Lunk
 */
public class CLIMatlabCreator {
    
    private boolean debugMode = false;
    
    public void setDebugMode(boolean debugMode) {
        this.debugMode = debugMode;
    }

    public void createMatlabModel(String modelName, String modelPath) throws SimulinkApiException, ViatraQueryException {
        createMatlabModel(modelName, modelPath, "slx", new DummyExporterLayoutProvider());
    }
    
    public void createMatlabModel(String modelName, String modelPath, String extension) throws SimulinkApiException, ViatraQueryException {
        createMatlabModel(modelName, modelPath, extension, new DummyExporterLayoutProvider());
    }

    public void createMatlabModel(String modelName, String modelPath, String extension, IExporterLayoutProvider layoutProvider) throws SimulinkApiException, ViatraQueryException {
        CLIInitializationUtil.setupEnvironment();
        CLISimulinkAPILogger logger = new CLISimulinkAPILogger();
 
        MatlabController controller = new MatlabController();
        controller.setDebug(debugMode);
        LocalScriptEvaluator localScriptEvaluator = new LocalScriptEvaluator(controller);
        Exporter exporter = new Exporter(logger);
        exporter.setLayoutProvider(layoutProvider);
        SimulinkModel loadedModel;
        logger.debug("Loading Simulink model...");
        loadedModel = exporter.loadSimulinkModel(modelPath +File.separator+ modelName);
        logger.debug("Simulink model loaded");
        MatlabCommandFactory commandFactory = new MatlabCommandFactory(localScriptEvaluator);
        exporter.export(loadedModel, commandFactory);
        String fqn = loadedModel.getSimulinkRef().getFQN();
        exporter.saveSimulinkModel(fqn, extension);
    }

}

@@ -2968,24 +2968,220 @@
 Evaluator);%0A
+        logger.debug(%22Loading model into MATLAB...%22);%0A%0A        Thread thread = new Thread(new Runnable() %7B%0A%0A            @Override%0A            public void run() %7B%0A                try %7B%0A            
         expo
@@ -3222,24 +3222,32 @@
 ctory);%0A
+%0A       
         
 String f
@@ -3238,16 +3238,21 @@
         
+     
 String f
@@ -3291,24 +3291,36 @@
 ).getFQN();%0A
+            
         expo
@@ -3359,16 +3359,229 @@
 nsion);%0A
+                %7D catch (SimulinkApiException e) %7B%0A                    e.printStackTrace();%0A                %7D%0A%0A            %7D%0A        %7D);%0A%0A        thread.start();%0A%0A        logger.debug(%22Model loaded into MATLAB%22);%0A
     %7D%0A%0A%7D

Revert "#181 Make CLIMatlabCreator synchronous"