(function () {
    'use strict';

    var request = require('request-promise');
    var cheerio = require('cheerio');
    var _ = require('lodash');

    module.exports = function (context, req) {
        var options = {
            method: 'POST',
            uri: 'http://apps.atl.com/Passenger/Parking/Default.aspx',
            transform: function (body) {
                return cheerio.load(body);
            }
        };
        var terminal;
        if(req.query && req.query.terminal ){
          terminal = req.query.terminal
        }
        var terminalPriority = {
          'North Daily': 3,
          'North Economy': 2,
          'North Hourly': 5,
          'Park-Ride-A': 4,
          'Park-Ride-C': 4,
          'South Daily': 3,
          'South Economy': 2,
          'South Hourly': 5,
          'West Economy': 1,
          'International Hourly': 2,
          'International Park-Ride': 1,
        }

        context.log('Before promise')
        request(options).then(($) => {
            var parking = [];
            context.log('Promise returned');
            $('div#bodySection_TabContainer1_TabPanel1_wucParkingLotStatus_UplParking')
            .children()
            .toArray()
            .forEach((value, index) => {
                if (index === 0) {
                    return;
                }

                var location = $(value).children('.col-xs-9').text(),
                    status = $(value).children('.col-xs-3').text();

                // This will only put parking lots that are relevant to the terminal passed in. A bit convoluted, could probably be cleaned up
                // If no terminal is passed in, then include all of them. Ignore full lots
                if (location !== "" && status !== "" && status !== 'Full') {
                    if (terminal) {
                      if (terminal === 'international' && _.startsWith(location, 'International')) {
                        parking.push({ location: location, status: status, priority:  terminalPriority[location]});
                      } else if (terminal === 'domestic' && !_.startsWith(location, 'International')) {
                        parking.push({ location: location, status: status, priority:  terminalPriority[location]});
                      }
                    } else {
                      parking.push({ location: location, status: status, priority:  terminalPriority[location]});
                    }
                }
            });

            // Sort lots by priority
            parking = _.sortBy(parking, (lot) => lot.priority );

            context.log(`Return ${JSON.stringify(parking[0], null, 2)}`)
            // Only return the first element
            context.res = { body: parking[0] };
            context.done();
        }).catch((error) => {
            context.log('error: ', error);
            context.done();
        });
    };

}());
@@ -226,16 +226,20 @@
         
+    
 method: 
@@ -246,16 +246,20 @@
 'POST',%0A
+    
         
@@ -333,16 +333,20 @@
         
+ 
+   
 transfor
@@ -362,24 +362,28 @@
 on (body) %7B%0A
+    
             
@@ -413,38 +413,32 @@
 (body);%0A        
-    %7D%0A
         %7D;%0A     
@@ -430,17 +430,16 @@
        %7D
-;
 %0A       
@@ -443,107 +443,35 @@
     
-var terminal;%0A        if(req.query && req.query.terminal )%7B%0A          terminal = req.query.
+    %7D,%0A            
 terminal
 %0A   
@@ -470,27 +470,22 @@
 inal
+,
 %0A
-  
     
-  %7D%0A
         
 var 
@@ -480,20 +480,16 @@
         
-var 
 terminal
@@ -507,24 +507,30 @@
 %7B%0A          
+     
+ 
 'North Daily
@@ -527,32 +527,38 @@
 orth Daily': 3,%0A
+      
           'North
@@ -577,24 +577,30 @@
 ,%0A          
+  
+    
 'North Hourl
@@ -608,32 +608,38 @@
 ': 5,%0A          
+     
+ 
 'Park-Ride-A': 4
@@ -636,24 +636,30 @@
 Ride-A': 4,%0A
+      
           'P
@@ -680,24 +680,30 @@
 ,%0A          
+ 
+     
 'South Daily
@@ -714,24 +714,30 @@
 ,%0A          
+    
+  
 'South Econo
@@ -750,24 +750,30 @@
 ,%0A          
+      
 'South Hourl
@@ -789,16 +789,22 @@
         
+ 
+     
 'West Ec
@@ -816,32 +816,38 @@
 ': 1,%0A          
+    
+  
 'International H
@@ -853,24 +853,30 @@
 Hourly': 2,%0A
+      
           'I
@@ -903,33 +903,38 @@
 de': 1,%0A        
+ 
-%7D
+   %7D;
 %0A%0A        contex
@@ -930,38 +930,101 @@
     
+ 
- context.log('Before promise')
+if (req.query && req.query.terminal) %7B%0A            terminal = req.query.terminal;%0A        %7D%0A
 %0A   
@@ -1093,52 +1093,8 @@
 %5B%5D;%0A
-            context.log('Promise returned');
 %0A   
@@ -1848,32 +1848,34 @@
                 
+  
 if (terminal ===
@@ -1929,32 +1929,36 @@
 ernational')) %7B%0A
+    
                 
@@ -2075,24 +2075,26 @@
             
+  
 %7D else if (t
@@ -2096,35 +2096,8 @@
 if (
-terminal === 'domestic' && 
 !_.s
@@ -2128,32 +2128,36 @@
 ernational')) %7B%0A
+    
                 
@@ -2282,152 +2282,11 @@
     
-%7D%0A                    %7D else %7B%0A                      parking.push(%7B location: location, status: status, priority:  terminalPriority%5Blocation%5D%7D);
+  %7D
 %0A   
@@ -2514,16 +2514,17 @@
 l, 2)%7D%60)
+;
 %0A       

Update parking to assume domestic if international not specified