jscape.domain = jscape.domain || {};

/** @class */
jscape.DomainDescriptor = Class.extend(
    /** @lends {jscape.DomainDescriptor.prototype} */
{
    /** @constructor */
    initialize: function(a, b, c, d) {
        this.name = a || "";
        this.description = b || "";
        this.serviceDescriptors = c || [];
        this.configurations = d || [];
    }
});
jscape.DomainDescriptor.fromJSON = function(data) {
    return data ? $.extend(new jscape.DomainDescriptor(), data) : null;
};

/** @class */
jscape.domain.DomainModule = jscape.ApplicationModule.extend(
    /** @lends {jscape.domain.DomainModule.prototype} */
{
    domainName: null,

    /** @override */
    onCreate: function(managementService, domainName) {
        this.domainName = domainName;
        this._super(managementService);
    },

    _fireApplySuccess: function () {
        $(document).triggerHandler("broadcast", [window.R.string.APPLICATION_SUCCESS_APPLY]);
    }
});

/** @class */
jscape.DomainSettingsApplication = jscape.Application.extend(
    /** @lends {jscape.domain.DomainSettingsApplication.prototype} */
{
    /** @override */
    onReady: function() {
        this._forceStartModule("domain-state");
        this.view.show(this);
    },

    /** @override */
    _createView: function() {
        this._super();
        this.view = new jscape.domain.DomainSettingsView();
    },

    /** @override */
    _destroy: function () {
        this._forceStopModule("domain-state");
        this._super();
    }
});

/** @class */
jscape.domain.DomainSettingsView = jscape.DashboardView.extend(
    /** @lends {jscape.DomainSettingsView.prototype} */
{
    /** @constructor */
    initialize: function() {
        this._super($("#modules[role=tablist]"));

        this.modules.tabs({fit: true, tabPosition: "left", headerWidth: 200, onSelect: $.proxy(this._onSelect$Tab, this)});
        this.modules.find("div[role=tablist]")
                .filter(function () { return $(this).parents("div.dialog").length == 0; })
                .each($.proxy(function (_, tablist) {
                          $(tablist).tabs({
                              border: false,
                              onSelect: $.proxy(this._onSelect$Tab, this)
                          });
                      }, this));

        this.modules.find(">div.tabs-header>div.tabs-wrap")
                .hover(
                function () { $(this).css({overflowY: "auto", overflowX: "hidden"}); },
                function () { $(this).css({overflowY: "", overflowX: ""}); }
        );
    },

    /** @override */
    show: function(_, domainName) {
        this._super.apply(this, arguments);
        this.domainName = domainName || "";

        this.modules.tabs("unselect", 0).tabs("select", 0);
    }
});

/** @class */
jscape.domain.DomainStateModule = jscape.domain.DomainModule.extend(
    /** @lends {jscape.domain.DomainStateModule.prototype} */
{
    onStart: function() {
        $(document).on("domainstatechange", $.proxy(this.onResume, this));

        if (this.view == null) {
            this.view = new jscape.domain.DomainStateView(this.domainName);
        }
        this.view.show(this);
    },

    onStop: function() {
        $(document).off("domainstatechange");
    },

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

    onRefresh: function () {
        return this.managementService.getDomainStatistics(this.domainName)
                .then(function (stats) { return stats ? {status: jscape.DomainStatus.valueOf(stats), fipsMode: !!stats.fipsModeEnabled} : {}; });
    },

    onStartDomain: function () {
        return this.managementService.startDomain(this.domainName)
                .done($.proxy(function() { this.view.refresh(); }, this));
    },

    onStopDomain: function () {
        return this.managementService.stopDomain(this.domainName)
                .done($.proxy(function() { this.view.refresh(); }, this));
    },

    onPauseDomain: function () {
        return this.managementService.pauseDomain(this.domainName)
                .done($.proxy(function() { this.view.refresh(); }, this));
    },

    onResumeDomain: function () {
        return this.managementService.resumeDomain(this.domainName)
                .done($.proxy(function() { this.view.refresh(); }, this));
    }
});

/** @class */
jscape.domain.DomainStateView = Class.extend(
    /** @lends {jscape.domain.DomainStateView.prototype} */
{
    /** @constructor */
    initialize: function(domainName) {
        this.domainName = domainName || "";

        this.panel = $("#viewpane");
        this.startButton = $("a[role=button][name=startdomain]", this.panel).linkbutton({disabled: true, onClick: $.proxy(this._onClick$StartButton, this)});
        this.stopButton = $("a[role=button][name=stopdomain]", this.panel).linkbutton({disabled: true, onClick: $.proxy(this._onClick$StopButton, this)});
        this.pauseButton = $("a[role=button][name=pausedomain]", this.panel).linkbutton({disabled: true, onClick: $.proxy(this._onClick$PauseButton, this)});
        this.resumeButton = $("a[role=button][name=resumedomain]", this.panel).linkbutton({disabled: true, onClick: $.proxy(this._onClick$ResumeButton, this)});
    },

    show: function(listener) {
        this.listener = listener || jscape.domain.DomainStateView.LISTENER;
        this.setTitle("");
        this._adjustButtonsState();
    },

    refresh: function () {
        this.setTitle(window.R.string.DOMAIN_MESSAGE_LOADING);
        this.listener.onRefresh()
                .done($.proxy(function (state) {
                          var domainStatus = state.status ? window.R.string["DOMAIN_STATUS_" + (state.status).toUpperCase()] : "";
                          var fipsModeState = state.fipsMode ? window.R.string["DOMAIN_FIPS_MODE"] : "";
                          this.setTitle(domainStatus + " " + fipsModeState);
                          this._adjustButtonsState(state.status);
                      }, this))
                .fail($.proxy(function() { this.setTitle(""); }, this));
    },

    setTitle: function(title) {
        var text = window.R.string.DOMAIN_TITLE.supplant({0: this.domainName, 1: title || ""});
        this.panel.layout("panel", "center").panel("setTitle", text);
        document.title = text;
    },

    _onClick$StartButton: function() {
        this.startButton.linkbutton("disable");
        this.listener.onStartDomain()
                .always($.proxy(function() { this.startButton.linkbutton("enable"); }, this));
    },
    _onClick$StopButton: function() {
        this.stopButton.linkbutton("disable");
        this.listener.onStopDomain()
                .always($.proxy(function() { this.stopButton.linkbutton("enable"); }, this));
    },

    _onClick$PauseButton: function() {
        this.pauseButton.linkbutton("disable");
        this.listener.onPauseDomain()
                .always($.proxy(function() { this.pauseButton.linkbutton("enable"); }, this));
    },

    _onClick$ResumeButton: function() {
        this.resumeButton.linkbutton("disable");
        this.listener.onResumeDomain()
                .always($.proxy(function() { this.resumeButton.linkbutton("enable"); }, this));
    },

    _adjustButtonsState: function (status) {
        this.startButton.linkbutton(jscape.DomainStatus.STOPPED == status ? "enable" : "disable");
        this.stopButton.linkbutton(jscape.DomainStatus.STOPPED != status ? "enable" : "disable");
        this.pauseButton.linkbutton(jscape.DomainStatus.RUNNING == status ? "enable" : "disable");
        this.resumeButton.linkbutton(jscape.DomainStatus.PAUSED == status ? "enable" : "disable");
    }
});
jscape.domain.DomainStateView.LISTENER = {
    onRefresh: $.noop,
    onStartDomain: $.noop,
    onStopDomain: $.noop,
    onPauseDomain: $.noop,
    onResumeDomain: $.noop
};