
angular.module('app.services')
  .factory('updateService', ['$log', '$timeout', '$q', '$http', '$rootScope', 'sparkDataService' ,'propertyCache', function ($log, $timeout, $q, $http, $rootScope, sparkDataService ,propertyCache) {

    return {

      updateApplication: function () {
        console.log("Checking Updates !");

        if (propertyCache.networkStatus != "online") {
          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 your internet connectivity</span></div>'
          });
        } else {

          var updateMessageWindow = LoadingMsg.showLoading();
          if ($filter('checkInprogress')(syncFunction)) {
            $ionicPopup.alert({
              title: 'Alert',
              content: 'Update in progress'
            }).then(function (res) {
            });
            return;
          }

          var subsResult = false;
          var checkResult = false;

          // IonicDeploy.init("qc_app", propertyCache.config.syncIp, function () {
          IonicDeploy.init(propertyCache.config.license + "-qc_app", "http://" + propertyCache.config.syncIp, function (result) {
            // IonicDeploy.init(propertyCache.config.license+"-qc_app", "http://192.168.1.68:9000", function (result) {
            subsResult = result;
            console.log("Subscribed ! ", result);
          }, function () {
            console.log("Init failed !");
          })

          // if(subsResult == "true"){
          IonicDeploy.check(propertyCache.config.license + "-qc_app", "dev", function (result) {
            console.log("Success Checked! ", result);
            if (result == "true") {
              checkResult = result;
              IonicDeploy.download(propertyCache.config.license + "-qc_app", function (dwnloadResult) {
                console.log("Downloaded !" + dwnloadResult);
                if (dwnloadResult == "true") {
                  // _self.extractUpdate();

                  IonicDeploy.extract(propertyCache.config.license + "-qc_app", function (extractResult) {
                    if (extractResult == "done") {
                      console.log("Extracted successfully !" + extractResult);
                      sparkDataService.sendUpdateAcknowledgement();
                      _self.redirectApp();
                    }
                  }, function () {
                    console.log("Extracted faliure !");
                  });

                }
              }, function () {
                console.log("Download failed !");
              });
            } else {
              LoadingMsg.hideLoading(updateMessageWindow);
              var alertPopup = $ionicPopup.alert({
                title: 'Info !',
                template: '<div class="nf-center nf-valign-wrapper">' +
                '<ion-spinner icon="ripple" class="nf-text-white"></ion-spinner>' +
                '<span>No Updates !</span></div>'
              });
            }
          }, function () {
            console.log("Check failed !");
          });
          // }

          var checkInterval = $interval(function () {
            // if (isProcessingUpdate) {
            LoadingMsg.hideLoading(updateMessageWindow);
            $interval.cancel(checkInterval);
            // }
          }, 10000);

        }
      }

    };

  }]);
