// Controller will call pokedexDB Services to present data to html view.
//
// For using sqlite you have to install $cordovaSQLite by running the following
// command in your cmd.exe for windows or terminal for mac:
// $ cd your_project_path
// $ ionic plugin remove io.litehelpers.cordova.sqlite
// $ ionic plugin add https://github.com/litehelpers/Cordova-sqlite-storage.git
//
// For install $cordovaSQLite plugin you also have to install this following plugin to get $cordovaSQLite work :
// $ ionic plugin add com.ionic.keyboard
//
// Learn more about $cordovaSQLite :
// http://ngcordova.com/docs/plugins/sqlite/
//
// Controller of Contract List Page.
appControllers.controller('imageDownloadCtrl', function ($scope, $stateParams, $mdBottomSheet, $timeout, $mdDialog, $cordovaFileTransfer, $cordovaFile, $state, pokedexDB) {
    
    $scope.progressval = 0;
  $scope.currentfile = '';
  $scope.progressprozent = 0;
  $scope.DownloadisVisible=true;
  $scope.AbbrechenisVisible=false;
  $scope.startdownloadtimer=false;

  // Open our new task modal
  $scope.downloadImages = function() {
  $scope.startdownloadtimer=true;

  $scope.DownloadisVisible=false;
  $scope.AbbrechenisVisible=true;

  //$ionicPlatform.ready(function() { 
    //$scope.taskModal.show();
    var baseurl = "http://assets.pokemon.com/assets/cms2/img/pokedex/full/";
    var url="";
    var filename="";
    var targetPath = "";
    var downloadprogress = 0;
    var pokedexid=1;
    var downloadinprogress=false;
  

    //Ordner erstellen auf SD-Karte
    $cordovaFile.createDir(cordova.file.externalRootDirectory, "PokedexHelperBilder", false)
        .then(function (success) {
            // success
            console.log('Erfolg bei Ordnererstellung. ' + success);
        }, function (error) {
            // error
            console.log('Error bei Ordnererstellung. ' + error);
        });

    function downloadimage(){
      downloadinprogress=true;
      filename=pokedexid + ".png";
      
      if (filename.length==5){
        filename='00' + filename;
      }

      if (filename.length==6){
        filename='0' + filename;
      }
    
      url=baseurl;
      url=encodeURI(url + filename);
      targetPath = cordova.file.externalRootDirectory + "/PokedexHelperBilder/" + filename;
      var tempPokemon = {
            pokedexid: null,
            image: ''
      }

      //Bild Download
      $cordovaFileTransfer.download(url, targetPath, {Connection: "close"}, true).then(function (result) {
            $scope.currentfile=filename;
            tempPokemon.image=targetPath;
            tempPokemon.pokedexid=pokedexid;

            console.log(tempPokemon.image + " " + tempPokemon.pokedexid);
            pokedexDB.setPokemonImage(tempPokemon);

            console.log(filename + ' heruntergeladen.' + result);
            pokedexid=pokedexid + 1;
            console.log(result);
            downloadinprogress=false;
          }, function (error) {
            console.log('Error. ' + error.code + ' ' + error.constant);
          }, function (progress) {
            //downloadprogress=(progress.loaded / progress.total) * 100;
            $scope.progressprozent = Math.round(pokedexid / 721 * 100);
            $scope.progressval = pokedexid;
            $scope.currentfile=filename; //+ downloadprogress;
          });
      console.log(pokedexid);

      if (pokedexid<721){
        downloadtimer();
      }
      else{
        $scope.progressprozent = 'Download abgeschlossen! ' + $scope.progressprozent;
        $scope.DownloadisVisible=true;
        $scope.AbbrechenisVisible=false;
      }

      if (  $scope.startdownloadtimer==false){
        $scope.progressprozent = 'Download Abgebrochen! ' + $scope.progressprozent;
      }
  

    }

    //});

  function downloadtimer(){
    if ($scope.startdownloadtimer==true){


    $timeout(function(){
      if (downloadinprogress==false){
        downloadimage();
        console.log("downinprogress ist false");
      }else{
        console.log("downinprogress ist true");
        downloadtimer();
      }
     }, 500);
    }
  }
  downloadtimer();
  };


$scope.downloadabbrechen = function() {
  $scope.DownloadisVisible=true;
  $scope.AbbrechenisVisible=false;
  $scope.startdownloadtimer=false;
};  

});// End of Contract List Page  Controller.
