import { Command }    from 'commander';
import SessionManager from '../managers/SessionManager';
import Config         from '../Config/Config';
import GitlabManager  from '../managers/GitlabManager';
import chalk          from 'chalk';
import inquirer       from 'inquirer';


class CommanderApp {
    program = new Command();

    constructor() {
        this.program
        .name('dojo')
        .description('CLI for the Dojo application')
        .version('1.0.0 dev')
        .showHelpAfterError()
        .configureHelp({
                           showGlobalOptions: true,
                           sortOptions      : true,
                           sortSubcommands  : true
                       })
        .option('-H, --host <string>', 'override the Dojo API endpoint.', Config.apiURL);

        this.program.on('option:host', () => {
            Config.apiURL = this.program.opts().host;
        });

        this.loginCommand();
        this.gitlabRegisterCommand();
        this.testAccessesCommand();

        this.program.parse();
    }

    loginCommand() {
        this.program.command('login')
        .description('Login into the application')
        .requiredOption('-u, --user <string>', '[required] username to use when connecting to server.')
        .option('-p, --password <string>', 'password to use when connecting to server. If password is not given it\'s asked.')
        .action(async (options) => {
            if ( !options.password ) {
                options.password = (await inquirer.prompt({
                                                              type   : 'password',
                                                              name   : 'password',
                                                              message: 'Please enter your password',
                                                              mask   : ''
                                                          })).password;
            }

            console.log(chalk.cyan('Please wait while we are logging in you to Dojo...'));

            await SessionManager.login(options.user, options.password);
        });
    }

    gitlabRegisterCommand() {
        this.program.command('gitlab_register')
        .description('Register the gitlab token')
        .argument('<token>', 'Personal access token from GitLab with api scope.')
        .action(async (token) => {
            console.log(chalk.cyan('Please wait while we are testing your Gitlab token...'));

            GitlabManager.token = token;
            await GitlabManager.testToken();
        });
    }

    testAccessesCommand() {
        this.program.command('test_accesses')
        .description('Test availability of registered Dojo API and Gitlab API sessions')
        .action(async _ => {
            console.log(chalk.cyan('Please wait while we are testing your Dojo accesses...'));
            await SessionManager.testSession();

            console.log(chalk.cyan('Please wait while we are testing your Gitlab accesses...'));
            await GitlabManager.testToken();
        });
    }
}


export default CommanderApp;