import View from 'girder/views/View';
import { renderMarkdown } from 'girder/misc';
import { restRequest } from 'girder/rest';
import router from 'girder/router';

import template from '../templates/raftRun.pug';
import '../stylesheets/raftRun.styl';

const RaftRunView = View.extend({
    events: {
        'click .g-run-raft': 'execute',
        'click .g-edit-raft': function (event) {
            router.navigate('raft/' + this.model.get('_id') + '/edit?frontendId=' + this._raftSpec.frontend, {
                params: this._raftSpec,
                trigger: true
            });
        }
    },

    initialize: function (settings) {
        this._raftSpec = this.model.get('meta').raftSpec;
        const promises = [
            restRequest({
                url: 'resource/' + this._raftSpec.data + '/path',
                type: 'GET',
                data: {
                    type: 'folder'
                }
            }).then((resp) => resp),
            restRequest({
                url: 'frontend/' + this._raftSpec.frontend,
                type: 'GET'
            }).then((resp) => resp)
        ];
        this._raftSpec.scripts.forEach(function (element) {
            promises.push(
                restRequest({
                    url: 'resource/' + element + '/path',
                    type: 'GET',
                    data: {
                        type: 'item'
                    }
                }).then((resp) => resp)
            );
        });

        var view = this;

        // Fetch the plugin list
        $.when(...promises).done(function () {
            view.scripts = [];
            if (arguments.length > 2) {
                for (var i = 2; i < arguments.length; i++) {
                    view.scripts.push(arguments[i]);
                }
            }
            view.data = arguments[0];
            view.frontend = arguments[1]['description'];
            view.render();
        }).fail(() => {
            router.navigate('/', { trigger: true });
        });
    },

    render: function () {
        this.$el.html(template({
            item: this.model,
            data: this.data,
            frontend: this.frontend,
            scripts: this.scripts || [],
            renderMarkdown: renderMarkdown
        }));
        return this;
    },

    execute: function (e) {
        restRequest({
            url: 'notebook',
            data: {
                folderId: this._raftSpec.data,
                frontendId: this._raftSpec.frontend,
                scripts: JSON.stringify(this._raftSpec.scripts)
            },
            type: 'POST'
        }).done(function (notebook) {
            window.location.assign(notebook['url']);
        });
    }
});

export default RaftRunView;

@@ -2326,24 +2326,160 @@
 ction (e) %7B%0A
+        // TODO Validate raft%0A%0A        this.$('.g-validation-failed-message').empty();%0A        $(e.currentTarget).girderEnable(false);%0A%0A
         rest
@@ -2480,32 +2480,32 @@
   restRequest(%7B%0A
-
             url:
@@ -2517,16 +2517,44 @@
 ebook',%0A
+            method: 'POST',%0A
         
@@ -2760,20 +2760,19 @@
     
-type: 'POST'
+error: null
 %0A   
@@ -2788,27 +2788,17 @@
 one(
-function (notebook)
+(resp) =%3E
  %7B%0A 
@@ -2835,23 +2835,194 @@
 ign(
-notebook%5B'url'%5D
+resp%5B'url'%5D);%0A        %7D).fail((resp) =%3E %7B%0A            $(e.currentTarget).girderEnable(true);%0A            this.$('.g-validation-failed-message').text('Error: ' + resp.responseJSON.message
 );%0A 

Disable 'run raft' button upon raft creation