import {ICommand} from "./ICommand";
import {OptParserUtil} from "./OptParserUtil";
import {IChallengeProvider} from "../challenges/IChallengeProvider";
import {IConsoleFormatter} from "../console/IConsoleFormatter";
import commander = require('commander');

export class InfoCommand implements ICommand {

    constructor(protected challengeProvider: IChallengeProvider,
                protected consoleFormatter: IConsoleFormatter) {
    }

    /**
     * reddit-do info <challenge_no>
     *     Shows information about a specific challenge.
     *
     * @param program
     * @returns {ICommand}
     */
    register(program: commander.ICommand): commander.ICommand {
        return program
            .command('info')
            .description('print information about a challenge')
            .option('-d, --difficulty [difficulty]', 'filter for difficulty (hard,inter,easy)')
            .arguments('<challenge_no>')
            .action((id: string, program: commander.ICommand) => {
                if (!/^[0-9]+$/.test(id)) {
                    throw new Error('invalid challenge_no');
                }

                let difficulty = OptParserUtil.parseDifficulty(program['difficulty']);
                return this.challengeProvider.get(parseInt(id), difficulty)
                    .then(challenges => {
                        process.stdout.write(this.consoleFormatter.format(challenges, false, [ "id", "title", "difficulty", "link" ]));
                        process.exit(0);
                    });
            });
    }
}