lunchControllers = angular.module 'lunchControllers'

lunchControllers.controller 'RunCtrl', ['$scope', 'socket', '$routeParams', ($scope, socket, $routeParams) ->
    $scope.show = false
    $scope.progress = 0
    $scope.numOrders = 0
    $scope.progressClass = 'progress-bar-danger'

    socket.get '/run/' + $routeParams['id'],
        (response) ->
            $scope.show = true
            $scope.run = response
            $scope.numOrders = $scope.run.invitees.length
            $scope.setStatusClasses()
            $scope.setProgress()
            $scope.$apply()

    socket.on 'message', (m) ->
        if m.verb is 'update' and m.id is $routeParams.id
            $scope.run = m.data
            $scope.setProgress()
            $scope.setStatusClasses()
            $scope.$apply()

    $scope.setStatusClasses = () ->
        for i in $scope.run.invitees
            if i.order
                i.statusClass = 'done'
            else
                i.statusClass = 'incomplete'

    $scope.setProgress = () ->
        numFilledOrders = _.filter($scope.run.invitees, (i) -> i.order).length
        $scope.progress = (numFilledOrders / $scope.numOrders) * 100
        if $scope.progress >= 100
            $scope.progressClass = 'progress-bar-success'
        else
            $scope.progressClass = 'progress-bar-danger'
]
