 ## Task status button directive
 #############################################################################
 
-TaskStatusButtonDirective = ($rootScope, $repo) ->
+TaskStatusButtonDirective = ($rootScope, $repo, $confirm) ->
     # Display the status of Task and you can edit it.
     #
     # Example:

        require: "ngModel"
    }

module.directive("tgTaskStatusButton", ["$rootScope", "$tgRepo", TaskStatusButtonDirective])


TaskIsIocaineButtonDirective = ($rootscope, $tgrepo, $confirm) ->

    """) #TODO: i18n

    link = ($scope, $el, $attrs, $model) ->
        isEditable = ->
            return $scope.project.my_permissions.indexOf("modify_task") != -1

        render = (task) =>
            status = $scope.statusById[task.status]

            html = template({
                status: status
                statuses: $scope.statusList
                editable: isEditable()
            })
            $el.html(html)

        $el.on "click", ".status-data", (event) ->
            event.preventDefault()
            event.stopPropagation()
            return if not isEditable()

            $el.find(".pop-status").popover().open()

        $el.on "click", ".status", (event) ->
            event.preventDefault()
            event.stopPropagation()
            return if not isEditable()

            target = angular.element(event.currentTarget)

            $.fn.popover().closeAll()

            task = $model.$modelValue.clone()
            task.status = target.data("status-id")
            $model.$setViewValue(task)

            onSuccess = ->
                $confirm.notify("success")
                $rootScope.$broadcast("history:reload")
            onError = ->
                $confirm.notify("error")
                task.revert()
                $model.$setViewValue(task)
            $repo.save($model.$modelValue).then(onSuccess, onError)

        $scope.$watch $attrs.ngModel, (task) ->
            render(task) if task
