import socket from '../../socket';
import * as utils from '../../utils';
import * as xhr from './tournamentXhr';
import socketHandler from './socketHandler';
import helper from '../helper';
import m from 'mithril';

const tournament = m.prop([]);

export default function controller() {
  let id = m.route.param('id');
  helper.analyticsTrackView('Tournament ' + id);

  let clockInterval = null;
  xhr.tournament(id).then(data => {
    tournament(data);
    clockInterval = setInterval(tick, 1000);
    return data;
  }, err => utils.handleXhrError(err));

  let returnVal = {
    tournament,
    reload,
    onunload: () => {
      socket.destroy();
      if(clockInterval)
        clearInterval(clockInterval);
    }
  };

  socket.createTournament(tournament().socketVersion, id, socketHandler(returnVal));
  return returnVal;
}

function reload (data) {
  tournament(data);
  if(data.socketVersion)
    socket.setVersion(data.socketVersion);
  m.redraw();
}

function tick () {
  let data = tournament();
  if(data.secondsToStart && data.secondsToStart > 0)
    data.secondsToStart--;

  if(data.secondsToFinish && data.secondsToFinish > 0)
    data.secondsToFinish--;

  m.redraw();
}

@@ -213,38 +213,8 @@
 ';%0A%0A
-const tournament = m.prop(%5B%5D);
 %0A%0Aex
@@ -245,24 +245,443 @@
 ntroller() %7B
+%0A  const tournament = m.prop(%5B%5D);%0A  function reload (data) %7B%0A    tournament(data);%0A    if(data.socketVersion)%0A      socket.setVersion(data.socketVersion);%0A    m.redraw();%0A  %7D%0A%0A  function tick () %7B%0A    let data = tournament();%0A    if(data.secondsToStart && data.secondsToStart %3E 0)%0A      data.secondsToStart--;%0A%0A    if(data.secondsToFinish && data.secondsToFinish %3E 0)%0A      data.secondsToFinish--;%0A%0A    m.redraw();%0A  %7D%0A
 %0A  let id = 
@@ -1219,362 +1219,4 @@
 ;%0A%7D%0A
-%0Afunction reload (data) %7B%0A  tournament(data);%0A  if(data.socketVersion)%0A    socket.setVersion(data.socketVersion);%0A  m.redraw();%0A%7D%0A%0Afunction tick () %7B%0A  let data = tournament();%0A  if(data.secondsToStart && data.secondsToStart %3E 0)%0A    data.secondsToStart--;%0A%0A  if(data.secondsToFinish && data.secondsToFinish %3E 0)%0A    data.secondsToFinish--;%0A%0A  m.redraw();%0A%7D%0A

Make tournament a private variable of controller