/*
 * File: app/controller/Disk.js
 *
 * This file was generated by Sencha Architect version 2.2.2.
 * http://www.sencha.com/products/architect/
 *
 * This file requires use of the Ext JS 4.1.x library, under independent license.
 * License of Sencha Architect does not include license for Ext JS 4.1.x. For more
 * details see http://www.sencha.com/license or contact license@sencha.com.
 *
 * This file will be auto-generated each and everytime you save your project.
 *
 * Do NOT hand edit this file.
 */

Ext.define('MyApp.controller.Disk', {
    extend: 'Ext.app.Controller',

    stores: [
        'Disk',
        'Volume',
        'DiskAdd'
    ],

    refs: [
        {
            ref: 'grid',
            selector: '#vmdiskgrid'
        },
        {
            ref: 'pathField',
            selector: '#pathField'
        },
        {
            ref: 'explorer',
            selector: 'explorer'
        }
    ],

    onDiskaddSave: function(button, e, eOpts) {
        var win = button.up('window'),
            form = win.down('form');

        if (this.save(form)) {
            win.close();
        }
    },

    openExistingDisk: function(button, e, eOpts) {
        var window = Ext.widget('diskadd');
        var store = this.getDiskStore();



    },

    explorerOpen: function(button, e, eOpts) {
        var window = Ext.widget('explorer');




    },

    onTreeviewItemClick: function(dataview, record, item, index, e, eOpts) {


        this.getPathField().setValue(record.raw.path);

        this.getExplorer().close();
    },

    onTabpanelBeforeShow: function(component, eOpts) {
        var store = this.getDiskStore();
        store.setVirtualmachineId(this.getController('Virtualmachine').getId());

    },

    deleteDisk: function(button, e, eOpts) {


        var selected = this.getGrid().getSelectionModel().getSelection();


        if (selected.length > 0 ) {
            Ext.Msg.confirm('Remove Storage', 'You are about to remove <span style="color:red;font-weight:bold;">' + selected.length+ '</span> Storage. Are you sure?', function (button) {
                if (button == 'yes') {
                    var selected = this.getGrid().getSelectionModel().getSelection();
                    this.getDiskStore().remove(selected);

                    this.getDiskStore().sync({
                        success: function(){},
                        failure: function(){this.getDiskStore().load();},
                        scope: this
                    });            
                }
            }, this);

        }
    },

    onDiskExistingButtonClick: function(button, e, eOpts) {
        button.up('window').close();
        var window = Ext.widget('diskexisting');
    },

    onButtonClickOther: function(button, e, eOpts) {
        var win = button.up('window');
        var form = win.down('form');

        if (this.save(form)) {
            win.close();
        }
    },

    onGridpanelItemDblClick: function(dataview, record, item, index, e, eOpts) {
        var window = Ext.widget('diskedit');

        window.down('form').loadRecord(record);
    },

    onButtonClick: function(button, e, eOpts) {
        var win = button.up('window');
        var form = win.down('form');

        if (this.save(form)) {
            win.close();
        }
    },

    onComboboxSelect: function(combo, records, eOpts) {

        var id = records[0].get('id');
        this.getDiskAddStore().setStorageId(id);
    },

    save: function(form) {
        var values = form.getValues();
        var store = this.getDiskStore();
        store.setVirtualmachineId(this.getController('Virtualmachine').getId());

        if (form.getForm().isValid()) {
            var record = form.getRecord();

            if (typeof(record) != 'object' ) {
                store.add(values);      
            } else {       
                record.set(values);
            }

            this.getDiskStore().sync();  

            return 1;
        } else {
            return 0;
        }

    },

    init: function(application) {
        this.control({
            "diskadd button[action=save]": {
                click: this.onDiskaddSave
            },
            "#existingDiskLink": {
                click: this.openExistingDisk
            },
            "#exploreropen": {
                click: this.explorerOpen
            },
            "explorer treepanel": {
                itemclick: this.onTreeviewItemClick
            },
            "#vmdiskgrid": {
                beforeshow: this.onTabpanelBeforeShow,
                itemdblclick: this.onGridpanelItemDblClick
            },
            "#deleteDisk": {
                click: this.deleteDisk
            },
            "#openDiskExisting": {
                click: this.onDiskExistingButtonClick
            },
            "diskexisting button[action=save]": {
                click: this.onButtonClickOther
            },
            "diskedit button[action=save]": {
                click: this.onButtonClick
            },
            "#diskaddstorage": {
                select: this.onComboboxSelect
            }
        });
    }

});
