
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("pokedex.db");
        } 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);
        });
      },
      getAllBuys: function(){
        var ArrayQ=[];
        query="SELECT * FROM buys order by id DESC";
        return $cordovaSQLite.execute(self.db,query).
            then(function(result) {
              for(j=0;j<result.rows.length;j++){
                var List={};
                //console.log(result.rows.item(j));
                List.id=result.rows.item(j).id;
                List.name=result.rows.item(j).Name;
                List.bild=result.rows.item(j).Bild;
                List.bildtext=result.rows.item(j).Bildtext;
                List.kaufdatum=result.rows.item(j).Kaufdatum;
                List.appimgpath=result.rows.item(j).appimgpath;
                List.appimgpublicpath=result.rows.item(j).appimgpublicpath;
                ArrayQ.push(List);
              }
              //console.log(ArrayQ);
              return ArrayQ;
              },function(e){
                console.error(error);
                return error;
              });
      }
    };
}]);
