module.exports = {
  createDrag (obj) {
    const drag = (ev) => {
      obj && obj.drag && obj.drag(ev)
    }
    const drop = (ev) => {
      document.removeEventListener('mousemove', drag)
      document.removeEventListener('mouseup', drop)
      obj && obj.drop && obj.drop(ev)
    }
    document.addEventListener('mousemove', drag)
    document.addEventListener('mouseup', drop)
  },
  guid (n) {
    var text = ''
    if (!n) n = 5
    var possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
    const d = new Date()

    text += possible[d.getMilliseconds() % possible.length]
    text += possible[d.getSeconds()]
    text += possible[d.getMinutes()]
    text += possible[d.getDate()]
    for (var i = 0; i < n; i++) { text += possible.charAt(Math.floor(Math.random() * possible.length)) }

    return text
  },
  textWrap (text, maxLen, opt) {
    const ret = []
    let parts = text.split(' ', -1)
    switch (opt) {
      case 'white-space':
        parts.forEach(n => { maxLen = Math.max(n.length + 1, maxLen) })
        break
      case 'any':
        // Break any word bigger than wrapThreshold
        let newParts = []
        parts.forEach(n => {
          while (n.length > maxLen) {
            const pre = n.substring(0, maxLen)
            newParts.push(pre)
            n = n.substr(maxLen)
          }
          newParts.push(n)
        })
        parts = newParts
        break
    }
    ret.push(parts[0])
    for (let i = 1; i < parts.length; i++) {
      let ri = ret.length - 1 // last
      if (ret[ret.length - 1].length + parts[i].length > maxLen) {
        ret.push(parts[i])
      } else {
        // we can add to same
        ret[ri] += ' ' + parts[i]
      }
    }
    return ret
  }
}
