export default list

import Entry from '../entry'
import {getOutputs} from './output'
import moment from 'moment'
import _ from 'lodash'
import chalk from 'chalk'
import format from '../time'
import chrono from 'chrono-node'
import db from '../db'
import { parseDateRange } from '../query'
import Query from '../query'
import csvStringify from 'csv-stringify'

function list (yargs) {
  let argv = yargs
  .usage('Usage: tick list [options]')
  .example('tick list -t sometag -d "Jan 1-31"')
  .example('tick list -d "Jan 1-15" -f csv')
  .option('t', {
    alias: 'tag',
    describe: 'tags to filter as boolean AND (no # symbol - e.g. -t tag1 tag2)',
    type: 'array'
  })
  .option('d', {
    alias: 'date',
    describe: 'date range to filter entries or number of days to list',
    type: 'string'
  })
  .option('f', {
    alias: 'format',
    describe: 'format to display data in',
    choices: ['csv', 'group'],
    type: 'string'
  })
  .help('h')
  .alias('h', 'help')
  .argv

  let { start, end } = parseDateRange(argv.date)
  start = moment(start).toArray()
  end = moment(end).toArray()

  const query = new Query(db).findEntries({ start, end, tags: argv.tag })

  switch (argv.format) {
    case 'csv':
      query.exec()
        .then(ticks => {
          const data = []
          _.each(ticks, t => {
            const tick = _.omit(getOutputs(t), ['simple', 'detailed'])
            data.push(tick)
          })
          csvStringify(data, { header: true, eof: false }, (err, output) => {
            console.log(output)
            return ticks
          })
        })
        .then(writeDefaultMessage)
      break
    default:
      query.groupByDate()
        .exec()
        .then(groups => {
          _.each(groups, writeEntryGroup)
          return groups
        })
        .then(writeDefaultMessage)
      break
  }
}

function writeDefaultMessage (arr) {
  if (arr.length === 0)
    console.log('You have no recent entries in tickbin. '
      + 'Create some with \'tick log\'')
}

function writeEntryGroup (group) {
  const date = moment(group.date).format('ddd, MMM DD, YYYY')
  const duration = format(group.minutes)
  console.log(`${chalk.yellow(date)} ${chalk.green(duration)}`)
  group.ticks.forEach(t => { console.log(getOutputs(t).simple) })
}

@@ -1248,55 +1248,112 @@
 hen(
-ticks =%3E %7B%0A          const data = %5B%5D%0A  
+writeCSV)%0A        .then(writeDefaultMessage)%0A      break%0A    default:%0A      query.groupByDate()%0A
         
 _.ea
@@ -1352,53 +1352,173 @@
     
-_
 .e
-ach(ticks, t =%3E %7B%0A            const tick =
+xec()%0A        .then(writeGroup)%0A        .then(writeDefaultMessage)%0A      break%0A  %7D%0A%7D%0A%0Afunction writeCSV (results) %7B%0A  const data = _.map(results, t =%3E %7B%0A    return
  _.o
@@ -1566,55 +1566,12 @@
 )%0A  
-          data.push(tick)%0A          %7D)%0A        
+%7D)%0A%0A
   cs
@@ -1636,24 +1636,16 @@
 t) =%3E %7B%0A
-        
     cons
@@ -1660,24 +1660,16 @@
 output)%0A
-        
     retu
@@ -1675,174 +1675,56 @@
 urn 
-ticks%0A          %7D)%0A        %7D)%0A        .then(writeDefaultMessage)%0A      break%0A    default:%0A      query.groupByDate()%0A        .exec()%0A        .then(groups =%3E %7B%0A        
+results%0A  %7D)%0A%7D%0A%0Afunction writeGroup (results) %7B%0A
   _.
@@ -1728,21 +1728,22 @@
  _.each(
-group
+result
 s, write
@@ -1760,91 +1760,22 @@
 )%0A  
-        return groups%0A        %7D)%0A        .then(writeDefaultMessage)%0A      break%0A  %7D
+return results
 %0A%7D%0A%0A

Clean up switch logic