import Duration from 'duration'
import shortid from 'shortid'
import moment from 'moment'
import parser from './parser'

export const hashPattern = /(#\w+[\w-]*)/g
export const version = 4
export default class Entry {
  constructor(message, opts = {}) {
    let {
      date = new Date(), 
    } = opts

    if (typeof message === 'object')
      return this._fromJSON(message)

    this.version = version
    this._id = shortid.generate()
    this.message = message
    this.ref = date
    this.parse(message, date)
    this.parseTags(message)
  }

  _fromJSON(doc) {
    Object.assign(this, doc)
    const start = new Date(this.start)
    const end = new Date(this.end)
    const text = this.time
    this.setDates({start, end, text})
    this.tags = new Set(doc.tags)
    return this
  }

  parse(msg, date) {
    let d = parser(msg, date)
    if (d.isValid) this.setDates(d)
  }

  parseTags(message) {
    // Set makes things unique
    this.tags = new Set(message.match(hashPattern))
  }

  setDates(opts) {
    this.hasDates = true
    this.start = opts.start
    this.startArr = moment(this.start).toArray()
    this.end = opts.end
    this.endArr = moment(this.end).toArray()
    this.time = opts.text
    this.duration = new Duration(this.start, this.end)
  }

  getDates() {
    let start = this.start
    let end = this.end
    return { start, end }
  }

  toJSON() {
    return {
      _id: this._id,
      version: this.version,
      ref: this.ref,
      message: this.message,
      hasDates: this.hasDates,
      start: this.start,
      startArr: this.startArr,
      end: this.end,
      endArr: this.endArr,
      time: this.time,
      tags: [...this.tags],
      duration: this.duration ? {
        seconds: this.duration.seconds,
      } : null
    } 
  }

  static fromJSON(doc) {
    let e = new Entry(doc)

    return e
  }
}

import Duration from 'duration'
import shortid from 'shortid'
import moment from 'moment'
import parser from './parser'
import config from './config'

export const hashPattern = /(#\w+[\w-]*)/g
export const version = 4
export default class Entry {
  constructor(message, opts = {}) {
    let {
      date = new Date(), 
    } = opts

    let {
      user = null
    } = config

    if (typeof message === 'object')
      return this._fromJSON(message)

    this.user = user
    this.version = version
    this._id = shortid.generate()
    this.message = message
    this.ref = date
    this.parse(message, date)
    this.parseTags(message)
  }

  _fromJSON(doc) {
    Object.assign(this, doc)
    const start = new Date(this.start)
    const end = new Date(this.end)
    const text = this.time
    this.setDates({start, end, text})
    this.tags = new Set(doc.tags)
    return this
  }

  parse(msg, date) {
    let d = parser(msg, date)
    if (d.isValid) this.setDates(d)
  }

  parseTags(message) {
    // Set makes things unique
    this.tags = new Set(message.match(hashPattern))
  }

  setDates(opts) {
    this.hasDates = true
    this.start = opts.start
    this.startArr = moment(this.start).toArray()
    this.end = opts.end
    this.endArr = moment(this.end).toArray()
    this.time = opts.text
    this.duration = new Duration(this.start, this.end)
  }

  getDates() {
    let start = this.start
    let end = this.end
    return { start, end }
  }

  toJSON() {
    return {
      _id: this._id,
      version: this.version,
      ref: this.ref,
      user: this.user,
      message: this.message,
      hasDates: this.hasDates,
      start: this.start,
      startArr: this.startArr,
      end: this.end,
      endArr: this.endArr,
      time: this.time,
      tags: [...this.tags],
      duration: this.duration ? {
        seconds: this.duration.seconds,
      } : null
    } 
  }

  static fromJSON(doc) {
    let e = new Entry(doc)

    return e
  }
}

Add the current user to the entry