
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("configData.appType", propertyCache.config.syncIp, function () {
          IonicDeploy.init(propertyCache.config.license + "-"+configData.appType, "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 + "-"+configData.appType, "dev", function (checkResult) {
            console.log("Success Checked! ", checkResult);
            if (checkResult == "true") {
              checkResult = checkResult;


              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 + "-"+configData.appType, function (dwnloadResult) {
                    console.log("Downloaded !" + dwnloadResult);
                    if (dwnloadResult == "true") {

                      IonicDeploy.extract(propertyCache.config.license + "-"+configData.appType, function (extractResult) {
                        if (extractResult == "done") {
                          console.log("Extracted successfully !" + extractResult);
                          sparkDataService.sendUpdateAcknowledgement();

                          IonicDeploy.redirect(propertyCache.config.license+"-"+configData.appType, function () {

                            var checkInterval = $interval(function () {
                              LoadingMsg.hideLoading(id);
                              $interval.cancel(checkInterval);
                            }, 3000);

                          }, function () {
                            console.log("Redirected failed !");
                          });

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

        }
      }

    };

  }]);


