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

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

        this.quotasController = new jscape.account.QuotasController(username);
        this.monitorsController = new jscape.account.DirectoryMonitorsController(username);
    },

    onStart: function() {
        this.quotasController.start();
        this.monitorsController.start();
    },

    onResume: function() {
        this.quotasController.onResume();
        this.monitorsController.onResume();
    }
});

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

    /** @constructor */
    initialize: function(username) {
        this.username = username;
        this.view = new jscape.account.QuotasView();
    },

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

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

    onRefresh: function() {
        return jscape.API.getUserQuota(this.username)
                .done($.proxy(this._setupView, this));
    },

    _setupView: function(quota) {
        if (quota) {
            this.view.setUploadsQuota(quota.uploadsQuota);
            this.view.setDownloadsQuota(quota.downloadsQuota);
            this.view.setTransfersQuota(quota.transfersQuota);
        } else {
            this.view.setUploadsQuota(null);
            this.view.setDownloadsQuota(null);
            this.view.setTransfersQuota(null);
        }
    }
});

/** @class */
jscape.account.QuotasView = Class.extend(
    /** @lends {jscape.account.QuotasView.prototype} */
{
    /** @constructor */
    initialize: function() {
        this.sizeFormat = new jscape.SizeFormat();
        this.fieldset = $("fieldset[name=quotasusagefields]");

        this.uploadsQuota = $("span[aria-label=uploadsusage]", this.fieldset);
        this.downloadsQuota = $("span[aria-label=downloadsusage]", this.fieldset);
        this.transfersQuota = $("span[aria-label=transfersusage]", this.fieldset);

        this.refreshButton = $("a[role=button][name=refresh]", this.fieldset).linkbutton({onClick: $.proxy(this._onClick$RefreshButton, this)});
    },

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

    refresh: function() {
        this.listener.onRefresh();
    },

    setUploadsQuota: function(quota) {
        this.uploadsQuota.empty().text(this._formatQuota(quota)).css(this._styleQuota(quota));
    },

    setDownloadsQuota: function(quota) {
        this.downloadsQuota.empty().text(this._formatQuota(quota)).css(this._styleQuota(quota));
    },

    setTransfersQuota: function(quota) {
        this.transfersQuota.empty().text(this._formatQuota(quota)).css(this._styleQuota(quota));
    },

    _formatQuota: function(quota) {
        return quota ? window.R.string.QUOTAS_TEMPLATE_USAGE.supplant({0: this.sizeFormat.format(quota.currentValue), 1: this.sizeFormat.format(quota.value)}) : "-";
    },

    _styleQuota: function (quota) {
        return quota ? !quota.exceeded() ? {color:"green"} : {color:"red"} : {color: "#000"};
    },

    _onClick$RefreshButton: function() {
        this.refreshButton.linkbutton("disable");
        this.listener.onRefresh()
                .always($.proxy(function() { this.refreshButton.linkbutton("enable"); }, this));
    }
});
jscape.account.QuotasView.LISTENER = {
    onRefresh: $.noop
};

/** @class */
jscape.account.DirectoryMonitorsController = jscape.ui.DatagridController.extend(
    /** @lends {jscape.account.DirectoryMonitorsController.prototype} */
{
    username: null,

    /** @constructor */
    initialize: function(username) {
        this.username = username;
        this.view = new jscape.account.DirectoryMonitorsView();
    },

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

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

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

/** @class */
jscape.account.DirectoryMonitorsView = jscape.ui.DatagridView.extend(
    /** @lends {jscape.account.DirectoryMonitorsView.prototype} */
{
    /** @constructor */
    initialize: function() {
        var initializing = true;

        this.sizeFormat = new jscape.SizeFormat();
        this.fieldset = $("fieldset[name=dirmonitorsusagefields]");

        this.data = $("table[role=grid][aria-label=dirmonitors]", this.fieldset);
        this.data.datagrid({
            fit: true,
            fitColumns: true,
            singleSelect: true,
            pagination: true,
            search: true,
            remoteSearch: true,
            remoteSort: true,
            sortName: "name",
            idField: "name",
            columns: [[
                {field: "path", title: $("thead th:nth-child(1)", this.data).text(), width: 200, sortable: true},
                {field: "quota", title: $("thead th:nth-child(2)", this.data).text(), width: 200, searchable: false, formatter: $.proxy(this._formatQuota, this), styler: $.proxy(this._styleQuota, this)}
            ]],
            loader: $.proxy(this._onLoad$Data, this),
            onBeforeLoad: function () { return !initializing; }
        });

        initializing = false;
    },

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

    _onLoad$Data: function(params, success, error) {
        this.listener.onRefresh(params).done(success).fail(error);
    },

    _formatQuota: function (quota) {
        return quota ? window.R.string.QUOTAS_TEMPLATE_USAGE.supplant({0: this.sizeFormat.format(quota.currentValue), 1: this.sizeFormat.format(quota.value)}) : "";
    },

    _styleQuota: function(quota) {
        return quota && !quota.exceeded() ? "color:green" : "color:red";
    }
});
jscape.account.DirectoryMonitorsView.LISTENER = {
    onRefresh: $.noop
};
