﻿///<reference path="../../Lib/Extjs_Intellisense.js" />

Ext.namespace('LUL');

LUL.FilePanel = Ext.extend(Ext.Panel, {
    id: 'filePanel',
    region: 'center',
    bodyStyle: 'padding: 10px',
    layout: 'fit',

    bodyStyle: 'overflow-y: auto',

    constructor: function(c) {
        LUL.FilePanel.superclass.constructor.call(this, Ext.apply(c, {
            tbar: new Ext.Toolbar(),
            keys: [
                { key: Ext.EventObject.DELETE, fn: this.onDeleteFile, scope: this },
                { key: Ext.EventObject.ENTER, fn: this.onOpenSelectedFile, scope: this }
            ]
        }));
    },

    afterRender: function() {
        LUL.FilePanel.superclass.afterRender.call(this);

        this.nav = new Ext.KeyNav(this.getEl(), {
            left: this.keyboardNav.createDelegate(this, ['left']),
            right: this.keyboardNav.createDelegate(this, ['right']),
            up: this.keyboardNav.createDelegate(this, ['up']),
            down: this.keyboardNav.createDelegate(this, ['down']),
            scope: this
        });
    },

    onRender: function(ct, position) {
        LUL.FilePanel.superclass.onRender.call(this, ct, position);

        this.uploadButton = this.getTopToolbar().addButton({ text: 'Ladda upp', iconCls: 'icon-plus' });
        this.deleteButton = this.getTopToolbar().addButton({ text: 'Ta bort', iconCls: 'icon-minus' });
        this.getTopToolbar().add({ xtype: 'tbseparator' });
        //this.moveButton = this.getTopToolbar().addButton({ text: 'Flytta' });
        //this.getTopToolbar().add({ xtype: 'tbseparator' });
        this.openButton = this.getTopToolbar().addButton({ text: 'Öppna', iconCls: 'icon-open' });
        this.downloadButton = this.getTopToolbar().addButton({ text: 'Hämta', iconCls: 'icon-save' });

        // händelsehantering för toolbar
        this.uploadButton.on('click', this.onUploadFile, this);
        this.downloadButton.on('click', this.onDownloadFile, this);
        this.openButton.on('click', this.onOpenSelectedFile, this);
        //this.moveButton.on('click', this.onMoveFile, this);
        this.deleteButton.on('click', this.onDeleteFile, this);
    },

    initComponent: function() {
        LUL.FilePanel.superclass.initComponent.call(this);

        this.addEvents(
            'activateFile',
            'openfile',
            'selectfile',
            'loadfile',
            'uploadFile',
            'deletefile',
            'downloadfile',
            'movefile'
        );

        // dataview som visar innehåll i en mapp
        this.contents = new LUL.FileView({ id: 'fileView', type: this.type });
        // vidarebefodring av händelser
        this.contents.on('selectionchange', this.onSelectFile, this);
        this.contents.store.on('load', this.onLoadFile, this);
        this.contents.on('dblclick', this.onOpenFile, this);

        // popupmeny
        this.openMenuItem = new Ext.menu.Item({ text: 'Öppna' });
        this.deleteMenuItem = new Ext.menu.Item({ text: 'Ta bort' });

        this.openMenuItem.on('click', this.onOpenSelectedFile, this);
        this.deleteMenuItem.on('click', this.onDeleteFile, this);

        this.contextMenu = new Ext.menu.Menu({
            items: [
                this.openMenuItem,
                '-',
                this.deleteMenuItem
            ]
        });

        this.contents.on('contextmenu', this.showContextMenu, this);

        this.add(this.contents);
    },

    keyboardNav: function(key) {
        if (this.contents.getSelectedNodes().length > 0) {
            var count = this.contents.getNodes().length;
            var sel = this.contents.getSelectedNodes()[0];
            var index = this.contents.indexOf(sel);
            var perRow = Math.floor(this.getInnerWidth() / sel.clientWidth);
            switch (key) {
                case 'left':
                    if (index > 0) {
                        this.contents.clearSelections();
                        this.contents.select(index - 1);
                    }
                    break;
                case 'right':
                    if (index < count - 1) {
                        this.contents.clearSelections();
                        this.contents.select(index + 1);
                    }
                    break;
                case 'up':
                    if (index >= perRow) {
                        this.contents.clearSelections();
                        this.contents.select(index - perRow);
                    }
                    break;
                case 'down':
                    if (index < count - perRow) {
                        this.contents.clearSelections();
                        this.contents.select(index + perRow);
                    }
                    break;
            }
        }
    },

    // visa popupmeny
    showContextMenu: function(view, index, node, e) {
        view.select(node);
        this.contextMenu.showAt(e.getXY());

    },

    onSelectFile: function() {
        this.fireEvent('selectfile');
    },

    onLoadFile: function() {
        this.fireEvent('loadfile');
    },

    onOpenFile: function(view, index, node) {
        var record = view.getRecord(node);
        record.data.path = this.contents.store.reader.meta.path;
        this.fireEvent('activateFile', record.data);
    },

    onUploadFile: function() {
        this.fireEvent('uploadFile');
    },

    onOpenSelectedFile: function() {
        this.fireEvent('openfile', this.contents.getSelectedRecords()[0].data);
    },

    onDeleteFile: function() {
        Ext.Msg.confirm('Bekräfta borttagning', 'Vill du verkligen ta bort markerad fil? Åtgärden kan inte ångras och paneler som eventuellt använder filen kommer inte att fungera som tänkt.', function(btn) {
            if (btn == 'yes') this.fireEvent('deletefile', this.contents.getSelectedData());
        }, this);        
    },

    onMoveFile: function() {
        this.fireEvent('movefile', this.contents.getSelectedData());
    },

    onDownloadFile: function() {
        this.fireEvent('downloadfile', this.contents.getSelectedRecords()[0].data);
    },

    getSelectionText: function() { return this.contents.getSelectionText(); },
    loadFiles: function(path) { this.contents.loadFiles(path); }
});
