/**
 * Created by shadeeka on 12/4/15.
 */
angular.module('app.controllers')
  .controller('RejectsCtrl', ["$scope", "$log", "issuesCache", "$ionicSlideBoxDelegate", "eventProtocol", "$state", "propertyCache", "ModalViews", "cacheViews",
    function ($scope, $log, issuesCache, $ionicSlideBoxDelegate, eventProtocol, $state, propertyCache, ModalViews, cacheViews) {

      var _self = this;

      _self.operations = []// issuesCache.getSelectedOperations();
      _self.currentRejects = []; //issues for selected operation
      _self.rejectslist = [];//final issue list to save
      _self.lastIndex = 0;
      $scope.currentPage = 0;
      $scope.pageSize = 12;
      $scope.isSingleSelectOps = true;
      $scope.isSingleSelectRejects = false;

      _self.selectedRejects = [];
      $scope.style = propertyCache.day_start.style;


      var selectedOperations = [];
      var selectedOperation = {};

      //init cache for second time
      issuesCache.init();

      _self.goTo = function (name) {
        if (ModalViews.getView(name))
          ModalViews.getView(name).show();
      }

      //watch for add operations changes
      $scope.$watch(function () {
        return issuesCache.selectedOperations;
      }, function (newValue) {
        _self.operations = issuesCache.getSelectedOperations();
        $scope.$safeApply($scope);
      });

      _self.handleSingleSelectOperations = function (operation) {

        selectedOperation.selected = false;
        selectedOperation = operation;
        operation.selected = true;
        _self.saveFinalList(operation);
        _self.currentRejects = angular.copy(issuesCache.getRejectsByGroup(operation.group_id)); //get filtered operations from separate list

        //goto issues page
        $log.debug(_self.currentRejects);
        cacheViews.rejectsToRender = _self.currentRejects;
        cacheViews.rejectListToSave = _self.rejectslist;
        cacheViews.toRejects = 0;
        $state.go("app.oprejects");
      }

      _self.resetState = function () {
        issuesCache.checkForRecentStyleChange();
        _self.operations = [];
        _self.operations = issuesCache.getSelectedOperations();
        $scope.style = propertyCache.day_start.style;

        angular.forEach(_self.operations, function (op, index) {
          _self.operations[index].selected = false;
        });

        cacheViews.toOperations = -1;

        issuesCache.loadIssue();
        issuesCache.loadRejects();
        $scope.$safeApply($scope);
      }

      _self.handleMultiSelectOperations = function (operation) {
        var index = selectedOperations.indexOf(operation);
        if (index > -1) {
          selectedOperations.splice(index, 1);
          operation.selected = false;
        } else {
          selectedOperations.push(operation);
          operation.selected = true;
        }
      }
      _self.startNew = function () {
        _self.selectedRejects = [];
        _self.currentRejects = [];
        _self.rejectslist = [];
      }

      _self.operationClick = function (operation) {
        _self.startNew();
        if ($scope.isSingleSelectOps) {
          _self.handleSingleSelectOperations(operation);
        }
      }

      _self.rejectClick = function (reject) {
        if ($scope.isSingleSelectIssues) {

        } else {
          _self.handleMultiSelectIssues(reject);
        }
      }

      _self.saveFinalList = function (reject) {

        if (!reject.hasOwnProperty('name'))
          return;

        //this is a new issue
        var newReject = angular.copy(reject);

        newReject.rejects = _self.selectedRejects;
        _self.lastIndex = _self.rejectslist.push(newReject);

      }
      _self.isInList = function (list, newReject) {
        var ret = {};
        angular.forEach(list, function (reject, index) {
          if (newReject.category_id == reject.category_id) {
            ret.index = index;
            ret.reject = reject;
          }
        });
        return ret;
      }

      $scope.numberOfPages = function (array) {
        return Math.ceil(array.length / $scope.pageSize);
      }
      $scope.$on('$ionicView.beforeEnter', function () {
        $scope.$parent.clearAllFabs();

        if (cacheViews.toOperations === 0) {
          _self.resetState();
        }
      });

    }]);
