'use strict';

angular.module('mean.jobs').controller('JobsController', ['$scope', '$stateParams',
'$location', '$window', '$http', 'Global', 'Employers', 'Engines', 'MeanUser',
'Circles', function($scope, $stateParams, $location, $window, $http, Global,
  Employers, Engines, MeanUser, Circles) {
    $scope.global = Global;

    $scope.upload = function() {
      var file = document.getElementById("file");
      var uploadFile = file.files[0];
      var fd = new FormData();
      fd.append('file', uploadFile);
      fd.append('name', $scope.name);

      $http.post('api/uploads', fd, {
        transformRequest: angular.identity,
        headers: {'Content-Type': undefined}
      }).success(function(data) {
        $window.location.reload();
      });
    };

    $scope.createEmployer = function(isValid) {
      if (isValid) {
        var employer = new Employers($scope.employer);

        employer.$createEmployer(function(response) {
          $location.path('/employers');
        });

        $scope.employer = {};

      } else {
        $scope.submitted = true;
      }
    };

    $scope.createEngine = function(isValid) {
      if (isValid) {
        var engine = new Engines($scope.engine);

        engine.$createEngine(function(response) {
          $location.path('/search-engines');
        });

        $scope.engine = {};

      } else {
        $scope.submitted = true;
      }
    };

    $scope.removeFile = function(file) {
      $http.delete('api/uploads/' + file.filename).success(function(response) {
          $window.location.reload();
      });
    };

    $scope.removeEmployer = function(employer) {
      if (employer) {
        employer.$destroyEmployer(function(response) {
          for (var i in $scope.employers) {
            if ($scope.employers[i] === employer) {
              $scope.employers.splice(i, 1);
            }
            }
            $window.location.reload();
          });
      } else {
        $scope.employer.$destroyEmployer(function(response) {
          $window.location.reload();
        });
      }
    };

    $scope.removeEngine = function(engine) {
      if (engine) {
        engine.$destroyEngine(function(response) {
          for (var i in $scope.engines) {
            if ($scope.engines[i] === engine) {
              $scope.engines.splice(i, 1);
            }
          }
          $window.location.reload();
        });
      } else {
        $scope.engine.$destroyEngine(function(response) {
          $window.location.reload();
        });
      }
    };

    $scope.updateEmployer = function(isValid) {
      if (isValid) {
        var employer = $scope.employer;

        employer.$updateEmployer(function() {
          $location.path('/employers');
        });
      } else {
        $scope.submitted = true;
      }
    };

    $scope.updateEngine = function(isValid) {
      if (isValid) {
        var engine = $scope.engine;

        engine.$updateEngine(function() {
          $location.path('/search-engines');
        });
      } else {
        $scope.submitted = true;
      }
    };

    $scope.findFiles = function() {
      $http.get('/api/uploads').success(function(files) {
        $scope.files = files;
      })
    };

    $scope.findEmployers = function() {
      Employers.allEmployers(function(employers) {
        $scope.employers = employers;
      });
    };

    $scope.findEngines = function() {
      Engines.allEngines(function(engines) {
        $scope.engines = engines;
      });
    };

    $scope.findEmployer = function() {
      if (typeof $stateParams.employerId !== 'undefined') {
        Employers.get({
          employerId: $stateParams.employerId
        }, function(employer) {
          $scope.employer = employer;
        });
      }
    };

    $scope.findEngine = function() {
      if (typeof $stateParams.engineId !== 'undefined') {
        Engines.get({
          engineId: $stateParams.engineId
        }, function(engine) {
          $scope.engine = engine;
        });
      }
    };
}]);
