/**
 * Created by ctalmacel on 12/21/15.
 */

var mysql = require('mysql');
var Q = require('q');
var modelUtil = require("../../lib/ModelDescription.js");

exports.createTable= function(persistenceStrategy,tableName,model){
    var query = 'CREATE TABLE IF NOT EXISTS '+tableName+'(';
    for(field in model){
        query+=field+' ';
        var type = model[field].type;
        var dbType = persistenceStrategy.getDatabaseType(type);
        if(dbType === 'varchar'){
            if(model[field]['length']){
                dbType += '('+model[field]['length']+') ';
            }else {
                dbType += '(30) ';
            }
        }

        if(dbType === 'int'){
            if(model[field]['length']){
                dbType += '('+model[field]['length']+') ';
            }else {
                dbType += '(10) ';
            }
        }
        query+=dbType;
        if(field.hasOwnProperty('default')){
            query+=' DEFAULT'+field.default;
        }
        query+=',';
    }
    for(field in model) {
        if (model[field].pk === true) {
            query += ' PRIMARY KEY (' + field + '),';
        }
    }
        query = query.slice(0,-1);
    query+=');';
    return query;
};

exports.insertRow = function(tableName,serializedData){

    var model = modelUtil.getModel(tableName);

    var query="REPLACE INTO "+tableName+" (";
    for (field in serializedData){
        query += field + ",";
    }
    query = query.slice(0, -1);
    query += ") VALUES (";



    for(var field in serializedData){
        query+= mysql.escape(serializedData[field])+',';
    }
    query = query.slice(0, -1);
    query+=');';
    return query;
};

exports.insertDataIntoTable = function(mysqlConnection,persistence,tableName,serializedData){
    var result = [];
    var runQuery = Q.nbind(mysqlConnection.query,mysqlConnection);
    serializedData.forEach(function(row,index){
        result.push(runQuery(exports.insertRow(mysqlConnection,persistence,tableName,row)));
    });
    return Q.all(result);
};

exports.createAndPopulateNewTable = function(mysqlConnection,persistence,tableName,model,serializedData){
    return exports.createNewTable(mysqlConnection,persistence,tableName,model).
    then(function(){return exports.insertDataIntoTable(mysqlConnection,persistence,tableName,serializedData,model)}).
    catch(function(err){console.log(err.stack);});
};

exports.createNewTable = function(mysqlConnection,persistence,tableName,model){
    return exports.dropTable(mysqlConnection,tableName).
    then(function(){return exports.createTable(mysqlConnection,persistence,tableName,model)}).
    catch(function(err){console.log(err.stack);});
};

exports.dropTable =function(tableName){
    return "DROP TABLE IF EXISTS " +tableName+";";
};

exports.deleteObject = function(typeName,serialized_id){
    return "DELETE from "+typeName+ " WHERE "+modelUtil.getPKField(typeName)+" = "+mysql.escape(serialized_id)+";";
}

exports.describeTable = function(typeName){
    return "DESCRIBE "+typeName;
}

exports.find = function(typeName,pkField,serializedPk){
    var query = 'SELECT * from ' + typeName + ' WHERE ' + pkField + " = " + mysql.escape(serializedPk)+";";
    return query
}

exports.update = function(typeName,pkField,serialisedPk,fields,values){
    var model = modelUtil.getModel(typeName);

    var query = 'UPDATE '+typeName+ " SET ";
    var length = fields.length;
    fields.forEach(function(field,index) {
            query += field+"=" +mysql.escape(values[index])+",";
        
    });
    query = query.slice(0,-1);
    query+=" WHERE "+pkField+"="+mysql.escape(serialisedPk)+";";

    return query;
}

exports.filter = function(typeName,filter){
    var query = "SELECT * from "+typeName+" ";
    var model = modelUtil.getModel(typeName);

    if(filter == undefined){
        return query+";";
    }
    query +="WHERE ";
    for(var field in filter){
        query += field + "="+mysql.escape(filter[field])+" AND ";
    }

    query = query.slice(0,-4);
    query+=";";
    return query;
}
@@ -1182,32 +1182,33 @@
    query+=');';%0A
+%0A
     return query
@@ -3897,24 +3897,342 @@
 in filter)%7B%0A
+        if(Array.isArray(filter%5Bfield%5D))%7B%0A            query+=%22( %22;%0A            filter%5Bfield%5D.forEach(function(acceptedValue)%7B%0A                query+=field+%22=%22+mysql.escape(acceptedValue)+%22 OR %22;%0A            %7D);%0A            query = query.slice(0,-3); //cut the last 'OR'%0A            query+=%22) AND %22;%0A        %7Delse%7B%0A    
         quer
@@ -4281,24 +4281,34 @@
 %5D)+%22 AND %22;%0A
+        %7D%0A
     %7D%0A%0A    q
@@ -4332,16 +4332,37 @@
 e(0,-4);
+ //cut the last 'AND'
 %0A    que
@@ -4369,28 +4369,31 @@
 ry+=%22;%22;%0A    return query;%0A%7D
+%0A%0A%0A

add support for filters containing 'OR' ; e.g. filter {"name":['n1','n2']} will retrieve all objects whose name is either 'n1' or 'n2' ... available only in mysql as of this moment