const m = require('mithril');

const StreamModel = require('../models/stream');
const DataDisplay = require('../components/datadisplay');

const Streams = module.exports = {
  controller: function () {
    let ctrl = this;

    ctrl.names = m.prop({
      title: 'Title',
      desc: 'Description',
      stats: 'Statistics',
      date: 'Start Date',
      user: 'User',
      actions: 'Actions'
    });

    let formatStats = (viewers, stickers) => [
      m('span', viewers + 'V'),
      m('br'),
      m('span', stickers + 'S')
    ];

    let getActions = (id) =>
        m('select', {onchange: m.withAttr('value', m.route)}, [
          m('option', {disabled: true, selected: true}, 'Choose...'),
          m('option', {value: '/streams/view/' + id}, 'View / Edit'),
          m('option', {value: '/streams/stop/' + id}, 'Stop')
        ]);

    let parse = (streams) => streams.map(
        function (stream) {
          return {
            title: stream.title(),
            desc: stream.description(),
            stats: formatStats(stream.viewers(), stream.stickers()),
            date: stream.startDateTime(),
            user: stream.user().alias(),
            actions: getActions(stream.id())
          };
        }
    );

    ctrl.data = StreamModel.list().then(parse);
  },
  view: function (ctrl) {
    return [m('h1', 'Streams'),
      m(DataDisplay, {
        names: ctrl.names(),
        data: ctrl.data
      })
    ];
  }
};

@@ -170,89 +170,116 @@
  = %7B
-%0A  controller: function () %7B%0A    let ctrl = this;%0A%0A    ctrl.names = m.prop(%7B%0A    
+%7D;%0A%0AStreams.init = function () %7B%0A  Streams.streamData = StreamModel.list().then(parse);%0A%7D;%0A%0Aconst names = %7B%0A
   ti
@@ -292,20 +292,16 @@
 Title',%0A
-    
   desc: 
@@ -315,20 +315,16 @@
 ption',%0A
-    
   stats:
@@ -338,20 +338,16 @@
 stics',%0A
-    
   date: 
@@ -360,20 +360,16 @@
  Date',%0A
-    
   user: 
@@ -376,20 +376,16 @@
 'User',%0A
-    
   action
@@ -401,23 +401,16 @@
 ns'%0A
-    %7D);%0A%0A    le
+%7D;%0A%0Acons
 t fo
@@ -446,20 +446,16 @@
 s) =%3E %5B%0A
-    
   m('spa
@@ -470,28 +470,24 @@
 ers + 'V'),%0A
-    
   m('br'),%0A 
@@ -481,28 +481,24 @@
 %0A  m('br'),%0A
-    
   m('span', 
@@ -517,22 +517,16 @@
 S')%0A
-    %5D;%0A%0A    le
+%5D;%0A%0Acons
 t ge
@@ -548,20 +548,16 @@
  =%3E%0A    
-    
 m('selec
@@ -604,20 +604,16 @@
 te)%7D, %5B%0A
-    
       m(
@@ -668,36 +668,32 @@
 ose...'),%0A      
-    
 m('option', %7Bval
@@ -742,20 +742,16 @@
 ,%0A      
-    
 m('optio
@@ -794,20 +794,16 @@
 'Stop')%0A
-    
     %5D);%0A
@@ -807,14 +807,12 @@
 );%0A%0A
-    le
+cons
 t pa
@@ -847,20 +847,16 @@
 ap(%0A    
-    
 function
@@ -873,20 +873,16 @@
 %7B%0A      
-    
 return %7B
@@ -890,20 +890,16 @@
         
-    
 title: s
@@ -909,28 +909,24 @@
 am.title(),%0A
-    
         desc
@@ -957,20 +957,16 @@
         
-    
 stats: f
@@ -1022,20 +1022,16 @@
         
-    
 date: st
@@ -1048,28 +1048,24 @@
 DateTime(),%0A
-    
         user
@@ -1097,20 +1097,16 @@
         
-    
 actions:
@@ -1140,93 +1140,41 @@
     
-    
 %7D;%0A    
-    %7D%0A    );%0A%0A    ctrl.data = StreamModel.list().then(parse);%0A  %7D,%0A  view:
+%7D%0A);%0A%0AStreams.controller =
  fun
@@ -1184,28 +1184,59 @@
 on (
-ctrl
 ) %7B%0A  
-  return %5B
+Streams.init();%0A%7D;%0A%0AStreams.view = () =%3E %5B%0A  
 m('h
@@ -1247,28 +1247,24 @@
 'Streams'),%0A
-    
   m(DataDisp
@@ -1270,20 +1270,16 @@
 play, %7B%0A
-    
     name
@@ -1285,26 +1285,15 @@
 es: 
-ctrl.names(),%0A    
+names,%0A
     
@@ -1302,37 +1302,31 @@
 ta: 
-ctrl.data%0A      %7D)%0A    %5D;%0A  %7D%0A%7D
+Streams.streamData%0A  %7D)%0A%5D
 ;%0A

Reorganize variables