/*******************************************************************************
 * Copyright 2013-2014 Aerospike, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

/*******************************************************************************
 *
 * Select bins of a record.
 * 
 ******************************************************************************/

var fs = require('fs');
var aerospike = require('aerospike');
var yargs = require('yargs');

var Policy = aerospike.policy;
var Status = aerospike.status;

/*******************************************************************************
 *
 * Options parsing
 * 
 ******************************************************************************/

var argp = yargs
    .usage("$0 [options] bin index type")
    .options({
        help: {
            boolean: true,
            describe: "Display this message."
        },
        profile: {
            boolean: true,
            describe: "Profile the operation."
        },
        host: {
            alias: "h",
            default: "127.0.0.1",
            describe: "Aerospike database address."
        },
        port: {
            alias: "p",
            default: 3000,
            describe: "Aerospike database port."
        },
        timeout: {
            alias: "t",
            default: 10,
            describe: "Timeout in milliseconds."
        },
        'log-level': {
            alias: "l",
            default: aerospike.log.INFO,
            describe: "Log level [0-5]"
        },
        'log-file': {
            default: undefined,
            describe: "Path to a file send log messages to."
        },
        namespace: {
            alias: "n",
            default: "test",
            describe: "Namespace for the keys."
        },
        set: {
            alias: "s",
            default: "demo",
            describe: "Set for the keys."
        },
        'key': {
            boolean: true,
            default: true,
            describe: "Display the record's key."
        },
        'metadata': {
            boolean: true,
            default: true,
            describe: "Display the record's metadata."
        },
        'bins': {
            boolean: true,
            default: true,
            describe: "Display the record's bins."
        }
    });

var argv  = argp.argv;
var bin   = argv._.shift();
var index = argv._.shift();
var type  = argv._;

if ( argv.help === true ) {
    argp.showHelp();
    process.exit(0);
}

if ( ! bin) {
    console.error("Error: Please provide a bin to be indexed");
    console.error();
    argp.showHelp();
    process.exit(1);
}

if ( !index ) {
    console.error("Error: Please provide a index name");
    console.error();
    argp.showHelp();
    process.exit(1);
}

if ( ! type) {
    console.error("Error: Please provide a type of index to be created");
    console.error();
    argp.showHelp();
    process.exit(1);
}
/*******************************************************************************
 *
 * Configure the client.
 * 
 ******************************************************************************/

config = {

    // the hosts to attempt to connect with.
    hosts: [
        { addr: argv.host, port: argv.port }
    ],
    
    // log configuration
    log: {
        level: argv['log-level'],
        file: argv['log-file'] ? fs.openSync(argv['log-file'], "a") : 2
    },

    // default policies
    policies: {
        timeout: argv.timeout
    }
};

/*******************************************************************************
 *
 * Perform the operation
 * 
 ******************************************************************************/

function format(o) {
    return JSON.stringify(o, null, '    ');
}

aerospike.client(config).connect(function (err, client) {

    if ( err.code != Status.AEROSPIKE_OK ) {
        console.error("Error: Aerospike server connection error. ", err.message);
        process.exit(1);
    }

    //
    // Perform the operation
    //

    var options = {
        ns:  argv.namespace,
        set: argv.set,
        bin : bin,
		index: index
    };

    if ( argv.profile ) {
        console.time("indexIntegerCreate");
    }

    callback = function(err) {     
        var exitCode = 0;

        switch ( err.code ) {
			case Status.AEROSPIKE_OK:
				console.log("Index Creation Success");
                break;
                
            default:
				console.log("Index creation failed ", err);
                console.error("Error: " + err.message);
                exitCode = 1;
                break;
			}

			if ( argv.profile ) {
				console.log("---");
				console.timeEnd("createIntegerIndex");
			}

			process.exit(exitCode);
		}
	if( type == "integer" )
	{
		client.createIntegerIndex(options, callback);
	}
	else if( type == "string" )
	{
		client.createStringIndex(options, callback);
	}
	else
	{
		console.log("Only integer and string indices are supported - unrecognized type");
	}
});
