/**
 * `list` type prompt
 */

var _ = require("lodash");
var util = require("util");
var clc = require("cli-color");
var Base = require("./base");


/**
 * Module exports
 */

module.exports = Prompt;


/**
 * Constructor
 */

function Prompt() {
  Base.apply( this, arguments );

  if (!this.opt.choices) {
    this.throwParamError("choices");
  }

  this.firstRender = true;
  this.selected = 0;

  var def = this.opt.default;
  if ( _.isNumber(def) && def >= 0 && def < this.opt.choices.length ) {
    this.selected = def;
  }

  // Make sure no default is set (so it won't be printed)
  this.opt.default = null;

  return this;
}
util.inherits( Prompt, Base );


/**
 * Start the Inquiry session
 * @param  {Function} cb      Callback when prompt is done
 * @return {this}
 */

Prompt.prototype._run = function( cb ) {
  this.done = cb;

  // Move the selected marker on keypress
  this.rl.on( "keypress", this.onKeypress.bind(this) );

  // Once user confirm (enter key)
  this.rl.once( "line", this.onSubmit.bind(this) );

  // Init the prompt
  this.render();
  this.hideCursor();

  // Prevent user from writing
  this.rl.output.mute();

  return this;
};


/**
 * Render the prompt to screen
 * @return {Prompt} self
 */

Prompt.prototype.render = function() {

  // Render question
  var message    = this.getQuestion();
  var choicesStr = this.getChoices();

  if ( this.firstRender ) {
    message += clc.blackBright("(Use arrow keys)");
  }

  // Render choices or answer depending on the state
  if ( this.status === "answered" ) {
    message += clc.cyan( this.opt.choices[this.selected].name ) + "\n";
  } else {
    message += choicesStr;
  }

  this.firstRender = false;

  var msgLines = message.split(/\n/);
  this.height = msgLines.length;

  // Write message to screen and setPrompt to control backspace
  this.rl.setPrompt( _.last(msgLines) );
  this.write( message );

  return this;
};


/**
 * Generate the prompt choices string
 * @return {String}  Choices string
 */

Prompt.prototype.getChoices = function() {
  var output = "";

  this.opt.choices.forEach(function( choice, i ) {
    output += "\n  ";
    if ( i === this.selected ) {
      output += clc.cyan( "[X] " + choice.name );
    } else {
      output += "[ ] " + choice.name;
    }
    output += " ";
  }.bind(this));

  return output;
};


/**
 * When user press `enter` key
 */

Prompt.prototype.onSubmit = function() {
  var choice = this.opt.choices[ this.selected ];
  this.status = "answered";

  // Rerender prompt
  this.rl.output.unmute();
  this.clean().render();

  this.showCursor();

  this.rl.removeAllListeners("keypress");
  this.done( choice.value );
};


/**
 * When user press a key
 */

Prompt.prototype.onKeypress = function( s, key ) {
  // Only process up and down key
  if ( !key || (key.name !== "up" && key.name !== "down") ) return;

  this.rl.output.unmute();

  var len = this.opt.choices.length;
  if ( key.name === "up" ) {
    (this.selected > 0) ? this.selected-- : (this.selected = len - 1);
  } else if ( key.name === "down" ) {
    (this.selected < len - 1) ? this.selected++ : (this.selected = 0);
  }

  // Rerender
  this.clean().render();

  this.rl.output.mute();
};

@@ -2130,21 +2130,34 @@
  %22;%0A    
-if ( 
+var isSelected = (
 i === th
@@ -2171,45 +2171,56 @@
 cted
- ) %7B
+);
 %0A    
-  output += clc.cyan( %22%5BX%5D %22
+var line = this.getCheckbox( isSelected )
  + c
@@ -2233,61 +2233,63 @@
 name
- )
 ;%0A    
-%7D else %7B%0A      output += %22%5B %5D %22 + choice.name
+if ( isSelected ) %7B%0A      line = clc.cyan( line )
 ;%0A  
@@ -2305,16 +2305,23 @@
 utput +=
+ line +
  %22 %22;%0A  
@@ -3221,12 +3221,263 @@
 .mute();%0A%7D;%0A
+%0A%0A/**%0A * Get the checkbox%0A * @param  %7BBoolean%7D checked - add a X or not to the checkbox%0A */%0A%0APrompt.prototype.getCheckbox = function( checked ) %7B%0A  var check = (process.platform === %22win32%22) ? %22X%22 : %22%E2%9C%94%22;%0A  return checked ? %22%5B%22+ check +%22%5D%22 : %22%5B %5D%22;%0A%7D;%0A

Implement utf-8 check icons in list (Only for unix system, windows fallback to 'X') - related #31 #14