#!/usr/bin/env node
'use strict';

var chalk = require('chalk');
var figures = require('figures');
var inquirer = require('inquirer');
var meow = require('meow');
var opn = require('opn');
var githubSearchRepos = require('./');

var cli = meow({
	help: [
		'Usage',
		'  $ github-search-repos gulp',
		'  $ github-search-repos gulp+languge:javascript',
		'',
		'Options',
		'  -i, --interactive    Show results in interactive interface',
		'  -s, --sort           Sort results by either `stars`, `forks` or `updated`',
		'  -t, --token          GitHub authentication token'
	].join('\n')
}, {
	boolean: ['interactive'],
	string: [
		'sort',
		'token'
	],
	alias: {
		i: 'interactive',
		s: 'sort',
		t: 'token'
	}
});

function listResults(repos) {
	inquirer.prompt([{
		name: 'results',
		message: 'Search results:',
		type: 'list',
		choices: repos.map(function (repo) {
			var stars = repo.stargazers_count + figures.star;

			return {
				name: repo.full_name + ' ' + chalk.dim(stars),
				value: repo.html_url
			};
		})
	}], function (answer) {
		opn(answer.results);
		process.exit();
	});
}

function init() {
	inquirer.prompt([{
		name: 'query',
		message: 'Search for GitHub repositories'
	}], function (answer) {
		githubSearchRepos(answer.query, function (err, data) {
			if (err) {
				console.error(err.message);
				process.exit(1);
			}

			listResults(data.items);
		});
	});
}

if (!cli.input.length) {
	init();
	return;
}

if (cli.flags.interactive) {
	githubSearchRepos(cli.input[0], cli.flags, function (err, data) {
		if (err) {
			console.error(err.message);
			process.exit(1);
		}

		listResults(data.items);
	});

	return;
}

githubSearchRepos(cli.input[0], cli.flags, function (err, data) {
	if (err) {
		console.error(err.message);
		process.exit(1);
	}

	data.items.forEach(function (repo) {
		var stars = repo.stargazers_count + figures.star;

		console.log([
			repo.full_name + ' ' + chalk.dim(stars),
			repo.description,
			chalk.dim(repo.html_url),
			''
		].join('\n'));
	});
});

@@ -923,45 +923,124 @@
 ar;%0A
-%0A%09%09%09return %7B%0A%09%09%09%09name: repo.
+%09%09%09var fullName = repo.full_name.split('/');%0A%0A%09%09%09return %7B%0A%09%09%09%09name: fullName%5B0%5D + '/' + chalk.blue.bold(
 full
-_n
+N
 ame
+%5B1%5D)
  + '
@@ -1949,42 +1949,120 @@
 ar;%0A
-%0A%09%09console.log(%5B%0A%09%09%09repo.
+%09%09var fullName = repo.full_name.split('/');%0A%0A%09%09console.log(%5B%0A%09%09%09fullName%5B0%5D + '/' + chalk.blue.bold(
 full
-_n
+N
 ame
+%5B1%5D)
  + '

make the repo title blue and bold