'use strict';

/**
 * @ngdoc controller
 * @name ActivitiesController
 * @description
 *  simple activity 설정 화면 (아래 controller 들의 state 를 포함하는 state 의 controller)
 */
app.controller('ActivitiesController', function($scope, Devices) {
  $scope.simpleActivity = { 'currentDevice': null, 'devices': [] };
  $scope.simpleActivity.index = 0;

  // function 들 array 초기화
  Devices.initFunctions();

});

/**
 * @ngdoc controller
 * @name ChooseActivityDeviceController
 * @description
 *  based on device 선택 화면
 */
app.controller('ChooseActivityDeviceController', function ($scope, $state, Systems ) {

  $scope.doNext = function() {

    // 해당 deivce ID 와 연계된 button 의 데이터 얻어와서 저장
    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);
      }
    );
  };
});

/**
 * @ngdoc controller
 * @name ActivityDevicesController
 * @description
 *  simple activity 선택 화면
 */
app.controller('ActivityDevicesController', function ($scope, $state, Systems, Devices, Auth) {
  var dataActivity = Systems.getCurrentMacro();
  $scope.simpleActivity.devices = [];
  console.log(dataActivity);

  var navigate = {};

  if (dataActivity) {
    _.each(dataActivity.activity, function(item) {
      if (item.type === 2 && item.navigate) {
        navigate[item.device_id] = true;
      }
    });
  }

  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) {
        var obj = {
          '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
        };
        if (navigate[data.device_id]) {
          obj.navigate = 1;
        }
        $scope.simpleActivity.devices.push(obj);
      }
    });
  }
  else {
    _.forEach($scope.room.devices, function(device) {
      if ($scope.simpleActivity.currentDevice.id === device.id && _.isEmpty($scope.simpleActivity.devices)) {
        var obj = {
          '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
        };
        if (navigate[device.id]) {
          obj.navigate = 1;
        }
        $scope.simpleActivity.devices.push(obj);
      }
    });
  }

  $scope.goBack = function() {
    $scope.simpleActivity.devices = [];
    Devices.initFunctions();
    $state.go('menu.programming.activities.choose-device' );
  };

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

  Auth.currentUser().then(function(user) {
    $scope.optMenuShow = user.optMenuShow;
  });
  
  $scope.goAdvanced = function() {
    $state.go('menu.advanced.add-macros.macro-type');
  };

});

/**
 * @ngdoc controller
 * @name ActivityDeviceCommandsController
 * @description
 *  command 선택 화면
 */
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);

    // 해당 device ID 와 연계된 button 의 데이터 저장
    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");
    });

  };

});
