'use strict';


app.controller('AddMacroRoomController', function ($scope, $state, toastr, Systems) {
  $scope.system = Systems.getCurrentSystem();


  $scope.doNext = function() {
    return Systems.updateRoomDevices($scope.addMacro.room.id)
    .then(function(data) {
      var roomDevices = Systems.getRoomDevices();
      $scope.addMacro.room.devices = roomDevices.devices;
      $state.go('menu.advanced.add-macros.macro-type');
    })
    .catch(toastr.error);
  };
});

app.controller('MacroTypeController', function ($scope, $state, toastr, Systems) {
  var steps = $scope.addMacro.steps;
  while(steps.length > 0) {
    steps.pop();
  }
  $scope.addMacro.currentDevice = {};
  $scope.addMacro.button = {};
  $scope.addMacro.doneScreen = {'index': 0,'device': {}, 'devices': []};
  $scope.addMacro.disablePopup = false;

  $scope.navigateTo = function(state) {

    var base = 'menu.advanced.add-macros';
    switch (state) {
      case 'Turn Off All':
        $scope.macroType.id = 0;
        Systems.updateCurrentMacroForPower($scope.addMacro.room.id).then( function(data) {
          console.log(data);
          $state.go(base + '.finish');
        }).catch(toastr.error);

        break;
      case 'Device Activity':
        $scope.macroType.id =  1;
        $state.go(base + '.choose-device');
        break;
      case 'Custom Button':
        $scope.macroType.id = 2;
        $state.go(base + '.button-name');
        break;
      default:
        //do nothing
    }
  };

});

app.controller('ButtonNameController', function ($scope, $state) {
  $scope.doNext = function() {
    $state.go('menu.advanced.add-macros.finish');
  };
});


app.controller('ChooseMacroDeviceController', function ($scope, $state) {
  $scope.doNext = function() {
    $state.go('menu.advanced.add-macros.finish');
  };
});


app.controller('FinishMacroController', function ($scope, $timeout, $state) {

  $scope.addStep = function (device, func) {
    return {'name': device.name + ': ' + func.name, 'type': 1, 'device_id': device.id, 'func': func.id };
  };

  $scope.deleteAll = function() {
    var arr = $scope.addMacro.steps;
    while(arr.length > 0) {
      arr.pop();                     
    }
  };

  $scope.goBack = function() {
    if ($scope.macroType.id === 1) {
      $state.go('menu.advanced.add-macros.choose-device');
    } else if ($scope.macroType.id === 2) {
      $state.go('menu.advanced.add-macros.button-name');
    } else {
      $state.go('menu.advanced.add-macros.macro-type');
    }
  };

  $scope.doNext = function() {
    $state.go('menu.advanced.add-macros.macro-done');
  };

});

app.controller('MacroDoneController', function ($scope, $state, toastr, Systems) {
  $scope.addMacro.doneScreen.index = 0;

  $scope.goBack = function() {
    $state.go('menu.advanced.add-macros.finish');
  };

  $scope.doNext = function() {
    if ($scope.addMacro.disablePopup) {
      var steps =  angular.copy($scope.addMacro.steps);
      for (var i=0,  tot=steps.length; i < tot; i++) { delete steps[i].name; }
      //var autoWait = $scope.addMacro.autoWait ? 1:0;
      var jsonData = JSON.stringify( { auto_wait: 1 , edit_type: 1, popup: 0, activity: [], macro: steps } );

      var handleSuccess = function(response) {
        if(response.data.result === true) {
          $state.go('menu.advanced.add-macros.next-action');
        } else {
          toastr.error('Your edit/create action failed.  Please try again.');
        }
      };

      var handleError = function(error){
        toastr.error("Please try again");
      };

      if ($scope.macroType.id === 0) {
        Systems.storePowerMacro($scope.addMacro.room.id, jsonData).then(handleSuccess, handleError);
      } else if ($scope.macroType.id === 1) {
        Systems.storeDeviceButtonMacro($scope.addMacro.room.id, $scope.addMacro.currentDevice.id, jsonData).then(handleSuccess, handleError);
      } else {
        jsonData = { "text": $scope.addMacro.button.name, "auto_wait": 1 , "edit_type": 1, "popup": 0, "activity": [], "macro": steps } ;
        Systems.createCustomButtonMacro($scope.addMacro.room.id, $scope.addMacro.room.main_device_id, jsonData).then(handleSuccess, handleError);
      }
    } else {
      $state.go('menu.advanced.add-macros.macro-commands');
    }
  };

});


app.controller('MacroCommandsController', function ($scope, $state, toastr, Systems) {

  $scope.goBack = function() {
    if ($scope.addMacro.doneScreen.index === 0) {
      $state.go('menu.advanced.add-macros.macro-done' );
    } else {
      $scope.addMacro.doneScreen.index--;
    }
  };

  $scope.doNext = function() {
    var len = $scope.addMacro.doneScreen.devices.length;
    if ($scope.addMacro.doneScreen.index + 1 === len) {

      //var autoWait = $scope.addMacro.autoWait ? 1:0;
      var steps =  angular.copy($scope.addMacro.steps);
      var activityData = angular.copy($scope.addMacro.doneScreen.devices);

      _.forEach(steps, function(step) { delete step.name; });
      _.forEach(activityData, function(activity) {
        activity.func = activity.func.id;
        delete activity.name;
        delete activity.repeat_min;
        delete activity.repeat_macro;
        delete activity.target_type;
        delete activity.target_addr;
        delete activity.target_port;
        delete activity.funcs;
      });

      var jsonData = JSON.stringify( { auto_wait: 1 , edit_type: 1, popup: 1, activity: activityData, macro: steps } );
      console.log(jsonData);

      var handleSuccess = function(response) {
        if(response.data.result === true) {
          $state.go('menu.advanced.add-macros.next-action');
        } else {
          toastr.error('Your edit/create action failed.  Please try again.');
        }
      };

      var handleError = function(error){
        toastr.error("Please try again");
      };

      if ($scope.macroType.id === 0) {
        Systems.storePowerMacro($scope.addMacro.room.id, jsonData).then(handleSuccess, handleError);
      } else if ($scope.macroType.id === 1) {
        Systems.storeDeviceButtonMacro($scope.addMacro.room.id, $scope.addMacro.currentDevice.id, jsonData).then(handleSuccess, handleError);
      } else {
        jsonData = JSON.stringify( { text: $scope.addMacro.button.name, auto_wait: 1 , edit_type: 1, popup: 1, activity: activityData, macro: steps } );
        Systems.createCustomButtonMacro($scope.addMacro.room.id, $scope.addMacro.room.main_device_id, jsonData).then(handleSuccess, handleError);
      }
    } else {
      $scope.addMacro.doneScreen.index++;
    }

  };
});

