var templates = require('./templates.js')

function applyOptions (requestXML, options) {
  var optionDefaults = {
    timeOffset: [ 0, 'INT' ],
    timeWindow: [ 120, 'INT' ],
    rows: [ 15, 'INT' ],
    filter: [ '', 'TEXT' ],
    destination: [ '', 'TEXT' ]
  }
  if (options) {
    for (var key in optionDefaults) {
      var value = optionDefaults[key][0]
      if (options[key]) {
        if (optionDefaults[key][1] === 'INT') value = parseInt(options[key])
        else value = options[key]
      }
      requestXML = requestXML.replace('$$' + key.toUpperCase() + '$$', value)
    }
  }
  return requestXML
}

var getDepartureBoardRequest = function (station, options) {
  var requestXML = applyOptions(templates.departureBoard, options)
  requestXML = requestXML.replace('$$FROM$$', station)
  return requestXML
}

var getDepartureBoardWithDetails = function (station, options) {
  var requestXML = applyOptions(templates.departureBoardWithDetails, options)
  requestXML = requestXML.replace('$$FROM$$', station)
  return requestXML
}

var getArrivalsBoard = function (station, options) {
  var requestXML = applyOptions(templates.arrivalsBoard, options)
  requestXML = requestXML.replace('$$FROM$$', station)
  return requestXML
}

var getArrivalsBoardWithDetails = function (station, options) {
  var requestXML = applyOptions(templates.arrivalsBoardWithDetails, options)
  requestXML = requestXML.replace('$$FROM$$', station)
  return requestXML
}
var getArrivalsDepartureBoard = function (station, options) {
  var requestXML = applyOptions(templates.arrivalsDepartureBoard, options)
  requestXML = requestXML.replace('$$FROM$$', station)
  return requestXML
}
var getArrivalsDepartureBoardWithDetails = function (station, options) {
  var requestXML = applyOptions(templates.arrivalsDepartureBoardWithDetails, options)
  requestXML = requestXML.replace('$$FROM$$', station)
  return requestXML
}
var getServiceDetails = function (serviceId) {
  var requestXML = templates.serviceDetails.replace('$$SERVICEID$$', serviceId)
  return requestXML
}
var getNextDeparture = function (station, destination, options) {
  options.destination = destination
  var requestXML = applyOptions(templates.nextDeparture, options)
  requestXML = requestXML.replace('$$FROM$$', station)
  return requestXML
}
var getNextDepartureWithDetails = function (station, destination, options) {
  options.destination = destination
  var requestXML = applyOptions(templates.nextDepartureWithDetails, options)
  requestXML = requestXML.replace('$$FROM$$', station)
  return requestXML
}
var getArrival = function (station, destination, options) {
  options.destination = destination
  var requestXML = applyOptions(templates.nextArrival, options)
  requestXML = requestXML.replace('$$FROM$$', station)
  return requestXML
}
var getFastestDeparture = function (station, destination, options) {
  options.destination = destination
  var requestXML = applyOptions(templates.fastestDeparture, options)
  requestXML = requestXML.replace('$$FROM$$', station)
  return requestXML
}
var getFastestDepartureWithDetails = function (station, destination, options) {
  options.destination = destination
  var requestXML = applyOptions(templates.fastestDepartureWithDetails, options)
  requestXML = requestXML.replace('$$FROM$$', station)
  return requestXML
}
module.exports.getDepartureBoardRequest = getDepartureBoardRequest
module.exports.getDepartureBoardWithDetails = getDepartureBoardWithDetails
module.exports.getArrivalsBoard = getArrivalsBoard
module.exports.getArrivalsBoardWithDetails = getArrivalsBoardWithDetails
module.exports.getArrivalsDepartureBoard = getArrivalsDepartureBoard
module.exports.getArrivalsDepartureBoardWithDetails = getArrivalsDepartureBoardWithDetails
module.exports.getServiceDetails = getServiceDetails
module.exports.getNextDeparture = getNextDeparture
module.exports.getNextDepartureWithDetails = getNextDepartureWithDetails
module.exports.getArrival = getArrival
module.exports.getFastestDeparture = getFastestDeparture
module.exports.getFastestDepartureWithDetails = getFastestDepartureWithDetails

@@ -262,27 +262,8 @@
   %7D%0A
-  if (options) %7B%0A  
   fo
@@ -290,26 +290,24 @@
 Defaults) %7B%0A
-  
     var valu
@@ -333,26 +333,24 @@
 key%5D%5B0%5D%0A    
-  
 if (options%5B
@@ -344,24 +344,35 @@
  if (options
+ && options
 %5Bkey%5D) %7B%0A   
@@ -368,18 +368,16 @@
 key%5D) %7B%0A
-  
       if
@@ -449,18 +449,16 @@
 )%0A      
-  
 else val
@@ -479,22 +479,18 @@
 ey%5D%0A    
-  %7D%0A  
+%7D%0A
     requ
@@ -557,22 +557,16 @@
  value)%0A
-    %7D%0A
   %7D%0A  re

fix applyOptions defaults if no options are passed