'use strict';

var major_progression = [0, 2, 4, 5, 7, 9, 11, 12, 11, 9, 7, 5, 4, 2, 0];
var minor_progression = [0, 2, 3, 5, 7, 8, 11, 12, 11, 8, 7, 5, 3, 2, 0];
var melodic_minor_progression = [0, 2, 3, 5, 7, 9, 11, 12, 10, 8, 7, 5, 3, 2, 0];

var major_fifths = ['C', 'G', 'D', 'A', 'E', 'B', 'G♭', 'D♭', 'A♭', 'E♭', 'B♭', 'F'];
var minor_fifths = ['A', 'E', 'B', 'F♯', 'C♯', 'G♯', 'D♯', 'B♭', 'F', 'C', 'G', 'D'];

var major_chromatic = ['C', 'D♭', 'D', 'E♭', 'E', 'F', 'G♭', 'G', 'A♭', 'A', 'B♭', 'B'];
var minor_chromatic = ['A', 'B♭', 'B', 'C', 'C♯', 'D', 'D♯', 'E', 'F', 'F♯', 'G', 'G♯'];

var myFace = function() {
  return "what";
}

angular.module('myApp.scale', ['ngRoute', 'ngCookies', 'ui.bootstrap'])

.config(['$routeProvider', function($routeProvider) {
  $routeProvider.when('/scale', {
    templateUrl: 'scale/scale.html',
    controller: 'ScaleCtrl'
  });
}])

.controller('ScaleCtrl', ['$scope', '$cookies', function($scope, $cookies) {
  $scope.debug = false;

  $scope.cookie = $cookies.getObject('scaler') || {};

  $scope.updateField = function(field, value) {
    $scope.cookie[field] = value;
  };

  $scope.updateData = function() {
    var now = new Date();
    var expiryDate = new Date(now.getFullYear() + 10, now.getMonth(), now.getDate());
    $cookies.putObject('scaler', $scope.cookie, {expires: expiryDate});
  };

  $scope.getField = function(field) {
    if ($scope.cookie.hasOwnProperty(field)) {
        return $scope.cookie[field];
    }
    return null;
  };

  $scope.major = "";
  $scope.tempos = {normal: 80};
  $scope.tempoIncrease = $scope.getField('tempoIncrease') ? $scope.getField('tempoIncrease') : 4;
  $scope.selected = $scope.getField('selected') ? $scope.getField('selected') : [];
  $scope.keysPerDay = $scope.getField('keysPerDay') ? $scope.getField('keysPerDay') : 3;
  $scope.doneToday = $scope.getField('doneToday') ? $scope.getField('doneToday') : 0;

  $scope.forToday = [];
  $scope.forTheFuture = [];

  $scope.lastUsedDate = $scope.getField("lastUsedDate");
  if (!$scope.lastUsedDate) {
    $scope.lastUsedDate = new Date()
    $scope.lastUsedDate.setDate($scope.lastUsedDate.getDate() - 1);
  } else {
    $scope.lastUsedDate = new Date($scope.lastUsedDate);
  }
  $scope.lastUsedDate.setHours(0,0,0,0); // In case the time is non-pure date

  $scope.majors = major_chromatic;
  $scope.minors = minor_chromatic;

  $scope.collectLists = function() {
    $scope.forToday = [];
    var numForToday = $scope.keysPerDay - $scope.doneToday;
    var selected = $scope.selected.slice();
    if (numForToday > 0) {
      while (numForToday-- && selected.length > 0) {
        $scope.forToday.push(selected.shift());
      }
    }
    $scope.forTheFuture = selected;
  };

  $scope.newDay = function() {
    $scope.doneToday = 0;
    $scope.updateField("doneToday", 0);
    $scope.updateData();
  };

  $scope.today = new Date();
  $scope.today.setHours(0,0,0,0);
  if ($scope.lastUsedDate.valueOf() !== $scope.today.valueOf()) {
    $scope.newDay();
  }

  $scope.collectLists();

  $scope.changeKeysPerDay = function() {
    $scope.updateField("keysPerDay", $scope.keysPerDay);
    $scope.updateData();
    $scope.collectLists();
  };

  $scope.changeTempoIncrease = function() {
    $scope.updateField("tempoIncrease", $scope.tempoIncrease);
    $scope.updateData();
  };

  $scope.resetDoneToday = function() {
    $scope.doneToday = 0;
    $scope.updateField("doneToday", 0);
    $scope.updateData();
    $scope.collectLists();
  };

  $scope.addToList = function(key, majorminor) {
    var newElement = {key: key + " " + majorminor, tempo: parseInt($scope.tempos.normal, 10)};
    $scope.selected.push(newElement);
    $scope.collectLists();
    $scope.updateField("selected", $scope.selected);
    $scope.updateData();
  };

  $scope.addAllToList = function(keys, majorminor) {
    keys.forEach(function(element, index, array) {
      $scope.addToList(element, majorminor);
    });
  };

  $scope.removeFromList = function(item) {
    var index = $scope.selected.indexOf(item);
    if (index > -1)
    {
      $scope.selected.splice(index, 1);
    }
    $scope.collectLists();
    $scope.updateField("selected", $scope.selected);
    $scope.updateData();
  };

  $scope.increaseTempo = function(item) {
    var index = $scope.selected.indexOf(item);
    if (index > -1)
    {
      $scope.selected.splice(index, 1);
      item.tempo += $scope.tempoIncrease;
      $scope.selected.push(item);
      $scope.doneToday += 1;
    }
    $scope.collectLists();

    $scope.updateField("selected", $scope.selected);
    var date = new Date();
    $scope.updateField("lastUsedDate", date.toISOString());
    $scope.updateField("doneToday", $scope.doneToday);
    $scope.updateData();
  };

}]);

@@ -682,21 +682,8 @@
 te',
- 'ngCookies',
  'ui
@@ -900,20 +900,8 @@
 pe',
- '$cookies',
  fun
@@ -916,18 +916,8 @@
 cope
-, $cookies
 ) %7B%0A
@@ -954,51 +954,35 @@
 ope.
-cookie = $cookies.getObject('scaler') %7C%7C %7B%7D
+localStorage = localStorage
 ;%0A%0A 
@@ -1043,29 +1043,58 @@
 ope.
-cookie%5Bfield%5D = 
+localStorage.setItem(field, JSON.stringify(
 value
+))
 ;%0A  
@@ -1140,337 +1140,158 @@
     
-var now = new Date();%0A    var expiryDate = new Date(now.getFullYear() + 10, now.getMonth(), now.getDate());%0A    $cookies.putObject('scaler', $scope.cookie, %7Bexpires: expiryDate%7D);%0A  %7D;%0A%0A  $scope.getField = function(field) %7B%0A    if ($scope.cookie.hasOwnProperty(field)) %7B%0A        return $scope.cookie%5Bfield%5D;%0A    %7D%0A    return null
+// Legacy, keeping for when we move to databases%0A  %7D;%0A%0A  $scope.getField = function(field) %7B%0A    return JSON.parse($scope.localStorage.getItem(field))
 ;%0A  

Move to localStorage. Oh my God, Santiago and Peralta forever