jscape.account = jscape.account || {};

/** @class */
jscape.account.DropZonesInfoModule = jscape.ApplicationModule.extend(
    /** @lends {jscape.account.DropZonesInfoModule.prototype} */
{
    onCreate: function() {
        this._super.apply(this, arguments);

        this.view = new jscape.account.DropZonesInfoView();
    },

    onStart: function() {
        this.view.show(this);
    },

    onResume: function() {
        this.view.refresh();
    },

    onRefresh: function(params) {
        params = jscape.Page.requestParameters(params);
        return jscape.API.listDropZones(params.startIndex, params.count, params.query)
                .then(jscape.Page.datagridFormatter());
    }
});

/** @class */
jscape.account.DropZonesInfoView = Class.extend(
    /** @lends {jscape.account.DropZonesInfoView.prototype} */
{
    selection: null,

    /** @constructor */
    initialize: function() {
        var initializing = true;

        this.fieldset = $("fieldset[name=dropzonesinfofields]");

        this.data = $("table[role=grid][aria-label=dropzonesinfo]", this.fieldset);
        this.data.datagrid({
            fit: true,
            fitColumns: true,
            ctrlSelect: true,
            pagination: true,
            search: true,
            remoteSearch: true,
            sortName: "name",
            idField: "name",
            columns: [[
                {field: "name", title: $("thead th:nth-child(1)", this.data).text(), width: 80, sortable: true},
                {field: "path", title: $("thead th:nth-child(2)", this.data).text(), width: 120, sortable: true},
                {field: "uri", title: $("thead th:nth-child(3)", this.data).text(), width: 120, sortable: false, searchable: false}
            ]],
            loader: $.proxy(this._onLoad$Data, this),
            onBeforeLoad: function() { return !initializing; }
        });

        initializing = false;
    },

    show: function(listener) {
        this.listener = listener || jscape.account.DropZonesInfoView.LISTENER;
    },

    refresh: function() {
        this.data.datagrid("reload");
    },

    _onLoad$Data: function (params, success, error) {
        this.listener.onRefresh(params).done(success).fail(error);
    }
});
jscape.account.DropZonesInfoView.LISTENER = {
    onRefresh: $.noop
};