#!/usr/bin/env node

var yargs = require('yargs'),
    argv = yargs.argv,
    inquirer = require('inquirer'),
    gka = require("../lib/gka"),
    tpl = require("../lib/core/tpl"),

    pkg = require('../package.json'),
    _ = argv._;

var cmd = "";

if (_[0] === 'tool' || _[0] === 't' ) {
    cmd = "tool";

    argv = yargs
    .usage('\nUsage: gka tool <dir> <option>')
    .option('u', {
        boolean: true,
        alias : 'unique',
        describe: 'remove duplicates',
    })
    .option('c', {
        boolean: true,
        alias : 'crop',
        describe: 'crop images',
    })
    .option('m', {
        boolean: true,
        alias : 'mini',
        describe: 'minify images',
    })
    .option('s', {
        boolean: true,
        alias : 'sprites',
        describe: 'sprites images',
        type: "boolean"
    })
    .option('a', {
        default: 'binary-tree',
        choices: ['top-down', 'left-right', 'binary-tree', 'diagonal', 'alt-diagonal'],
        alias : 'algorithm',
        describe: 'sprites layout types',
    })
    .option('p', {
        type: "string",
        alias : 'prefix',
        describe: 'rename with prefix',
        coerce: function (arg) {
            return arg || "prefix-";
        }
    })
    .option('r', {
        boolean: true,
        default: false,
        alias : 'replace',
        describe: 'replace dir'
    })
    .option('i', {
        boolean: true,
        alias : 'info',
        describe: 'get images info',
    })
    .help('h')
    .example('gka tool E:\\img -s')
    .epilog('for more detailed instructions, visit https://github.com/joeyguo/gka')
    .version()
    .argv;

    if (argv.v || argv.version || argv.V) {
        console.log(pkg.version);
        return;
    }

    var dir = _[1] || argv.d;
    if (!dir) {
        console.log();
        console.log('[error]: ' + 'gka tool need dir !');
        console.log('----------------------------');
        yargs.showHelp();
        return;
    }

    gka(dir, {
        // c
        crop: argv.crop,
        // s
        sprites: argv.sprites,
        // m 
        mini: argv.mini,
        // r
        unique: argv.unique,
        // i
        info: argv.info,
        // tpl
        tpl: argv.t || argv.tpl || argv.template,
        // p
        prefix: argv.prefix,
        // f
        frameduration: argv.frameduration,
        // a
        algorithm: argv.algorithm,
        // r
        replace: argv.replace,
        cmd: cmd,
    });

} else {
    argv = yargs
    .option('u', {
        boolean: true,
        alias : 'unique',
        default: true,
        describe: 'remove duplicates'
    })
    // .option('s', {
    //     boolean: true,
    //     alias : 'sprites',
    //     describe: 'sprites images',
    //     type: "boolean"
    // })
    // .option('c', {
    //     boolean: true,
    //     alias : 'crop',
    //     describe: 'crop images',
    // })
    .option('m', {
        boolean: true,
        alias : 'mini',
        describe: 'minify images',
    })
    .option('p', {
        alias : 'prefix',
        describe: 'rename with prefix',
        type: "string",
        coerce: function (arg) {
            return arg || "prefix-";
        }
    })
    .option('f', {
        alias : 'frameduration',
        describe: 'frame duration',
        type: "number",
    })
    .option('t', {
        alias : 'template',
        describe: 'html/js/css template',
        type: "string",
        // demandOption: true,
        default: "n",
        coerce: function (arg) {
            return arg //|| "n";
        }
    })
    .option('i', {
        boolean: true,
        alias : 'info',
        describe: 'get images info',
    })
    .option('a', {
        default: 'binary-tree',
        choices: ['top-down', 'left-right', 'binary-tree', 'diagonal', 'alt-diagonal'],
        alias : 'algorithm',
        describe: 'sprites layout types',
    })
    .help('h')
    .alias('h', 'help')
    .usage('\nUsage: \n  gka <dir> [options] \n  gka <cmd> <dir> [options]')
    .example('gka E:\\img')
    .epilog('for more detailed instructions, visit https://github.com/joeyguo/gka')
    .version()
    .command('tool', 'images processing tools')
    .argv;

    if (argv.v || argv.version || argv.V) {
        console.log(pkg.version);
        return;
    }

    var dir = _[0] || argv.d;
    if (!dir) {
        console.log();
        console.log('[error]: ' + 'gka need dir !');
        console.log('----------------------------');
        yargs.showHelp();
        return;
    }

    var tplMap = tpl();
    var template = argv.template;
    var tplList = Object.keys(tplMap).map(function(item){
            return item.substring(8);
        });
    if (template === "") {

        inquirer.prompt([{
            type: 'list',
            name: 'template',
            message: 'which template do you like: ',
            choices: tplList,
            filter: function (val) {
              return val.toLowerCase();
            }
          }]).then((answers) => {
            gka(dir, {
                // c
                crop: argv.c,
                // s
                sprites: argv.s,
                // m 
                mini: argv.mini,
                // u
                unique: argv.unique,
                // i
                info: argv.info,
                // tpl
                tpl: tplMap['gka-tpl-' + answers.template],
                tplName: answers.template,
                tplList: tplList,
                // p
                prefix: argv.prefix,
                // f
                frameduration: argv.frameduration,
                // a
                algorithm: argv.algorithm
            });
        })
    } else {
        // 内置别名
        template = template === 'c'? 'crop': template;
        template = template === 's'? 'sprites': template;
        template = template === 'n'? 'normal': template;
        
        var tpl = tplMap['gka-tpl-' + template];

        gka(dir, {
            // c
            crop: argv.c,
            // s
            sprites: argv.s,
            // m 
            mini: argv.mini,
            // u
            unique: argv.unique,
            // i
            info: argv.info,
            // tpl
            tpl: tpl,
            tplName: template,
            tplList: tplList,
            // p
            prefix: argv.prefix,
            // f
            frameduration: argv.frameduration,
            // a
            algorithm: argv.algorithm
        });
    }
    
}

// console.log(argv);
// return;
// console.log(typeof argv.s);


@@ -4009,13 +4009,12 @@
 gka 
-%3Ccmd%3E
+tool
  %3Cdi
@@ -4158,16 +4158,19 @@
 on()%0A   
+ //
  .comman

remove command info