package org.myrobotlab.service;

import java.util.Map;
import java.util.TreeMap;

import org.myrobotlab.framework.Service;
import org.myrobotlab.framework.ServiceType;
import org.myrobotlab.framework.repo.Repo;
import org.myrobotlab.io.FileIO;
import org.myrobotlab.logging.Level;
import org.myrobotlab.logging.LoggerFactory;
import org.myrobotlab.logging.LoggingFactory;
import org.slf4j.Logger;

public class Intro extends Service {

  private static final long serialVersionUID = 1L;

  public final static Logger log = LoggerFactory.getLogger(Intro.class);

  public class TutorialInfo {

    public String id;
    public String title;
    public boolean isInstalled;
    public String[] servicesRequired;
    public String script;

  }

  Map<String, TutorialInfo> tutorials = new TreeMap<>();

  public Intro(String n, String id) {
    super(n, id);
    try {
      Runtime runtime = Runtime.getInstance();
      Repo repo = runtime.getRepo();
      TutorialInfo tuto = new TutorialInfo();
      tuto.id = "servo-hardware";
      tuto.title = "Servo with Arduino Hardware";
      tuto.servicesRequired = new String[] { "Servo", "Arduino" };
      tuto.isInstalled = repo.isInstalled(Servo.class) && repo.isInstalled(Arduino.class);

      tuto.script = getResourceAsString(Servo.class, "Servo.py"); //FileIO.toString(getResource(Servo.class,"Servo.py"));
      tutorials.put(tuto.title, tuto);
    } catch (Exception e) {
      log.error("Intro constructor threw", e);
    }
  }

  /**
   * This static method returns all the details of the class without it having
   * to be constructed. It has description, categories, dependencies, and peer
   * definitions.
   * 
   * @return ServiceType - returns all the data
   * 
   */
  static public ServiceType getMetaData() {

    ServiceType meta = new ServiceType(Intro.class);
    meta.addDescription("Introduction to MyRobotlab");
    meta.setAvailable(true);
    meta.addCategory("general");
    return meta;
  }

  public void checkInstalled(String forTutorial, String serviceType) {
    Runtime runtime = Runtime.getInstance();
    Repo repo = runtime.getRepo();

    TutorialInfo tutorial = new TutorialInfo();
    tutorial.title = forTutorial;
    tutorial.isInstalled = repo.isInstalled(serviceType);
  }

  public static void main(String[] args) {
    try {

      LoggingFactory.init(Level.INFO);
      
      Runtime.main(new String[] { "--interactive", "--id", "admin", "-s", "intro", "Intro", "python", "Python" });
      
      // Arduino arduino = (Arduino)Runtime.start("arduino", "Arduino");
      WebGui webgui = (WebGui) Runtime.create("webgui", "WebGui");
      // webgui.setSsl(true);
      webgui.autoStartBrowser(false);
      webgui.setPort(8888);
      webgui.startService();


    } catch (Exception e) {
      log.error("main threw", e);
    }
  }
}

@@ -2329,32 +2329,867 @@
 eType);%0D%0A  %7D%0D%0A%0D%0A
+  /**%0D%0A%09 * This method will load a python file into the python interpreter.%0D%0A%09 */%0D%0A%09public static boolean loadFile(String file) %7B%0D%0A%09%09File f = new File(file);%0D%0A%09%09Python p = (Python) Runtime.getService(%22python%22);%0D%0A%09%09log.info(%22Loading  Python file %7B%7D%22, f.getAbsolutePath());%0D%0A%09%09if (p == null) %7B%0D%0A%09%09%09log.error(%22Python instance not found%22);%0D%0A%09%09%09return false;%0D%0A%09%09%7D%0D%0A%09%09String script = null;%0D%0A%09%09try %7B%0D%0A%09%09%09script = FileIO.toString(f.getAbsolutePath());%0D%0A%09%09%7D catch (IOException e) %7B%0D%0A%09%09%09log.error(%22IO Error loading file : %22, e);%0D%0A%09%09%09return false;%0D%0A%09%09%7D%0D%0A%09%09// evaluate the scripts in a blocking way.%0D%0A%09%09boolean result = p.exec(script, true);%0D%0A%09%09if (!result) %7B%0D%0A%09%09%09log.error(%22Error while loading file %7B%7D%22, f.getAbsolutePath());%0D%0A%09%09%09return false;%0D%0A%09%09%7D else %7B%0D%0A%09%09%09log.debug(%22Successfully loaded %7B%7D%22, f.getAbsolutePath());%0D%0A%09%09%7D%0D%0A%09%09return true;%0D%0A%09%7D%0D%0A%0D%0A
   public static 

add loadFile to Intro