import $ from 'jquery';
import { inject as service } from '@ember/service';
import { computed, observer } from '@ember/object';
import { A } from '@ember/array';
import Controller from '@ember/controller';

export default Controller.extend({
    currentUser: service(),
    revision: null,
    edit: false,
    displays: A([]),

    mfrVersion: computed('model.file', 'revision', function() {
        return this.get('revision') ? this.get('revision') : this.get('model.file.currentVersion');
    }),

    fileUrl: computed('model.file', function() {
        return encodeURIComponent(window.location.href);
    }),

    twitterUrl: computed('model.file', 'fileUrl', function() {
        return `https://twitter.com/intent/tweet?url=${this.get('fileUrl')}&text=${this.get('model.file.name')}&via=OSFramework`;
    }),

    facebookUrl: computed('model.file', 'fileUrl', function() {
        return `https://www.facebook.com/sharer/sharer.php?u=${this.get('fileUrl')}`;
    }),

    linkedInUrl: computed('model.file', 'fileUrl', function() {
        return `https://www.linkedin.com/cws/share?url=${this.get('fileUrl')}&title=${this.get('model.file.name')}`;
    }),

    emailUrl: computed('model.file', 'fileUrl', function() {
        return `mailto:?subject=${this.get('model.file.name')}&body=${this.get('fileUrl')}`;
    }),

    mfrUrl: computed('model.file', function() {
        return `https://mfr.osf.io/render?url=${window.location.href}?action=download%26mode=render`;
    }),

    shareiFrameDynamic: computed('model.file', function() {
        return `<style>.embed-responsive{position:relative;height:100%;}.embed-responsive iframe{position:absolute;height:100%;}</style><script>window.jQuery || document.write('<script src="//code.jquery.com/jquery-1.11.2.min.js">\x3C/script>') </script><link href="https://mfr.osf.io/static/css/mfr.css" media="all" rel="stylesheet"><div id="mfrIframe" class="mfr mfr-file"></div><script src="https://mfr.osf.io/static/js/mfr.js"></script> <script>var mfrRender = new mfr.Render("mfrIframe", "${this.get('mfrUrl')}");</script>`;
    }),

    shareiFrameDirect: computed('model.file', function() {
        return `<iframe src="${this.get('mfrUrl')}" width="100%" scrolling="yes" height="677px" marginheight="0" frameborder="0" allowfullscreen webkitallowfullscreen>`;
    }),

    fileTags: computed('model.file', function() {
        return this.get('model.file.tags');
    }),

    fileVersions: computed('model.file', function() {
        return $.getJSON(`${this.get('model.file.links.download')}?revisions=&`).then(function(data) {
            return data.data;
        });
    }),

    _edit: observer('currentUser', 'model.user', function() {
        if (this.get('model.user.id')) { // don't change the value while id is not loaded.
            this.set('edit', this.get('model.user.id') === this.get('currentUser.currentUserId'));
        }
    }),

    actions: {
        share() {
            document.querySelector('#sharePaneUrl').select();
            document.execCommand('copy');
        },

        download(version) {
            const url = `${this.get('model.file.links.download')}?revision=${version}`;
            window.location = url;
        },

        changeView() {
            $('#mfrIframeParent').toggle();
            $('#revisionsPanel').toggle();
            $('.view-button').toggleClass('btn-default btn-primary');
        },

        openFile(file) {
            const fileID = file.get('guid') ? file.get('guid') : file.id;
            this.transitionToRoute('file-detail', fileID);
        },

        addTag(tag) {
            const model = this.get('model.file');
            this.get('fileTags').pushObject(tag);
            model.set('tags', this.get('fileTags'));
            model.save();
        },

        removeTagAtIndex(index) {
            const model = this.get('model.file');
            this.get('fileTags').removeAt(index);
            model.set('tags', this.get('fileTags'));
            model.save();
        },

        versionChange(version) {
            this.set('revision', version);
        },
    },
});

@@ -90,18 +90,8 @@
 uted
-, observer
  %7D f
@@ -277,25 +277,8 @@
 ll,%0A
-    edit: false,%0A
     
@@ -2540,64 +2540,42 @@
 hen(
-function(data) %7B%0A            return data.data;%0A        %7D
+this._returnFileVersion.bind(this)
 );%0A 
@@ -2590,23 +2590,22 @@
     
-_
 edit: 
-observer
+computed
 ('cu
@@ -2635,32 +2635,59 @@
 ', function() %7B%0A
+        let _edit = false;%0A
         if (this
@@ -2714,88 +2714,30 @@
 )) %7B
- // don't change the value while id is not loaded.%0A            this.set('edit', 
+%0A            _edit = (
 this
@@ -2812,16 +2812,38 @@
       %7D%0A
+        return _edit;%0A
     %7D),%0A
@@ -4048,13 +4048,82 @@
 ,%0A    %7D,
+%0A%0A    _returnFileVersion(result) %7B%0A        return result.data;%0A    %7D,
 %0A%7D);%0A

Fix more linting warnings