import {findMatches as commuterConnectionsMatches} from 'commuter-connections'
import {Router} from 'express'

import CommuterLocations from '../commuter-locations/model'
import * as otp from '../otp'
import Ridepool from '../ridepool/model'

const app = Router()

export default app

app.get('/', function (req, res) {
  const [startLat, startLng] = parseFloatArray(req.query.from)
  const [endLat, endLng] = parseFloatArray(req.query.to)
  const qs = req.url.split('?')[1]

  const plans = [
    otp.profile(qs),
    otp.routes(),
    commuterConnectionsMatches({
      startLat: startLat,
      startLng: startLng,
      endLat: endLat,
      endLng: endLng,
      startRadius: req.query.fromRadius || 1,
      endRadius: req.query.toRadius || 0.5
    }),
    Ridepool.findMatches([ startLng, startLat ], [ endLng, endLat ])
  ]

  if (req.user) {
    plans.push(CommuterLocations.findLocationsForCommuter(req.user.id))
  }

  let data = {}
  Promise.all(plans)
  .then(([profile, routes, externalMatches, ridepoolMatches, internalMatches]) => {
    data = {
      externalMatches,
      internalMatches,
      profile: profile.options,
      ridepoolMatches,
      routes
    }

    return otp.patternsFromProfile(data.profile)
  })
  .then(patterns => {
    data.patterns = patterns

    // Populate the transit segments in the profile
    otp.populateTransitSegments(data.profile, data.patterns, data.routes)

    // Populate the stop times
    return otp.populateStopTimes(data.profile, req.query.date, parseInt(req.query.startTime, 10), parseInt(req.query.endTime, 10))
  })
  .then(() => {
    res.status(200).send(data)
  })
  .catch((err) => {
    res.status(400).send(err)
  })
})

function parseFloatArray (str) {
  if (str && str.length > 0) {
    return str.split(',').map(parseFloat)
  }
  return []
}

@@ -530,16 +530,18 @@
 (),%0A    
+/*
 commuter
@@ -753,16 +753,18 @@
 %0A    %7D),
+*/
 %0A    Rid
@@ -960,24 +960,26 @@
 ll(plans)%0A  
+//
 .then((%5Bprof
@@ -1044,24 +1044,91 @@
 ches%5D) =%3E %7B%0A
+  .then((%5Bprofile, routes, ridepoolMatches, internalMatches%5D) =%3E %7B%0A
     data = %7B
@@ -1141,32 +1141,35 @@
  externalMatches
+: 0
 ,%0A      internal

Disable Commuter Connections query