'use strict'

//
// robot.coffee に対する拡張部分
//
const GOOGLE_SHORTENER_API_KEY = 'AIzaSyAUisTOqBoSigbgtdZDIH-2PYHpzSRYmoQ'

const _map = (msg, callback) => {
  const text = msg.match[1].replace(/[\n\r]/g, ' ')
  const m = text.match(/^(今ココ|I'm here)[:：] (.*) (https?:\/\/.*)$/)
  if (!m) return

  const place = m[2].replace(/ ?\((近辺|Near)\)$/, '').replace(/^(緯度|LAT) [:：].*$/, '')
  const url = m[3]

  const cb = (err, url) => {
    if (err) console.error(err)
    if (typeof url !== 'string') url = ''

    const loc = url.match(/[@=]([0-9.]+),([0-9.]+)/) || ['', '', '']
    msg.json = { place, lat: loc[1], lng: loc[2] }
    return callback(msg)
  }

  if (url.indexOf('goo.gl') === -1) {
    return cb(null, url)
  }

  const get = msg.http(`https://www.googleapis.com/urlshortener/v1/url?shortUrl=${url}&key=${GOOGLE_SHORTENER_API_KEY}`).get()
  get((err, res, body) => {
    if (err) return cb(err)
    try {
      const json = JSON.parse(body)
      return cb(json.longUrl, json)
    } catch (e) {
      return cb(e)
    }
  })
}

// public:
const jsonMatcher = (prop, options, callback) => {
  if (!callback) {
    callback = options
    options = {}
  }

  if (prop === 'map') {
    const regex = /((.|[\n\r])*)/
    const cb = (msg) => _map(msg, callback)
    return [regex, options, cb]
  }

  const checker = (obj) => {
    if (!obj) return false
    switch (prop) {
      case 'stamp':
        return !!obj.stamp_set && !!obj.stamp_index
      case 'yesno':
        return !!obj.question && !obj.options
      case 'select':
        return !!obj.question && !!obj.options
      case 'task':
        return !!obj.title
      case 'file':
        return !!obj.file_id
      default:
        return !!obj[prop]
    }
  }

  const regex = /({.*})/
  const cb = (msg) => {
    try {
      const json = JSON.parse(msg.match[1])
      if (checker(json)) {
        msg.json = json
        callback(msg)
      }
    } catch (e) {
      return false
    }
  }
  return [regex, options, cb]
}

module.exports = {
  jsonMatcher
}

@@ -41,83 +41,8 @@
 %E5%88%86%0A//
-%0Aconst GOOGLE_SHORTENER_API_KEY = 'AIzaSyAUisTOqBoSigbgtdZDIH-2PYHpzSRYmoQ'
 %0A%0Aco
@@ -577,385 +577,27 @@
 %0A%0A  
-if (url.indexOf('goo.gl') === -1) %7B%0A    return cb(null, url)%0A  %7D%0A%0A  const get = msg.http(%60https://www.googleapis.com/urlshortener/v1/url?shortUrl=$%7Burl%7D&key=$%7BGOOGLE_SHORTENER_API_KEY%7D%60).get()%0A  get((err, res, body) =%3E %7B%0A    if (err) return cb(err)%0A    try %7B%0A      const json = JSON.parse(body)%0A      return cb(json.longUrl, json)%0A    %7D catch (e) %7B%0A      return cb(e)%0A    %7D%0A  %7D
+return cb(null, url
 )%0A%7D%0A

Remove Google short link support.