// https://github.com/ajaxorg/ace/wiki/Configuring-Ace

var PawSettings = (function () {
  var configFile  = path.join(process.cwd(), 'data', 'config.json');
  var modes       = {
        clj:        { m: 'ace/mode/clojure',        n: 'Clojure' },
        coffee:     { m: 'ace/mode/coffee',         n: 'CoffeeScript' },
        css:        { m: 'ace/mode/css',            n: 'CSS' },
        diff:       { m: 'ace/mode/diff',           n: 'Diff' },
        erl:        { m: 'ace/mode/erlang',         n: 'Erlang' },
        go:         { m: 'ace/mode/golang',         n: 'Golang' },
        gitignore:  { m: 'ace/mode/gitignore',      n: 'Gitignore' },
        haml:       { m: 'ace/mode/haml',           n: 'HAML' },
        hbs:        { m: 'ace/mode/handlebars',     n: 'Handlebars' },
        html:       { m: 'ace/mode/html',           n: 'HTML' },
        jade:       { m: 'ace/mode/jade',           n: 'Jade' },
        java:       { m: 'ace/mode/java',           n: 'Java' },
        js:         { m: 'ace/mode/javascript',     n: 'JavaScript' },
        json:       { m: 'ace/mode/json',           n: 'JSON' },
        jsp:        { m: 'ace/mode/jsp',            n: 'JSP' },
        less:       { m: 'ace/mode/less',           n: 'Less' },
        lua:        { m: 'ace/mode/lua',            n: 'Lua' },
        md:         { m: 'ace/mode/markdown',       n: 'Markdown' },
        pl:         { m: 'ace/mode/perl',           n: 'Perl' },
        php:        { m: 'ace/mode/php',            n: 'PHP' },
        properties: { m: 'ace/mode/properties',     n: 'Properties' },
        py:         { m: 'ace/mode/python',         n: 'Python' },
        rb:         { m: 'ace/mode/ruby',           n: 'Ruby' },
        sass:       { m: 'ace/mode/sass',           n: 'SASS' },
        scala:      { m: 'ace/mode/scala',          n: 'Scala' },
        scss:       { m: 'ace/mode/scss',           n: 'SCSS' },
        soy:        { m: 'ace/mode/soy_tamplate',   n: 'Soy' },
        svg:        { m: 'ace/mode/svg',            n: 'SVG' },
        txt:        { m: 'ace/mode/plain_text',     n: 'Plain Text' },
        xml:        { m: 'ace/mode/xml',            n: 'XML' },
        yml:        { m: 'ace/mode/yaml',           n: 'YAML' }
      };
  var defaultConfiguration = {
        theme: 'ace/theme/paw',
        fontSize: 16,
        editor: {
          dragEnabled: false,
          selectionStyle: 'text',
          highlightActiveLine: true,
          cursorStyle: "smooth",
          autoScrollEditorIntoView: true,
          enableSnippets: true,
          spellcheck: true,
          enableBasicAutocompletion: true,
          enableLiveAutocompletion: true
        },
        session: {
          enableEmmet: true,
          tabSize: 4,
          wrap: true,
          useSoftTabs: true
        }
      };
  var config;
  var editor;

  function readConfig() {
    fs.readFile(configFile, 'utf8', function (err, data) {
      if (err) {
        config = defaultConfiguration;
      } else {
        config = JSON.parse(data);
      }

      editor.setTheme(config.theme);
      editor.setFontSize(config.fontSize);
      editor.session.setMode(modes.txt.m);

      PawStatusBar.setMode(modes.txt);
      PawStatusBar.setTabSize(config.session.tabSize);

      for (var k in config.editor) {
        if (config.editor.hasOwnProperty(k)) {
          editor.setOption(k, config.editor[k]);
        }
      }

      for (var k in config.session) {
        if (config.session.hasOwnProperty(k)) {
          editor.getSession().setOption(k, config.session[k]);
        }
      }
    });
  }

  function saveConfig() {

  }

  function getMode(filename) {
    var ext = (/[.]/.exec(filename)) ? /[^.]+$/.exec(filename)[0] : undefined;

    if (ext && modes[ext]) {
      return modes[ext];
    } else {
      return modes.txt;
    }
  }

  function getModeByName(mode) {
    var m;

    if (mode === 'auto') {
      m = getMode(PawEditor.getSelectedFile());
    } else {
      for (var k in modes) {
        if (modes.hasOwnProperty(k) && modes[k].m === mode) {
          m = modes[k];
        }
      }
    }

    if (!m) {
      m = modes.txt;
    }

    return m;
  }

  function setModeByName(mode) {
    var m = getModeByName(mode);

    PawEditor.setSessionMode(m);
    PawStatusBar.setMode(m);
  }

  function init() {
    editor = PawEditor.getEditor();
    readConfig();
  }

  return {
    init:           init,
    getMode:        getMode,
    setModeByName:  setModeByName
  }
}());
