export default log

import Entry from '../entry'
import prompt from 'prompt'
import chrono from 'chrono-node'
import {write} from './output'
import chalk from 'chalk'
import db from '../db'

function log (yargs) {
  let argv = yargs
  .usage('Usage: tick log [options]')
  .option('d', {
    alias: 'date',
    describe: 'date for tick'
  })
  .option('m', {
    alias: 'message',
    describe: 'entry message. E.g. 8am-12pm fixing bugs #tickbin'
  })
  .help('h')
  .alias('h', 'help')
  .argv

  let {message, date} = argv
  date = chrono.parseDate(date)

  if (!message) {
    prompt.message = ''
    prompt.delimiter = ''
    prompt.start()
    prompt.get('message', function(err, res) {
      if (!err) createEntry(res.message, {date}) 
    })
  } else {
    createEntry(message, {date}) 
  }
}

function createEntry (message, opts = {}) {
  let entry = new Entry(message, opts)

  db.put(entry.toJSON())
  .then(doc => {
    if (!entry.duration) {
      throw('You must specify a time in your message')
    }

    console.log(chalk.bgGreen('saved'))
    write(entry)
  })
  .catch(err => {
    console.error(chalk.bgRed('error'), err)
  })
}


export default log

import Entry from '../entry'
import prompt from 'prompt'
import chrono from 'chrono-node'
import {write} from './output'
import chalk from 'chalk'
import db from '../db'

function log (yargs) {
  let argv = yargs
  .usage('Usage: tick log [options]')
  .option('d', {
    alias: 'date',
    describe: 'date for tick'
  })
  .option('m', {
    alias: 'message',
    describe: 'entry message. E.g. 8am-12pm fixing bugs #tickbin'
  })
  .help('h')
  .alias('h', 'help')
  .argv

  let {message, date} = argv
  date = chrono.parseDate(date)

  if (!message) {
    prompt.message = ''
    prompt.delimiter = ''
    prompt.start()
    prompt.get('message', function(err, res) {
      if (!err) createEntry(res.message, {date}) 
    })
  } else {
    createEntry(message, {date}) 
  }
}

function createEntry (message, opts = {}) {
  let entry = new Entry(message, opts)

  if (!entry.duration) {
    console.error(chalk.bgRed('error'), 'You must specify a time in your message')
    return
  }

  db.put(entry.toJSON())
  .then(doc => {
    console.log(chalk.bgGreen('saved'))
    write(entry)
  })
  .catch(err => {
    console.error(chalk.bgRed('error'), err)
  })
}


Check for duration before saving to db
Check for duration before saving to db
