/**
 * Created by shadeeka on 12/4/15.
 */
angular.module('app.controllers')
  .controller('DefectsCtrl', ["$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.currentIssues = []; //issues for selected operation
      _self.issueslist = [];//final issue list to save
      _self.lastIndex = 0;
      $scope.currentPage = 0;
      $scope.pageSize = 12;
      $scope.isSingleSelectOps = true;
      $scope.isSingleSelectIssues = false;

      _self.selectedIssues = [];
      $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.currentIssues = angular.copy(issuesCache.getIssuesByGroup(operation.group_id)); //get filtered operations from separate list

        //goto issues page
        $log.debug(_self.currentIssues);
        cacheViews.issuesToRender = _self.currentIssues;
        cacheViews.issueListToSave = _self.issueslist;
        cacheViews.toIssues = 0;
        $state.go("app.opissues", {issuedata: {issues: _self.currentIssues, list: _self.issueslist}});
      }

      _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.selectedIssues = [];
        _self.currentIssues = [];
        _self.issueslist = [];
      }

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

      _self.issueClick = function (issue) {
        if ($scope.isSingleSelectIssues) {

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

      _self.saveFinalList = function (issue) {

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

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

        newIssue.issues = _self.selectedIssues;
        _self.lastIndex = _self.issueslist.push(newIssue);

      }
      _self.isInList = function (list, newIssue) {
        var ret = {};
        angular.forEach(list, function (issue, index) {
          if (newIssue.category_id == issue.category_id) {
            ret.index = index;
            ret.issue = issue;
          }
        });
        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();
        }
      });

    }]);
