'use strict';

angular.module('emission.splash.updatecheck', ['emission.plugin.logger',
                                               'angularLocalStorage'])

.factory('UpdateCheck', function($ionicPopup, $rootScope, Logger, storage) {
  var uc = {};
  var CHANNEL_KEY = 'deploy_channel';

  /*
   * Returns a promise that resolves to the name of the channel
   * to load UI updates from.
   */
  uc.getChannel = function() {
    return storage.get(CHANNEL_KEY);
  };

  uc.setChannel = function(channelName) {
    storage.set(CHANNEL_KEY, channelName);
  };

  uc.handleClientChangeURL = function(urlComponents) {
    Logger.log("handleClientChangeURL = "+JSON.stringify(urlComponents));
    if (urlComponents['clear_local_storage'] == "true") {
        Logger.log("About to clear all local storage");
        storage.clearAll();
    }
    if (urlComponents['clear_usercache'] == "true") {
        Logger.log("About to clear usercache");
        window.cordova.plugins.BEMUserCache.clearAll();
    }
    uc.setChannel(urlComponents['channel']);
    uc.checkForUpdates();
  };

  // Default to dev
  var getChannelToUse = function() {
      var channel = uc.getChannel();
      if (channel == null || channel == "") {
        console.log("No saved channel found, using prod")
        channel = 'prod';
      };
      console.log("Returning channel "+channel)
      return channel;
  }

  var deploy = new Ionic.Deploy();

  var applyUpdate = function() {
    $ionicPopup.show({
      title: 'Downloading UI-only update',
      template: '<progress class="download" value="{{progress}}" max="100"></progress>',
      scope: $rootScope,
      buttons: []
    });
    if ($rootScope.isDownloading) {
      return;
    }
    deploy.update().then(function(res) {
      Logger.log('Ionic Deploy: Update Success! ' + res);
      var reloadAlert = $ionicPopup.alert("Update done, reloading...");
      reloadAlert.then(function(res) {
        deploy.load();
      });
    }, function(err) {
       console.log('Ionic Deploy: Update error! ', err);
       Logger.log('Ionic Deploy: Update error! '+ err);
       $rootScope.isDownloading = false;
       $ionicPopup.alert({template: 'Error during update'});
    }, function(prog) {
      console.log('Ionic Deploy: Progress... ', prog);
      $rootScope.$apply(function(){
          $rootScope.progress = prog;
          $rootScope.isDownloading = true;
          if(prog==100) {
            $rootScope.isDownloading = false;
          }
      });
    })
  };


    // Check Ionic Deploy for new code
  uc.checkForUpdates = function() {
    console.log('Ionic Deploy: Checking for updates');
    deploy.setChannel(getChannelToUse());
    deploy.check().then(function(hasUpdate) {
      Logger.log('Ionic Deploy: Update available: ' + hasUpdate);
      if (hasUpdate) {
        Logger.log('Ionic Deploy: found update, asking user: ');

        $rootScope.rn_list = [];
        deploy.getMetadata().then(function(metadata) {
          for (var key in metadata) {
            if (key.startsWith('release_note')) {
              $rootScope.rn_list.push(metadata[key]);
            }
          }
          console.log("About to ask for update with release notes"+
            $rootScope.rn_list);
          $ionicPopup.show({
            title: "Download new UI-only update?",
            templateUrl: 'templates/splash/release-notes.html',
            scope: $rootScope,
            buttons: [{ // Array[Object] (optional). Buttons to place in the popup footer.
              text: 'Not now',
              type: 'button-default',
            }, {
              text: 'Apply',
              type: 'button-positive',
              onTap: function(e) {
                return true;
              }
            }]
          }).then(function(res){
            if(res) {
              Logger.log('Ionic Deploy: User accepted deploy update');
              applyUpdate();
            } else {
              Logger.log('User skipped deploy update');
            }
          });
        }, function(err) {
          Logger.log('Ionic Deploy: Unable to read metadata: '+err);
        }, function() {});
      } else {
        // TODO: Figure out a better way to do this using promises
        // $ionicPopup.alert({title: "Up to date!"});
      }
    }, function(err) {
      Logger.log('Ionic Deploy: Unable to check for updates'+err);
      console.error('Ionic Deploy: Unable to check for updates',err)
    });
  }

  return uc;
});


@@ -1028,23 +1028,26 @@
 onents%5B'
-channel
+new_client
 '%5D);%0A   

Change the parameter for the channel name