lunchControllers = angular.module 'lunchControllers'

lunchControllers.controller 'CreateOrderCtrl', ['$scope','socket','$routeParams','$location',
    CreateOrderController = ($scope, socket, $routeParams, $location) ->
        $scope.show = false
        $scope.showMenuUrl = false

        socket.get '/run/' + $routeParams['id'],
            (response) ->
                $scope.show = true
                $scope.showMenuUrl = response.menuUrl

                $scope.run = response
                $scope.invitee = _.find response.invitees, (i) -> i.name.toLowerCase() == $routeParams['inviteeName'].toLowerCase()
                $scope.invitees = response.invitees

                $scope.$apply()

        $scope.submit = (invitee) ->
            invitees = _.map $scope.invitees, (i) ->
                if i.name is invitee.name
                    return invitee
                return i

            socket.put '/run/' + $routeParams['id'], { invitees: invitees }, (response) ->
                $location.path '/run/' + response.id
                $scope.$apply()
]
