/**
 * Created by shadeeka on 1/9/16.
 */
angular.module('app.controllers')
  .controller('SummeryCtrl', function ($scope,ionicMaterialInk, dataPersistenceService,ModalViews) {

    ionicMaterialInk.displayEffect();

    var _self = this;
    _self.viewName ="summery";
    _self.summeryArry=[];
    _self.sortedTopFiveIssues=[];
    _self.sortedTopFiveOperation=[];

    _self.exit =function(){
      if(ModalViews.getView(_self.viewName)) {
        ModalViews.getView(_self.viewName).hide();
      }
    }

    _self.initializeSettingData = function() {
      console.log("initializeSettingData !");
      _self.summeryArry=[];
      _self.sortedTopFiveIssues=[];
      _self.sortedTopFiveOperation=[];

      dataPersistenceService.selectSummeryReport().then(function (result) {
        var temTotArr=[];
        var totChecked=0;
        var totPass=0;
        var totDefect=0;
        var totDefective=0;
        var totReject=0;
        var totRectified=0;
        for (var j = 0; j < result.rows.length; j++) {
          var temArr=[];
          temArr.Hour=result.rows.item(j).Hour;

          if(result.rows.item(j).Style.length > 9){
            temArr.Style=result.rows.item(j).Style.substr(0,9);
          }else{
            temArr.Style=result.rows.item(j).Style;
          }

          if(result.rows.item(j).Po.length > 9){
            temArr.Po=result.rows.item(j).Po.substr(0,9);
          }else{
            temArr.Po=result.rows.item(j).Po;
          }

          if(result.rows.item(j).Color.length > 9){
            temArr.Color=result.rows.item(j).Color.substr(0,9);
          }else{
            temArr.Color=result.rows.item(j).Color;
          }

          temArr.TotChecked=result.rows.item(j).totFtt +result.rows.item(j).totDefective + result.rows.item(j).totReject;
          temArr.PassPcs=result.rows.item(j).totFtt;
          temArr.Defects=result.rows.item(j).totDefect;
          temArr.DefectivePcs=result.rows.item(j).totDefective;
          temArr.RejectPcs=result.rows.item(j).totReject;
          temArr.RectifiedPcs=result.rows.item(j).totRectified;
          temArr.Dhu=((result.rows.item(j).totDefect*100)/temArr.TotChecked).toFixed(2);
          //add total here
          totChecked+=temArr.TotChecked;
          totPass+=temArr.PassPcs;
          totDefect+=temArr.Defects;
          totDefective+=temArr.DefectivePcs;
          totReject+=temArr.RejectPcs;
          totRectified+=temArr.RectifiedPcs;
          _self.summeryArry.push(temArr);
        }

        temTotArr.Hour="";
        temTotArr.Style="";
        temTotArr.Po="";
        temTotArr.Color="Total ";
        temTotArr.TotChecked=totChecked;
        temTotArr.PassPcs=totPass;
        temTotArr.Defects=totDefect;
        temTotArr.DefectivePcs=totDefective;
        temTotArr.RejectPcs=totReject;
        temTotArr.RectifiedPcs=totRectified;
        temTotArr.Dhu=((temTotArr.Defects*100)/temTotArr.TotChecked).toFixed(2);
        _self.summeryArry.push(temTotArr);
      }, function (err) {
        //_self.data="";
        console.log("Getting the line assicned data ------->  error " , err);
      });
      var operationArr=[];
      var issueArr=[];
      dataPersistenceService.selectAllSummery().then(function (result) {
        for (var j = 0; j < result.rows.length; j++) {
          if(result.rows.item(j).DefectIssue!=undefined && result.rows.item(j).DefectIssue!="") {
            var issueA = JSON.parse(result.rows.item(j).DefectIssue);
            for (var i in issueA) {
              issueArr.push(issueA[i]);
            }
          }
          if(result.rows.item(j).DefectOperation!=undefined && result.rows.item(j).DefectOperation!="") {
            operationArr.push(result.rows.item(j).DefectOperation);
          }
        }

        var topFiveOperation=[];
        var topFiveIssues=[];
        for(var i in operationArr){
          if(topFiveOperation[operationArr[i]] > 0){
            topFiveOperation[operationArr[i]]=topFiveOperation[operationArr[i]]+1;
          }else {
            topFiveOperation[operationArr[i]] = 1;
          }
        }

        for(var j in issueArr){
          if(topFiveIssues[issueArr[j]] > 0){
            topFiveIssues[issueArr[j]]=topFiveIssues[issueArr[j]]+1;
          }else {
            topFiveIssues[issueArr[j]] = 1;
          }
        }

        Object.keys(topFiveIssues).map(function (key) { return _self.sortedTopFiveIssues.push({k:key,v:topFiveIssues[key]}); });
        _self.sortedTopFiveIssues.sort(function(a,b){if (a.v > b.v) {return -1;}if (a.v < b.v) {return 1;}return 0;});
        Object.keys(topFiveOperation).map(function (key) { return _self.sortedTopFiveOperation.push({k:key,v:topFiveOperation[key]}); });
        _self.sortedTopFiveOperation.sort(function(a,b){if (a.v > b.v) {return -1;}if (a.v < b.v) {return 1;}return 0;});
        _self.sortedTopFiveIssues=_self.sortedTopFiveIssues.slice(0,5);
        _self.sortedTopFiveOperation=_self.sortedTopFiveOperation.slice(0,5);

      }, function (err) {
        //_self.data="";
        console.log("Getting the line assicned data ------->  error " , err);
      });


    }


    $scope.safeApply = function (fn) {
      var phase = this.$root.$$phase;
      if (phase == '$apply' || phase == '$digest') {
        if (fn && (typeof(fn) === 'function')) {
          fn();
        }
      } else {
        this.$apply(fn);
      }
    }

    /*$rootScope.$on("summery-data", function () {
     console.log('summery-data');
     _self.initializeSettingData();
     }
     );*/


    $scope.$on('modal.shown', function (modal) {
      if (ModalViews.getView(_self.viewName).isShown())
        console.log('initialize summary...');
      _self.initializeSettingData();
    });

  });
