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

    return {



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

        var _self = this;

        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 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 !");
          })

          IonicDeploy.check(propertyCache.config.license + "-qc_app", "dev", function (result) {
            console.log("Success Checked! ", result);
            if (result == "true") {
              checkResult = result;


              var confirmPopup = $ionicPopup.confirm({
                title: 'Synchronize data',
                template: 'Are you sure you want to Update ?'
              });
              confirmPopup.then(function(res) {
                if(res) {

                  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 {
                  console.log('You are not sure');
                }
              });

            } 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 !");
          });

        }
      }

    };

  }]);


