var express = require('express')
var router  = express.Router()
var path    = require('path')
var debug   = require('debug')('app:' + path.basename(__filename).replace('.js', ''))
var async   = require('async')
var op      = require('object-path')

var mapper  = require('../helpers/mapper')

var festivals = {}

router.get('/:festival_id', function(req, res) {
    debug('Loading "' + path.basename(__filename).replace('.js', '') + '"')
    var festival = op.get(festivals, req.params.festival_id)
    res.render('festival', {
        'festival': festival
    })
    res.end()
})

router.prepare = function prepareFestivals(callback) {
    // var festivals = {}
    async.each(SDC.get(['local_entities', 'by_class', 'festival']), function(festivalEntity, callback) {
        op.set(festivals, festivalEntity.id, mapper.event(festivalEntity.id))
        // debug(JSON.stringify(event, null, 2))
        async.each(SDC.get(['relationships', festivalEntity.id, 'event']), function(eid, callback) {
            var event = mapper.event(eid)
            // debug(JSON.stringify(event, null, 2))
            if(event['start-time']) {
                var eventDate = (event['start-time']).slice(0,10)
                var eventTime = (event['start-time']).slice(11,16)
                op.push(festivals, [festivalEntity.id, 'events', eventDate, eventTime], event)
            }
            callback()
        }, function(err) {
            if (err) {
                debug('Failed to prepare festival ' + festivalEntity.id, err)
                callback(err)
                return
            }
            // debug('Festival ' + festivalEntity.id + ' prepared.')
            callback()
        })
    }, function(err) {
        if (err) {
            debug('Failed to prepare festivals.', err)
            callback(err)
            return
        }
        // debug('Festivals prepared.' + JSON.stringify(festivals, null, 4))
        callback()
    })
}


module.exports = router

@@ -357,16 +357,206 @@
 es) %7B%0A  
+return res.redirect('./' + req.params.festival_id + '/program')%0A%7D)%0A%0Arouter.get('/:festival_id/:action', function(req, res) %7B%0A  let action = req.params.action ? req.params.action : 'program'%0A
   debug(
@@ -618,21 +618,29 @@
 '') + '%22
-')%0A  
+ ' + action)%0A
   var fe
@@ -690,18 +690,16 @@
 val_id)%0A
-  
   res.re
@@ -716,17 +716,23 @@
 ival
-', %7B%0A    
+_' + action, %7B%0A
     
@@ -755,18 +755,36 @@
 ival
+,
 %0A  
+  'action': action%0A
   %7D)%0A
-  
   re
@@ -825,24 +825,8 @@
 ion 
-prepareFestivals
 (cal
@@ -834,18 +834,16 @@
 back) %7B%0A
-  
   // var
@@ -858,18 +858,16 @@
 ls = %7B%7D%0A
-  
   async.
@@ -961,24 +961,63 @@
 back) %7B%0A    
+debug('festival ' + festivalEntity.id)%0A
     op.set(f
@@ -1086,57 +1086,135 @@
     
-    // debug(JSON.stringify(event, null, 2
+op.set(festivals, %5BfestivalEntity.id, 'coverage'%5D, mapper.coverageByEventSync(festivalEntity.id
 ))%0A
+%0A
     
+// prep festival events%0A
     
@@ -1308,30 +1308,24 @@
 ck) %7B%0A      
-  
-    
 var event = 
@@ -1342,22 +1342,16 @@
 nt(eid)%0A
-      
       //
@@ -1389,22 +1389,16 @@
 ll, 2))%0A
-      
       if
@@ -1425,32 +1425,24 @@
 ) %7B%0A        
-    
-    
 var eventDat
@@ -1487,24 +1487,16 @@
         
-      
-  
 var even
@@ -1540,28 +1540,26 @@
 1,16)%0A      
-    
+%7D%0A
       op.pus
@@ -1631,36 +1631,16 @@
  event)%0A
-            %7D%0A      
       ca
@@ -1652,20 +1652,16 @@
 k()%0A    
- 
-   
 %7D, funct
@@ -1667,30 +1667,24 @@
 tion(err) %7B%0A
-      
       if (er
@@ -1684,32 +1684,24 @@
  if (err) %7B%0A
-        
         debu
@@ -1762,32 +1762,24 @@
 rr)%0A        
-   
-     
 callback(err
@@ -1788,37 +1788,23 @@
         
-     
-   
 return%0A
-      
       %7D%0A
@@ -1809,25 +1809,16 @@
 %7D%0A      
- 
-     // 
 debug('F
@@ -1863,22 +1863,16 @@
 ared.')%0A
-      
       ca
@@ -1884,21 +1884,16 @@
 k()%0A
-    
     %7D)%0A
+%0A
-  
   %7D,
@@ -1905,28 +1905,24 @@
 tion(err) %7B%0A
-    
     if (err)
@@ -1930,22 +1930,16 @@
 %7B%0A      
-    
-  
 debug('F
@@ -1969,30 +1969,24 @@
 als.', err)%0A
-      
       callba
@@ -1999,22 +1999,16 @@
 )%0A      
-  
-    
 return%0A 
@@ -2014,24 +2014,13 @@
     
- 
+%7D%0A
    
-%7D%0A        //
  deb
@@ -2047,51 +2047,10 @@
 ed.'
- + JSON.stringify(festivals, null, 4))%0A    
+)%0A
     
@@ -2056,26 +2056,24 @@
  callback()%0A
-  
   %7D)%0A%7D%0A%0A%0Amod

prepare festival coverage