'use strict';

app.directive('deviceBtnDelete', function() {
  // Runs during compile
  return {
    restrict: 'E', // E = Element, A = Attribute, C = Class, M = Comment
    template: '<a title="Delete this button" ng-click="showModal()"><i class="fa fa-fw fa-lg fa-trash-o action-icon"></i></a>',
    scope: {
      selectedBtn: '='
    },
    replace: true,
    controller: function($scope, $modal) {
      var deleteModal = $modal({ scope: $scope, title: 'Delete this button', template:'views/modal-tpls/device-delete-btn.html', show:false});
      
      function showModal() {
        deleteModal.$promise.then(deleteModal.show);
      }
      function hideModal() {
        deleteModal.$promise.then(deleteModal.hide);
      }

      $scope.showModal = showModal;

      $scope.delete = function(obj) {

        var keys = ['id', 'page_btn_idx', 'name', 'device_id', 'room_id', 'repeat_min', 'repeat_macro', 'press_hold', 'repeatSetting'];

        for (var prop in obj) {
          if (keys.indexOf(prop) === -1) {
            delete obj[prop];
          } else {
            if (prop === 'repeatSetting') {
              obj[prop] = 'default';
            }
          }
        }
        $scope.selectedBtn = null;
        hideModal();
      };
    }  
  };
});