var redis = require("redis"),
    util = require("util"),
    commands;

exports.debug_mode = redis.debug_mode;

// helper borrowed from node_redis
function to_array(args) {
    var i;
    var len = args.length;
    var arr = new Array(len);
    for (i = 0; i < len; i += 1) {
        arr[i] = args[i];
    }
    return arr;
}

// new commands that we'll be adding
commands = [
    "CHANGEDB",
    "DUMP", "DESC",
    "NORM", "DENORM"
];

// merge these in with the RedisClient prototype in both upper and lower case
commands.forEach(function (command) {
    redis.RedisClient.prototype[command] = function () {
        var args = to_array(arguments);
        args.unshift(command); // put command at the beginning

        // remove this when everything works
        console.log("Sending new command " + command +
                    " with args " + JSON.stringify(args));

        this.send_command.apply(this, args);
    };
    redis.RedisClient.prototype[command.toLowerCase()] =
        redis.RedisClient.prototype[command];
});


// CREATE
redis.RedisClient.prototype["CREATE"] = function () {
    var args = to_array(arguments), mod_args, 
        first_arg = args[0].toLowerCase(), command, sargs;

    if (first_arg === "table") {
        mod_args = "TABLE " + args[1] + " (" + args[2] + ")";
    } else if (first_arg === "index") {
        mod_args = "INDEX " + args[1] + " ON " + args[2] + " (" + args[3] + ")";
    } else { // usage error
        throw new Error("Bad args to \"CREATE\" " + args[0] +
                         ", must be either \"TABLE\" OR \"INDEX\"");
    }

    // TODO - error on args.length > 4 || args.length < 2;

    command = "CREATE";
    sargs = mod_args.split(' ');
    sargs.unshift(command); // put command at the beginning
    if (typeof args[args.length - 1] === "function") {
        sargs.push(args[args.length - 1]);
    }

    //console.log("Sending " + command + " with args ", util.inspect(sargs));
    console.log("Sending " + command + " with args " + JSON.stringify(sargs));

    this.send_command.apply(this, sargs);
};
redis.RedisClient.prototype["create"] = redis.RedisClient.prototype["CREATE"];

// DROP
redis.RedisClient.prototype["DROP"] = function () {
    var args = to_array(arguments);
    var mod_args;
    if (args[0].toLowerCase() === "table") {
        mod_args = "TABLE " + args[1];
    } else if (args[0].toLowerCase() === "index") {
        mod_args = "INDEX " + args[1];
    } else { // usage error
        throw new Error("Bad args to \"DROP\" " + args[0] +
                         ", must be either \"TABLE\" OR \"INDEX\"");
    }
    var command = "DROP";
    var sargs   = mod_args.split(' ');;
    sargs.unshift(command); // put command at the beginning
    if (typeof args[args.length - 1] === "function") {
        sargs.push(args[args.length - 1]);
    }
    console.log("Sending " + command + " with args " + JSON.stringify(sargs));
    this.send_command.apply(this, sargs);
};
redis.RedisClient.prototype["drop"] = redis.RedisClient.prototype["DROP"];

// SELECT
redis.RedisClient.prototype["SELECT"] = function () {
    var args = to_array(arguments);
    var mod_args;
    if (args.length != 1) { // rewrite Redisql SELECT * FROM tbl WHERE id = 4
        mod_args = args[0] + " FROM "  + args[1] + " WHERE " + args[2];
    } else { // redis SELECT DB
        mod_args = arguments;
    }
    var command = "SELECT";
    var sargs   = mod_args.split(' ');;
    sargs.unshift(command); // put command at the beginning
    if (typeof args[args.length - 1] === "function") {
        sargs.push(args[args.length - 1]);
    }
    console.log("Sending " + command + " with args " + JSON.stringify(sargs));
    this.send_command.apply(this, sargs);
};
redis.RedisClient.prototype["select"] = redis.RedisClient.prototype["SELECT"];

// SCANSELECT
redis.RedisClient.prototype["SCANSELECT"] = function () {
    var args     = to_array(arguments);
    var mod_args = args[0] + " FROM "  + args[1];;
    if (args.length > 3) {
        mod_args += " WHERE " + args[2];
    }
    var command = "SCANSELECT";
    var sargs   = mod_args.split(' ');;
    sargs.unshift(command); // put command at the beginning
    if (typeof args[args.length - 1] === "function") {
        sargs.push(args[args.length - 1]);
    }
    console.log("Sending " + command + " with args " + JSON.stringify(sargs));
    this.send_command.apply(this, sargs);
};
redis.RedisClient.prototype["scanselect"] = redis.RedisClient.prototype["SCANSELECT"];

// INSERT
redis.RedisClient.prototype["INSERT"] = function () {
    var args = to_array(arguments),
        mod_args = "INTO " + args[0] + " VALUES",
        command  = "INSERT",
        sargs    = mod_args.split(' ');;
        
    sargs.unshift(command); // put command at the beginning
    sargs.push(args[1]);    // put val_list at end as single argument
    if (typeof args[args.length - 1] === "function") {
        sargs.push(args[args.length - 1]);
    }
    // TODO - error if args length is invalid
    console.log("Sending " + command + " with args " + JSON.stringify(sargs));
    this.send_command.apply(this, sargs);
};
redis.RedisClient.prototype["insert"] = redis.RedisClient.prototype["INSERT"];

// DELETE
redis.RedisClient.prototype["DELETE"] = function () {
    var args     = to_array(arguments);
    var mod_args = "FROM " + args[0] + " WHERE "  + args[1];
    var command  = "DELETE";
    var sargs    = mod_args.split(' ');;
    sargs.unshift(command); // put command at the beginning
    if (typeof args[args.length - 1] === "function") {
        sargs.push(args[args.length - 1]);
    }
    console.log("Sending " + command + " with args " + JSON.stringify(sargs));
    this.send_command.apply(this, sargs);
};
redis.RedisClient.prototype["delete"] = redis.RedisClient.prototype["DELETE"];

// UPDATE
redis.RedisClient.prototype["UPDATE"] = function () {
    var args     = to_array(arguments);
    var mod_args = args[0] + " SET"
    var sargs    = mod_args.split(' ');;
    sargs.push(args[1]); // push val_list at end as single argument
    sargs.push("WHERE");
    var wargs = args[2].split(' ');
    for (var i = 0; i < wargs.length; i++) {
        sargs.push(wargs[i]);
    };
    var command = "UPDATE";
    sargs.unshift(command); // put command at the beginning
    if (typeof args[args.length - 1] === "function") {
        sargs.push(args[args.length - 1]);
    }
    console.log("Sending " + command + " with args " + JSON.stringify(sargs));
    this.send_command.apply(this, sargs);
};
redis.RedisClient.prototype["update"] = redis.RedisClient.prototype["UPDATE"];

exports.createClient = function (port_arg, host_arg) {
    return redis.createClient(port_arg, host_arg);
};

exports.print = redis.print;