'use babel';
'use strict';

// IMPORT LIBS
import {Dom} from './dom';
import {Utility} from './util';

// PARSE SETTINGS
const Settings = {

  init: function() {

    console.log('Seti UI Loaded', atom.config.get('seti-ui.ui'));

    // TAB SIZE
    this.tabSize(atom.config.get('seti-ui.ui.compactView'));

    // DISPLAY IGNORED FILES
    this.ignoredFiles(atom.config.get('seti-ui.ui.displayIgnored'));

    // DISPLAY FILE ICONS
    this.fileIcons(atom.config.get('seti-ui.ui.fileIcons'));

    // HIDE TABS
    this.hideTabs(atom.config.get('seti-ui.ui.hideTabs'));

    // SET THEME COLOR
    this.setTheme(atom.config.get('seti-ui.ui.themeColor'), false);
    atom.config.onDidChange('seti-ui.ui.themeColor', (value) => {
      this.setTheme(value.newValue, true);
    });

  },

  package: atom.packages.getLoadedPackage('seti-ui'),

  refresh: function() {
    console.log('reload theme');
    this.package.deactivate();
    setImmediate(() => this.package.activate());
  },

  setTheme: function( theme, reload ) {

    var self = this;

    atom.config.set('seti-ui.ui.themeColor', theme);

    let fs = require('fs'),
        path = require('path'),
        pkg = atom.packages.getLoadedPackage('seti-ui'),
        themeData = '@seti-primary: @' + theme.toLowerCase() + ';';
        themeData = themeData + '@seti-primary-text: @' + theme.toLowerCase() + '-text;';
        themeData = themeData + '@seti-primary-highlight: @' + theme.toLowerCase() + '-highlight;';

    fs.writeFile(pkg.path + '/styles/user-theme.less', themeData, function(err) {
        if(err) {
          alert('Unable to save configuration file: ' + pkg.path + '/styles/user-theme.less');
        } else {
          if (reload) {
            self.refresh();
          }
        }
    });
  },

  tabSize: function( val ) {
    Utility.applySetting({
      action: 'addWhenTrue',
      config: 'seti-ui.ui.compactView',
      el: [
        'atom-workspace-axis.vertical .tab-bar',
        'atom-workspace-axis.vertical .tabs-bar',
        'atom-panel-container.left',
        'atom-panel-container.left .project-root > .header',
        '.entries.list-tree'
      ],
      className: 'seti-compact',
      val: val,
      cb: this.tabSize
    });
  },

  hideTabs: function( val ) {
    Utility.applySetting({
      action: 'addWhenTrue',
      config: 'seti-ui.ui.hideTabs',
      el: [
        '.tab-bar',
        '.tabs-bar'
      ],
      className: 'seti-hide-tabs',
      val: val,
      cb: this.hideTabs
    });
  },

  fileIcons: function( val ) {
    Utility.applySetting({
      action: 'addWhenTrue',
      config: 'seti-ui.ui.fileIcons',
      el: [
        'atom-workspace'
      ],
      className: 'seti-icons',
      val: val,
      cb: this.fileIcons
    });
  },

  // DISPLAY IGNORED FILES
  ignoredFiles: function( val ) {
    Utility.applySetting({
      action: 'addWhenFalse',
      config: 'seti-ui.ui.displayIgnored',
      el: [
        '.file.entry.list-item.status-ignored',
        '.directory.entry.list-nested-item.status-ignored'
      ],
      className: 'seti-hide',
      val: val,
      cb: this.ignoredFiles
    });
  }

};

export {Settings};

@@ -36,16 +36,47 @@
 RT LIBS%0A
+import config from './config';%0A
 import %7B
@@ -678,16 +678,23 @@
 Color'),
+ false,
  false);
@@ -795,16 +795,32 @@
 ewValue,
+ value.oldValue,
  true);%0A
@@ -1060,16 +1060,26 @@
 ( theme,
+ previous,
  reload 
@@ -1091,11 +1091,11 @@
     
-var
+let
  sel
@@ -1106,72 +1106,59 @@
 this
-;%0A
+,
 %0A    
-atom.config.set('seti-ui.ui.themeColor', theme);%0A%0A
+    el = Dom.query('atom-workspace'),%0A   
     
-let
  fs 
@@ -1522,16 +1522,70 @@
 ght;';%0A%0A
+    atom.config.set('seti-ui.ui.themeColor', theme);%0A%0A
     fs.w
@@ -1669,16 +1669,17 @@
      if(
+!
 err) %7B%0A 
@@ -1691,109 +1691,173 @@
     
-alert('Unable to save configuration file: ' + pkg.path + '/styles/user-theme.less');%0A        %7D else %7B
+if (previous) %7B%0A            el.classList.remove('seti-theme-' + previous.toLowerCase());%0A          %7D%0A          el.classList.add('seti-theme-' + theme.toLowerCase());
 %0A   

add theme color a class to seti-syntax can use it