
angular.module('starter.services', [])

.factory('$database',['$cordovaSQLite', function($cordovaSQLite) {
  var self = this;
  self.db = null;
  var query;

  //this updateTable function is for ilustrative use here, it can be changed to manage all queries indeed.
  updateTable=function (table, column, value, condition) {
    if(!condition){
      query = "UPDATE " + table + " SET " + column + " = " + value;
    } else{
      query = "UPDATE " + table + " SET " + column + " = " + value + " WHERE " + condition;
    }
    return $cordovaSQLite.execute(self.db,query,[]).
        then(function(result) {
           console.log(query);
           query=null;
           return result;
        },function(error) {
                console.error(error);
                query=null;
                return error;
          });
  };

  return {
      initDB: function() {
        if(window.cordova){
          //self.db = sqlitePlugin.openDatabase({name: "data.db", location: 2, createFromLocation: 1});
          self.db = $cordovaSQLite.openDB({name:"pokedex.db", iosDatabaseLocation:'default'});
        } else {
          self.db = window.openDatabase('pokedex.db','1','my',800*1024); // only available when WebSQL is available in Browser
        }
        console.log('Database opened');
        return self.db;
      },
      setBuys: function(itemName,itemBild,itemBildtext,itemKaufdatum,itemappimgpath,itemappimgpublicpath) {
        query="INSERT INTO buys (Name, Bild, Bildtext, Kaufdatum, appimgpath, appimgpublicpath) VALUES (?,?,?,?,?,?);";
        return $cordovaSQLite.execute(self.db,query,[itemName,itemBild,itemBildtext,itemKaufdatum,itemappimgpath,itemappimgpublicpath]).
          then(function(result) {
            console.log("Gespeichert");
            query=null;
          }, function(error) {
            console.error(error);
        });
      },
      deleteBuy: function(id) {
        query="DELETE FROM buys WHERE ID = ?;";
        return $cordovaSQLite.execute(self.db,query,[id]).
          then(function(result) {
            console.log("Gelöscht");
            query=null;
          }, function(error) {
            console.error(error);
        });
      },
      isInitialRun: function(){
        var InitilaRun;
        query="SELECT InitialRun FROM InitialRun";
        return $cordovaSQLite.execute(self.db,query).
            then(function(result) {
              for(j=0;j<result.rows.length;j++){
                InitilaRun=result.rows.item(j).InitialRun;
              }
              console.log('isInitialRun aufgerufen wert : ' + InitilaRun);
              return InitilaRun;
              },function(e){
                console.error(e);
                return e;
              });
      },
      setInitialRun: function(InitilaRunBool) {
        query="UPDATE InitialRun Set InitialRun = ?;";
        return $cordovaSQLite.execute(self.db,query,[InitilaRunBool]).
          then(function(result) {
            console.log("Update erfolge auf " + InitilaRunBool);
            query=null;
          }, function(error) {
            console.error(error);
        });
      },
      setPokemonImage: function(Image,Pokedexid) {
        query="UPDATE tblPokemon Set POKEMONIMAGE = ? WHERE POKEDEXID = ?;";
        return $cordovaSQLite.execute(self.db,query,[Image,Pokedexid]).
          then(function(result) {
            console.log("Update erfolge auf " + Pokedexid + ' mit ' + Image);
            query=null;
          }, function(error) {
            console.error(error);
        });
      },
      getAllPokemon: function(){
        var ArrayQ=[];
        query="SELECT * FROM tblPokemon order by POKEDEXID ASC";
        return $cordovaSQLite.execute(self.db,query).
            then(function(result) {
              for(j=0;j<result.rows.length;j++){
                var List={};
                List.ID=result.rows.item(j).ID;
                List.POKEDEXID=result.rows.item(j).POKEDEXID;
                List.POKEMONNAME=result.rows.item(j).POKEMONNAME;
                List.POKEMONTYPA=result.rows.item(j).POKEMONTYPA;
                List.POKEMONTYPB=result.rows.item(j).POKEMONTYPB;
                List.POKEMONIMAGE=result.rows.item(j).POKEMONIMAGE;
                ArrayQ.push(List);
              }
              //console.log(ArrayQ);
              return ArrayQ;
              },function(e){
                console.error(error);
                return error;
              });
      }
    };
}]);