import Ember from 'ember';
import loadAll from 'ember-osf/utils/load-relationship';

export default Ember.Component.extend({
    elementId: 'preprint-file-view',
    endIndex: 6,
    startIndex: 0,

    scrollAnim: '',
    selectedFile: null,

    hasAdditionalFiles: function() {
        return this.get('files.length') > 1;
    }.property('files'),

    hasPrev: function() {
        return this.get('startIndex') > 0;
    }.property('files', 'endIndex', 'startIndex'),

    hasNext: function() {
        return this.get('endIndex') < this.get('files.length');
    }.property('files', 'endIndex', 'startIndex'),

    __files: function() {
        this.set('files', []);
        this.set('selectedFile', null);
        this.get('preprint').get('files')
            .then(providers => {
                this.set('provider', providers.findBy('name', 'osfstorage'));
                return loadAll(this.get('provider'), 'files', this.get('files'), {'page[size]': 50});
            })
            .then(() => {
                let pf = this.get('files').findBy('id', this.get('preprint.primaryFile.id'));
                if (pf) {
                    this.get('files').removeObject(pf);
                    this.set('primaryFile', pf);
                }

                this.set('selectedFile', this.get('primaryFile'));
                this.set('files', [this.get('primaryFile')].concat(this.get('files')));
            });

    }.observes('preprint'),

    init() {
        this._super(...arguments);
        this.__files();

    },
    actions: {
        next() {
            if (this.get('endIndex') > this.get('files.length')) return;

            this.set('scrollAnim', 'toLeft');
            this.set('endIndex', this.get('endIndex') + 5);
            this.set('startIndex', this.get('startIndex') + 5);
        },
        prev() {
            if (this.get('startIndex') <= 0) return;

            this.set('scrollAnim', 'toRight');
            this.set('endIndex', this.get('endIndex') - 5);
            this.set('startIndex', this.get('startIndex') - 5);
        },
        changeFile(file) {
            this.set('selectedFile', file);

            if (this.attrs.chooseFile) {
                this.sendAction('chooseFile', file);
            }
        },
    },
});

@@ -76,16 +76,67 @@
 onship';
+%0Aimport Analytics from '../mixins/analytics-mixin';
 %0A%0Aexport
@@ -167,16 +167,27 @@
 .extend(
+Analytics, 
 %7B%0A    el
@@ -1616,24 +1616,231 @@
    next() %7B%0A
+            Ember.get(this, 'metrics')%0A                .trackEvent(%7B%0A                    category: 'file browser',%0A                    action: 'click',%0A                    label: 'next'%0A                %7D);%0A%0A
             
@@ -2095,24 +2095,231 @@
    prev() %7B%0A
+            Ember.get(this, 'metrics')%0A                .trackEvent(%7B%0A                    category: 'file browser',%0A                    action: 'click',%0A                    label: 'prev'%0A                %7D);%0A%0A
             
@@ -2565,24 +2565,232 @@
 ile(file) %7B%0A
+            Ember.get(this, 'metrics')%0A                .trackEvent(%7B%0A                    category: 'file browser',%0A                    action: 'select',%0A                    label: 'file'%0A                %7D);%0A%0A
             

Add file browser analytics