/**
 * Created by nalee on 9/19/2016.
 */
angular.module('app.controllers')
  .controller('ScoreboardConfigCtrl', function ($scope, $state, ionicMaterialInk, $ionicPlatform, ionicMaterialMotion, $log, dataPersistenceService, propertyCache, ModalViews, sparkDataService, auditProtocol, $ionicLoading,$ionicPopup) {

    ionicMaterialInk.displayEffect();
    var loadingTemp='<div class="nf-center nf-valign-wrapper">' +
      '<ion-spinner icon="ripple" class="nf-text-white"></ion-spinner>' +
      '<span class="padding-10">Please wait</span></div>';

    var intervals = {
      quality: 0,
      efficiency: 0,
      pcb: 0,
      notification: 0
    };

    var _self = this;
    _self.viewName = "scoreboard_config";

    $scope.isPausedQuality = false;
    $scope.isPausedEfficiency = false;
    $scope.isPausedPcb = false;
    $scope.isPausedNotification = false;

    _self.initializeAppConfigData = function () {
      $ionicLoading.show({
        template: loadingTemp
      });
      sparkDataService.getAppConfig().then(function (res) {

        if (res.data[0].pcb != 99999) {
          intervals.pcb = res.data[0].pcb;
        }
        if (res.data[0].efficiency != 99999) {
          intervals.efficiency = res.data[0].efficiency;
        }
        if (res.data[0].quality != 99999) {
          intervals.quality = res.data[0].quality;
        }
        if (res.data[0].notification != 99999) {
          intervals.notification = res.data[0].notification;
        }
        localStorage.setItem('interval', JSON.stringify(intervals));
        $ionicLoading.hide();
      }).catch(function(){
        $ionicLoading.hide();
        var alertPopup = $ionicPopup.alert({
          title: 'Application is <b>Offline</b>',
          template: '<div class="nf-center nf-valign-wrapper">' +
          '<ion-spinner icon="ripple" class="nf-text-white"></ion-spinner>' +
          '<span>Please check the connectivity</span></div>'
        });
        _self.exit();
      });
    };


    _self.click = function (viewType) {
      var Tempintervals = JSON.parse(localStorage.getItem('interval'));
      if (viewType == 'efficiency') {
        if ($scope.isPausedEfficiency == false) {
          $scope.isPausedPcb = false;
          $scope.isPausedEfficiency = true;
          $scope.isPausedQuality = false;
          _self.sendData(Tempintervals.pcb, 99999, Tempintervals.quality,Tempintervals.notification)
        }
        else {
          $scope.isPausedEfficiency = false;
          _self.sendData(Tempintervals.pcb, Tempintervals.efficiency, Tempintervals.quality,Tempintervals.notification)
        }
      }
      else if (viewType == 'pcb') {
        if ($scope.isPausedPcb == false) {
          $scope.isPausedPcb = true;
          $scope.isPausedEfficiency = false;
          $scope.isPausedQuality = false;
          _self.sendData(99999, Tempintervals.efficiency, Tempintervals.quality,Tempintervals.notification)
        }
        else {
          $scope.isPausedPcb = false;
          _self.sendData(Tempintervals.pcb, Tempintervals.efficiency, Tempintervals.quality,Tempintervals.notification)
        }
      }

      else if (viewType == 'quality') {
        if ($scope.isPausedQuality == false) {
          $scope.isPausedPcb = false;
          $scope.isPausedEfficiency = false;
          $scope.isPausedQuality = true;
          _self.sendData(Tempintervals.pcb, Tempintervals.efficiency, 99999,Tempintervals.notification)
        }
        else {
          $scope.isPausedQuality = false;
          _self.sendData(Tempintervals.pcb, Tempintervals.efficiency, Tempintervals.quality,Tempintervals.notification)
        }
      }
      else if (viewType == 'notification') {
        if ($scope.isPausedNotification == false) {
          $scope.isPausedPcb = false;
          $scope.isPausedEfficiency = false;
          $scope.isPausedQuality = false;
          $scope.isPausedNotification = true;
          _self.sendData(Tempintervals.pcb, Tempintervals.efficiency,Tempintervals.quality, 99999)
        }
        else {
          $scope.isPausedNotification = false;
          _self.sendData(Tempintervals.pcb, Tempintervals.efficiency, Tempintervals.quality,Tempintervals.notification)
        }
      }
    };

    _self.sendData=function(pcb, efficiency, quality,notification){
      $ionicLoading.show({
        template: loadingTemp
      });
      sparkDataService.sendAppConfig(pcb, efficiency, quality,notification).then(function(){
        //////////////////audit//////////////////////
        var $audit = auditProtocol.getEvent("appConfiguration");
        $audit.data.reference.pcbInterval = pcb;
        $audit.data.reference.efficiencyInterval = efficiency;
        $audit.data.reference.qualityInterval = quality;
        $audit.data.reference.notification = notification;
        sparkDataService.sendDataToServer($audit);
        $ionicLoading.hide();
        /////////////////////////////////////////////////
      }).catch(function(){
        $ionicLoading.hide();
        //todo revert icon after fail
        var alertPopup = $ionicPopup.alert({
          title: 'Application is <b>Offline</b>',
          template: '<div class="nf-center nf-valign-wrapper">' +
          '<ion-spinner icon="ripple" class="nf-text-white"></ion-spinner>' +
          '<span>Please check the connectivity</span></div>'
        });
      })
    };

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

      }
    };


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

    $scope.$on('modal.shown', function (modal) {
      if (ModalViews.getView(_self.viewName).isShown()) {
        _self.initializeAppConfigData();
      }
    });
  });
