'use strict';

// Attestments controller
angular.module('attestments').controller('AttestmentsController', ['$scope', '$stateParams', '$location', 'Authentication', 'Attestments',
    function ($scope, $stateParams, $location, Authentication, Attestments) {
      $scope.authentication = Authentication;
      $scope.SearchConsumerID = '';
      $scope.SearchPageID = '';

      $scope.sortType = 'consumer_ID'; // set the default sort type
      $scope.sortReverse = false;  // set the default sort order

      // input array
      $scope.attestment = new Attestments({
        consumer_ID: '',
        consumer_description: '',
        page_ID: '',
        page_description: '',
        statements: []
      });

      // Create new Attestment
      $scope.create = function (isValid) {
        $scope.error = null;

        if (!isValid) {
          $scope.$broadcast('show-errors-check-validity', 'attestmentForm');
          return false;
        }

        // Create new attestment object
        var attestment = new Attestments({
          consumer_ID: this.attestment.consumer_ID,
          consumer_description: this.attestment.consumer_description,
          page_ID: this.attestment.page_ID,
          page_description: this.attestment.page_description,
          statements: this.attestment.statements
        });

        // Redirect after save
        attestment.$save(function (response) {
          $location.path('attestments/' + response._id);
          // Clear form fields
          $scope.attestment.consumer_ID = '';
          $scope.attestment.consumer_description = '';
          $scope.attestment.page_ID = '';
          $scope.attestment.page_description = '';
          $scope.attestment.statements = '';
        }, function (errorResponse) {
          $scope.error = errorResponse.data.message;
        });
      };

      // Remove existing Attestment
      $scope.remove = function (attestment) {
        if (attestment) {
          attestment.$remove();

          for (var i in $scope.attestments) {
            if ($scope.attestments[i] === attestment) {
              $scope.attestments.splice(i, 1);
            }
          }
        } else {
          $scope.attestment.$remove(function () {
            $location.path('attestments/list');
          });
        }
      };

      // Update existing Attestment
      $scope.update = function (isValid) {
        $scope.error = null;
        if (!isValid) {
          $scope.$broadcast('show-errors-check-validity', 'attestmentForm');
          return false;
        }
        
        console.log('$scope.attestment--%j', $scope.attestment);
        $scope.attestment.$update(function () {
          $location.path('attestments/' + $scope.attestment._id);
        }, function (errorResponse) {
          $scope.error = errorResponse.data.message;
        });
      };

      // Find a list of Attestments
      $scope.find = function () {
        $scope.attestments = Attestments.query();
      };

      // Find existing Attestment
      $scope.findOne = function () {
        // Callback set up  won't get instant response  Just a promise
        $scope.attestment = Attestments.get({
          attestmentId: $stateParams.attestmentId
        });
      };

      // Add array member on button click
      $scope.addMember = function (targets) {
        if (angular.isDefined(targets)) {
          var addVar = {
            statement_ID: $scope.statement_ID,
            statement_text: $scope.statement_text,
            response_type: $scope.response_type,
            date_start_string: $scope.date_start_string,
            date_end_string: $scope.date_end_string
          };

          if (targets.indexOf(addVar) < 0){
            targets.push(addVar);
          }
        }
      };

      // Remove array member on button click
      $scope.removeMember = function (source, targets) {
        if (angular.isDefined(targets)) {
          if (angular.isDefined(source)) {
            var index = targets.indexOf(source);
            if (index >= 0) {
              targets.splice(index,1);
            }
          }
        }
      };
    }
]);
