/**
 * Created by shadeeka on 1/24/16.
 */
angular.module('app.controllers')
  .controller('OpsIssuesCtrl', ["$scope", "$log", "issuesCache", "$ionicSlideBoxDelegate", "eventProtocol", "$state", "propertyCache", "ModalViews", "cacheViews",
    function ($scope, $log, issuesCache, $ionicSlideBoxDelegate, eventProtocol, $state, propertyCache, ModalViews, cacheViews) {

      var _self = this;
      _self.currentIssues = []; //issues for selected operation

      _self.issueslist = [];//final issue list to save
      _self.lastIndex = 1;
      $scope.currentPage = 0;
      $scope.currentPagei = 0;
      $scope.pageSize = 14;
      $scope.isSingleSelectIssues = false;

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

      _self.issueClick = function (issue) {

        if ($scope.isSingleSelectIssues) {

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

      _self.resetState = function () {
        _self.selectedIssues=[];
        _self.currentIssues = cacheViews.issuesToRender;
        _self.issueslist = cacheViews.issueListToSave;

        angular.forEach(_self.currentIssues, function (op, index) {
          _self.currentIssues[index].ischecked = false;
        });
        cacheViews.toIssues = -1;
        $scope.$safeApply($scope);
      }
      var selectedIssue = {};

      _self.handleSingleSelectIssues = function (issue) {
        selectedIssue.ischecked = false;
        selectedIssue = issue;
        issue.ischecked = true;
      }
      _self.handleMultiSelectIssues = function (issue) {
        var index = _self.selectedIssues.indexOf(issue);
        if (index > -1) {
          _self.selectedIssues.splice(index, 1);
          issue.ischecked = false;
        } else {
          _self.selectedIssues.push(issue);
          issue.ischecked = true;
        }

        _self.updateIssues(_self.selectedIssues);
      }

      _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);

        $log.debug(">> added to final list :", _self.issueslist);

      }
      _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;
      }

      _self.transformIntoStandard = function (operationsList) {
        var list = angular.copy(operationsList);
        var issueList = [];
        angular.forEach(list, function (opp, index) {
          angular.forEach(opp.issues, function (issue, idx) {

            var newIssue = new Object();
            newIssue.category_id = issue.category_id;
            newIssue.name = issue.name;
            newIssue.operations = [opp.name];

            var indexItm = _self.isInList(issueList, newIssue);

            if (angular.isDefined(indexItm.index))
              issueList[indexItm.index].operations.push(opp.name);
            else
              issueList.push(newIssue);
          });
        });

        return issueList;
      }

      _self.save = function () {

        var $event = eventProtocol.getEvent('defects');
        var issues = [];
        issues = _self.transformIntoStandard(_self.issueslist);
        $event.data.issues = issues;
        if (issues[issues.length - 1].operations.length > 0) {
          cacheViews.defect =$event;
          $state.go('app.measles');
        } else {

        }
      }


      _self.updateIssues = function (operations) {
        //override existing
        _self.issueslist[_self.lastIndex - 1].issues = operations;
        $log.debug(">> updated list :", _self.issueslist);
      }

      $scope.numberOfPages = function (array) {
        return Math.ceil(array.length / $scope.pageSize);
      }
      $scope.$on('$ionicView.beforeEnter', function () {
        $scope.$parent.clearAllFabs();
        if (cacheViews.toIssues === 0) {
          _self.resetState();
        }
      });
    }]);
