'use strict';
import angular from 'angular';
import $ from 'jquery';
import db from '../db';

const index = {
  'folderList': '.row > .col-md-6:first-child > ul',
  'taskList': '.row > .col-md-6:nth-child(2) > ul',
  'editTaskBody': '#editTaskModal > .modal-dialog > .modal-content > .container-fluid > .row > .modal-body'
}

const TaskCtrl = ($scope) => {

  setTimeout(() => {
    let folder = getActiveFolder();
    return configHelper
    .getTasks(folder.name, folder.path)
    .then(tasks => {
      $scope.tasks = tasks;
      $scope.safeApply();
    })
    .catch(err => {
      console.log(err.message);
    });
  }, 0);

  $('form').submit((event) => {
    event.preventDefault();
  })

  $('#taskSave').on('click', (event) => {

    let folder = getActiveFolder();
    let matchAll = ($('#taskMatch').val() === 'all');

    var values = {
      'matchAll':   matchAll
    };


    values.rules = [];

    $.each($('#formTask').serializeArray(), function(i, field) {
      values[field.name] = field.value;
    });

    $('li > select').parent().each((index, value) => {

      let selects = $(value).find('select');
      values.rules.push({
        'type':       $(selects[0]).val(),
        'comparator': $(selects[1]).val(),
        'reference':  $(value).find('input').val()
      });
    });
    console.log(folder);
    return configHelper
    .saveTask(folder.name, folder.path, values)
    .then(() => {
      console.log('Task Saved!');
    })
    .catch(err => {
      throw err;
    });

  });


  $('form').submit((event) => {
    event.preventDefault();
  })
  
  $('#taskSave').on('click', (event) => {

    let folder = getActiveFolder();
    let matchAll = ($('#taskMatch').val() === 'all');

    var values = {
      'folderName': folder.name,
      'folderPath': folder.path,
      'matchAll':   matchAll
    };


    values.rules = [];
    $.each($('#formTask').serializeArray(), function(i, field) {
        values[field.name] = field.value;
    });

    $('li > select').parent().each((index, value) => {

      let selects = $(value).find('select');
      values.rules.push({
        'type':       $(selects[0]).val(),
        'comparator': $(selects[1]).val(),
        'reference':  $(value).find('input').val()
      });
    });

    ipc.send('tasks', {query: 'add', values});

  });


  $scope.safeApply = function(fn) {
    var phase = this.$root.$$phase;
    if(phase === '$apply' || phase === '$digest') {
      if(fn && (typeof(fn) === 'function')) {
        fn();
      }
    } else {
      this.$apply(fn);
    }
  };
}

function getActiveFolder() {
  console.log($(index.folderList).children().find('.activeFolder'));
  let folderName = $('.activeFolder').find('h3').html();
  let folderPath = $('.activeFolder').find('p').find('i').html();
  setTimeout(() => {console.log($('.activeFolder'))}, 0);

  return {
    'name': folderName,
    'path': folderPath
  }
}

module.exports = TaskCtrl;
@@ -85,16 +85,46 @@
 '../db';
+%0Aimport uuid from 'node-uuid';
 %0A%0Aconst 
@@ -394,21 +394,26 @@
 Timeout(
-() =%3E
+function()
  %7B%0A    l
@@ -451,119 +451,59 @@
     
-return configHelper%0A    .getTasks(folder.name, folder.path)%0A    .then(tasks =%3E %7B%0A      $scope.tasks = 
+$scope.tasks = db('folders').find(%7Bfolder%7D).
 tasks;%0A
-  
     
@@ -528,147 +528,14 @@
 ;%0A  
-  %7D)%0A    .catch(err =%3E %7B%0A      console.log(err.message);%0A    %7D);%0A  %7D, 0);%0A%0A  $('form').submit((event) =%3E %7B%0A    event.preventDefault();%0A  %7D)
+%7D, 0);
 %0A%0A  
@@ -681,24 +681,52 @@
  values = %7B%0A
+      'id':         uuid(),%0A
       'match
@@ -742,33 +742,32 @@
 atchAll%0A    %7D;%0A%0A
-%0A
     values.rules
@@ -1174,218 +1174,79 @@
 %7D);%0A
+%0A
     
-console.log(
+db('
 folder
+s'
 )
-;
 %0A    
-return configHelper%0A    .saveTask(folder.name, folder.path, values)%0A    .then(() =%3E %7B%0A      console.log('Task Saved!');%0A    %7D)%0A    .catch(err =%3E %7B%0A      throw err;%0A    %7D
+.find(%7Bfolder%7D)%0A    .tasks.push(values
 );%0A
-%0A
   %7D);%0A%0A
-%0A
   $(
@@ -1310,745 +1310,8 @@
  %7D)%0A
-  %0A  $('#taskSave').on('click', (event) =%3E %7B%0A%0A    let folder = getActiveFolder();%0A    let matchAll = ($('#taskMatch').val() === 'all');%0A%0A    var values = %7B%0A      'folderName': folder.name,%0A      'folderPath': folder.path,%0A      'matchAll':   matchAll%0A    %7D;%0A%0A%0A    values.rules = %5B%5D;%0A    $.each($('#formTask').serializeArray(), function(i, field) %7B%0A        values%5Bfield.name%5D = field.value;%0A    %7D);%0A%0A    $('li %3E select').parent().each((index, value) =%3E %7B%0A%0A      let selects = $(value).find('select');%0A      values.rules.push(%7B%0A        'type':       $(selects%5B0%5D).val(),%0A        'comparator': $(selects%5B1%5D).val(),%0A        'reference':  $(value).find('input').val()%0A      %7D);%0A    %7D);%0A%0A    ipc.send('tasks', %7Bquery: 'add', values%7D);%0A%0A  %7D);%0A%0A
 %0A  $

replace use of configHelper with lowdb in tasks controller