package jycessing.mode;

import java.io.File;
import java.io.IOException;

import jycessing.mode.run.SketchServiceManager;
import processing.app.Base;
import processing.app.Editor;
import processing.app.EditorState;
import processing.app.Formatter;
import processing.app.Mode;
import processing.app.syntax.TokenMarker;

public class PythonMode extends Mode {

  public static final boolean VERBOSE = Boolean.parseBoolean(System.getenv("VERBOSE_PYTHON_MODE"));
  public static final boolean SKETCH_RUNNER_FIRST = Boolean.parseBoolean(System
      .getenv("SKETCH_RUNNER_FIRST"));

  private final FormatServer formatServer;
  private final SketchServiceManager sketchServiceManager;

  public PythonMode(final Base base, final File folder) {
    super(base, folder);
    formatServer = new FormatServer(folder);
    formatServer.start();
    sketchServiceManager = new SketchServiceManager(this);
    sketchServiceManager.start();
  }

  @Override
  public String getTitle() {
    return "Python";
  }

  @Override
  public Editor createEditor(final Base base, final String path, final EditorState state) {
    return new PyEditor(base, path, state, this);
  }

  @Override
  public String getDefaultExtension() {
    return "pde";
  }

  @Override
  public String[] getExtensions() {
    return new String[] { "pde", "py", "pyde" };
  }

  @Override
  public String[] getIgnorable() {
    return new String[] {};
  }

  Formatter getFormatter() {
    return formatServer;
  }

  @Override
  protected void loadKeywords(File keywordFile) throws IOException {
    loadKeywords(keywordFile, "//");
  }

  @Override
  public TokenMarker createTokenMarker() {
    return new PythonTokenMarker();
  }

  public SketchServiceManager getSketchServiceManager() {
    return sketchServiceManager;
  }

  public void handleSketchStopped() {
    base.getActiveEditor().deactivateRun();
  }

  public void handleSketchException(Exception e) {
    base.getActiveEditor().statusError(e);
  }
}

@@ -987,16 +987,21 @@
  %22Python
+ Mode
 %22;%0A  %7D%0A%0A
@@ -1224,16 +1224,17 @@
 eturn %22p
+y
 de%22;%0A  %7D
@@ -1311,16 +1311,17 @@
 g%5B%5D %7B %22p
+y
 de%22, %22py
@@ -1325,16 +1325,8 @@
 %22py%22
-, %22pyde%22
  %7D;%0A

Change default extension to pyde.