'use strict';

app.controller('ActivitiesController', function($scope, Devices) {
  $scope.simpleActivity = { 'currentDevice': null, 'devices': [] };
  $scope.simpleActivity.index = 0;

  Devices.initFunctions();

});

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

  $scope.doNext = function() {

    Systems.updateCurrentMacroForDevice($scope.room.id, $scope.simpleActivity.currentDevice.id).then(
      function(data) {
        console.log(data);
        $state.go('menu.programming.activities.devices-to-use');
      },
      function (error) {
        alert(error);
      }
    );
  };
});


app.controller('ActivityDevicesController', function ($scope, $state, Systems, Devices) {
  var dataActivity = Systems.getCurrentMacro();
  $scope.simpleActivity.devices = [];
  console.log(dataActivity);


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

      if (device) {
        $scope.simpleActivity.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 ($scope.simpleActivity.currentDevice.id === device.id && _.isEmpty($scope.simpleActivity.devices)) {
        $scope.simpleActivity.devices.push({
          'device_id': device.id,
          'name': device.name,
          'type': 1,
          '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.goBack = function() {
    $scope.simpleActivity.devices = [];
    Devices.initFunctions();
    $state.go('menu.programming.activities.choose-device' );
  };

  $scope.doNext = function() {
    $state.go('menu.programming.activities.commands');
  };

});

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

  $scope.goBack = function() {
    if ($scope.simpleActivity.index === 0) {
      $state.go('menu.programming.activities.devices-to-use');
    } else {
      $scope.simpleActivity.index--;
    }
  };

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


  $scope.finish = function() {
    //Save to server
    var sendData = angular.copy($scope.simpleActivity.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;
    }

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

    Systems.storeDeviceButtonMacro($scope.room.id, $scope.simpleActivity.currentDevice.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");
    });

  };

});
