Application.Directives.directive('wizardStepDone', wizardStepDoneDirective);

function wizardStepDoneDirective() {
    return {
        scope: {},
        controller: "wizardStepDoneDirectiveController",
        restrict: "EA",
        templateUrl: "application/core/projects/directives/provenance/wizard-step-done.html"
    };
}

Application.Controllers.controller('wizardStepDoneDirectiveController',
                                   ["$scope", "provStep", "$stateParams",
                                    "Restangular", "User", "$timeout", "model.projects",
                                    "projectState", "recent", wizardStepDoneDirectiveController]);
function wizardStepDoneDirectiveController($scope, provStep, $stateParams, Restangular, User,
                                           $timeout, projects, projectState, recent) {
    var state = projectState.get($stateParams.id, $stateParams.sid);
    $scope.unfinishedSteps = [];
    function determineDoneState() {
        if (!state.currentDraft.process.done) {
            $scope.unfinishedSteps.push({
                stepType: "process",
                step: "process",
                name: state.selectedTemplate.template_name
            });
        }
        state.selectedTemplate.input_templates.forEach(function(t) {
            if (!state.currentDraft.inputs[t.id].done) {
                $scope.unfinishedSteps.push({
                    stepType: "inputs",
                    step: t.id,
                    name: "(I) " + t.template_name
                });
            }
        });

        if (state.selectedTemplate.required_input_files) {
            if (!state.currentDraft.inputs.files.done) {
                $scope.unfinishedSteps.push({
                    stepType: "inputs",
                    step: "files",
                    name: "(I) Files"
                });
            }
        }

        state.selectedTemplate.output_templates.forEach(function(t) {
            if (!state.currentDraft.outputs[t.id].done) {
                $scope.unfinishedSteps.push({
                    stepType: "outputs",
                    step: t.id,
                    name: "(O) " + t.template_name
                });
            }
        });

        if (state.selectedTemplate.required_output_files) {
            if (!state.currentDraft.outputs.files.done) {
                $scope.unfinishedSteps.push({
                    stepType: "outputs",
                    step: "files",
                    name: "(O) Files"
                });
            }
        }

        if ($scope.unfinishedSteps.length === 0) {
            state.currentDraft.completed = true;
        }
    }

    determineDoneState();

    $scope.gotoStep = function(stepType, stepName) {
        var step = provStep.makeStep(stepType, stepName);
        provStep.setStep($stateParams.id, step);
    };

    function closeProvenanceAction() {
        provStep.resetProject($stateParams.id);
        recent.gotoLast($stateParams.id, $stateParams.sid);
    }

    $scope.submit = function() {
        Restangular.one("provenance2")
            .post($stateParams.id,
                  state.currentDraft,
                  {apikey: User.apikey()})
            .then(function() {
                recent.delete($stateParams.id, $stateParams.sid);
                projectState.delete($stateParams.id, $stateParams.sid);
                closeProvenanceAction();
            });
    };

    $scope.saveDraft = function() {
        Restangular.one("drafts2")
            .post($stateParams.id,
                  state.currentDraft,
                  {apikey: User.apikey()})
            .then(function(id) {
                projects.getList(true).then(function() {
                    closeProvenanceAction();
                });
            });
    };

    $scope.cancel = function() {
        // The bootstrap modal black backdrop isn't dismissed
        // if we don't wrap these functions in a timeout. It
        // appears that the changes to the dom are happening
        // too quickly.
        $timeout(function() {
            recent.delete($stateParams.id, $stateParams.sid);
            projectState.delete($stateParams.id, $stateParams.sid);
            closeProvenanceAction();
        }, 100);
    };
}

@@ -546,22 +546,15 @@
 %22, %22
-model.projects
+current
 %22,%0A 
@@ -798,24 +798,23 @@
 imeout, 
-projects
+current
 , projec
@@ -2939,24 +2939,146 @@
 Params.id);%0A
+        recent.delete($stateParams.id, $stateParams.sid);%0A        projectState.delete($stateParams.id, $stateParams.sid);%0A
         rece
@@ -3104,34 +3104,16 @@
 arams.id
-, $stateParams.sid
 );%0A    %7D
@@ -3353,169 +3353,625 @@
     
-recent.delete($stateParams.id, $stateParams.sid);%0A                projectState.delete($stateParams.id, $stateParams.sid);%0A                closeProvenanceAction()
+closeProvenanceAction();%0A            %7D);%0A    %7D;%0A%0A    function updateDraft(draft) %7B%0A        var project = current.project();%0A        if (!('id' in state.currentDraft)) %7B%0A            // New draft so add it to the list.%0A            project.drafts.push(draft);%0A        %7D else %7B%0A            // we are working with an existing draft so%0A            // update that item in the list.%0A            var i = _.indexOf(project.drafts, function(d) %7B%0A                return d.id === draft.id;%0A            %7D);%0A            if (i !== -1) %7B%0A                // if i === -1 then we are in trouble.%0A                project.drafts%5Bi%5D = draft
 ;%0A  
@@ -3977,33 +3977,40 @@
            %7D
-);
+%0A        %7D
 %0A    %7D
-;
 %0A%0A    $scope
@@ -4027,32 +4027,78 @@
  = function() %7B%0A
+        var apikey = %7Bapikey: User.apikey()%7D;%0A
         Restangu
@@ -4150,34 +4150,16 @@
 rams.id,
-%0A                 
  state.c
@@ -4174,50 +4174,15 @@
 aft,
-%0A                  %7Bapikey: User.apikey()%7D
+ apikey
 )%0A  
@@ -4206,18 +4206,21 @@
 unction(
-i
 d
+raft
 ) %7B%0A    
@@ -4235,50 +4235,28 @@
     
-projects.getList(true).then(function() %7B%0A 
+updateDraft(draft);%0A
     
@@ -4259,35 +4259,32 @@
                 
-   
 closeProvenanceA
@@ -4292,36 +4292,16 @@
 tion();%0A
-                %7D);%0A
         

Update or add draft to project.