#! /usr/bin/env node

require('colors')

var charm = require('charm')(process.stdout)
var keypress = require('keypress')(process.stdin)
var opts = require('optimist').argv
var fs = require('fs')
var iq = require('insert-queue')
var js = require('hipster/highlight/javascript')

var file = opts._[0]
var text = require('fs').readFileSync(file, 'utf-8')
var slides = text.split(/---+\n/)
if(slides.length <= 1) {
  console.error('markdown should be split into slides by --- (hdiv)')
  process.exit(1)
}
//console.log(slides)
//return

var mleft = 5
var mtop  = 2

function show () {
  if(index < 0) index = 0
  if(index >= slides.length) index = slides.length - 1

  var s = stats(slides[index])

  charm
    .reset()
    .position(1, mtop)
    .write(indent(slides[index], mleft))
    .position(mleft, process.stdout.rows - 1)
}
var index = 0
show(index)

process.stdin.setRawMode(true)
process.stdin.resume()

process.stdin.on('keypress', function (ch, key) {

  if(!key) return
  if(key.ctrl && /c|q/.test(key.name))
    charm.reset(), process.exit(0)
  else if(key.name == 'left')
    show(--index)
  else if(key.name == 'right')
    show(index ++)
  else if(key.name == 'home')
    show(index = 0)
  else if(key.name == 'end')
    show(index = slides.length - 1)
})

function stats (slide) {
  var lines = slide.split('\n')
  var max = 0
  lines.forEach(function (line) {
    if(line.length > max)
      max = line.length
  })
  return {
    height: lines.length,
    width:  max
  }
}

function indent(slide, indent) {
  var space = ''
  while (indent--)
    space += ' '

  var code = false
  return slide.split('\n').map(function (l) {
    if(/^#/.test(l))
      l = l.bold
    if(/^```/.test(l))
      code = !code
    if(code) {
      var q = iq(l)
      js.highlight(q)
      l = q.apply()
    }
    return space + l
  }).join('\n')
}


@@ -1074,16 +1074,54 @@
 = 'left'
+ %7C%7C key.name == 'h' %7C%7C key.name == 'j'
 )%0A    sh
@@ -1161,16 +1161,54 @@
  'right'
+ %7C%7C key.name == 'k' %7C%7C key.name == 'l'
 )%0A    sh

add hjkl bindings to change slides