'use strict';

app.controller('PowerOffController',  function ($scope, Devices) {
  Devices.initFunctions();
  $scope.powerOff = { 'devices': [], 'device': {} };
  $scope.powerOff.index = 0;
});


app.controller('PowerOffDevicesController', function ($scope, $state, $http, Systems, Devices, currentMacro) {

  var currentSystem = Systems.getCurrentSystem(),
       dataActivity = currentMacro;
  console.log(dataActivity);

  if(dataActivity && dataActivity.edit_type === 0 && dataActivity.activity.length ) {
    _.forEach(dataActivity.activity, function(data) {
      var device = Systems.getDevice($scope.room.id, data.device_id);

      if (device) {
        $scope.powerOff.devices.push({
          'device_id': data.device_id,
          'name': device.name,
          'type': data.type,
          'func': data.func,
          'repeat_min': device.repeat_min,
          'repeat_macro': device.repeat_macro,
          'target_type': device.target_type,
          'target_addr': device.target_addr,
          'target_port': device.target_port
        });
      }
    });
  }
  // else {
  //   _.forEach($scope.room.devices, function(device) {
  //     if ( _.isEmpty($scope.powerOff.devices)) {
  //       $scope.powerOff.devices.push({
  //         'device_id': device.id,
  //         'name': device.name,
  //         'type': 0,
  //         'func': null,
  //         'repeat_min': device.repeat_min,
  //         'repeat_macro': device.repeat_macro,
  //         'target_type': device.target_type,
  //         'target_addr': device.target_addr,
  //         'target_port': device.target_port,
  //       });
  //     }
  //   });
  // }

  $scope.doNext = function() {

    $state.go('menu.programming.power-off.commands');
  };

});

app.controller('PowerOffDeviceCommandsController', function ($scope, $state, Systems, Devices) {

  $scope.goBack = function() {
    if ($scope.powerOff.index === 0) {
      $scope.powerOff.devices = [];
      Devices.initFunctions();
      $state.go('menu.programming.power-off.devices-to-use' );
    } else {
      $scope.powerOff.index--;
    }
  };


  $scope.incrIdx = function() { $scope.powerOff.index++; };

  $scope.finish = function() {
    //Save to server

    var sendData = angular.copy($scope.powerOff.devices);

    for (var i=0,  tot=sendData.length; i < tot; i++) {
      sendData[i].func = sendData[i].func.id;
      delete sendData[i].name;
      delete sendData[i].repeat_min;
      delete sendData[i].repeat_macro;
      delete sendData[i].target_type;
      delete sendData[i].target_addr;
      delete sendData[i].target_port;
      delete sendData[i].funcs;
    }

    var jsonData = JSON.stringify( { auto_wait : 1, edit_type : 0, popup : 1, activity : sendData, macro : [] } );
    console.log(jsonData);

    Systems.storePowerMacro($scope.room.id, jsonData).then(function(response) {
      if(response.data.result === true) {
        $state.go('menu.programming.options');
      }
      else {
        alert('Your edit/create action failed.  Please try again.');
      }
    }, function(error){
      alert("Please try again");
    });
  };

});
