/* See license.txt for terms of usage */

define([
    "firebug/lib/object",
    "firebug/firebug",
    "firebug/chrome/firefox",
    "firebug/lib/locale",
    "firebug/lib/events",
    "firebug/lib/dom",
    "firebug/lib/options",
    "firebug/firefox/browserOverlayLib",
],
function(Obj, Firebug, Firefox, Locale, Events, Dom, Options, BrowserOverlayLib) {

// ********************************************************************************************* //
// Constants

const Cc = Components.classes;
const Ci = Components.interfaces;

// ********************************************************************************************* //
// Module Implementation

/**
 * @module StartButton module represents the UI entry point to Firebug. This "start button"
 * formerly known as "the status bar icon" is automatically appended into Firefox toolbar
 * (since Firefox 4).
 *
 * Start button is associated with a menu (fbStatusContextMenu) that contains basic actions
 * such as panel activation and also indicates whether Firebug is activated/deactivated for
 * the current page (by changing its color).
 */
Firebug.StartButton = Obj.extend(Firebug.Module,
/** @lends Firebug.StartButton */
{
    dispatchName: "startButton",

    initializeUI: function()
    {
        Firebug.Module.initializeUI.apply(this, arguments);

        if (FBTrace.DBG_INITIALIZE)
            FBTrace.sysout("StartButton.initializeUI;");

        // When Firebug is full loaded content of the start button tooltip
        // will be generated by this StartButton object.
        var firebugButton = Firefox.getElementById("firebug-buttonTooltip");
        var listener = Firebug.StartButton.onTooltipShowing.bind(this);
        firebugButton.addEventListener("popupshowing", listener);
    },

    shutdown: function()
    {
    },

    // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
    // Tooltip

    onTooltipShowing: function(event)
    {
        var tooltip = event.target;
        var doc = tooltip.ownerDocument;

        Dom.eraseNode(tooltip);

        with (BrowserOverlayLib)
        {
            tooltip.appendChild($label(doc, {
                "class": "version",
                value: "Firebug " + Firebug.getVersion()
            }));

            var status = $el(doc, "hbox");
            tooltip.appendChild(status);

            var suspended = Firebug.getSuspended();
            var contexts = Firebug.TabWatcher.contexts.length;

            // The tooltip shows number of active contexts (aka tabs with active Firebug)
            // for every tab, even for those that don't have Firebug active.
            // This should help the user to see that there is active Firebug instance(s) in
            // a background Firefox tab.
            status.appendChild($label(doc, {
                "class": "status",
                value: (contexts == 0) ? Locale.$STR("startbutton.tip.deactivated") :
                    Locale.$STRP("plural.Total_Firebugs2", [contexts])
            }));

            if (contexts == 0)
                return;

            status.appendChild($label(doc, {
                "class": "placement",
                value: "(" + Locale.$STR(Firebug.getPlacement()) + ")"
            }));

            if (Firebug.allPagesActivation == "on")
            {
                tooltip.appendChild($label(doc, {
                    "class": "alwaysOn",
                    value: Locale.$STR("enablement.on") + " " +
                        Locale.$STR("enablement.for_all_pages")
                }));
            }

            // If there are active contexts, but not on the current page make a little
            // note about that
            if (!Firebug.currentContext && contexts)
            {
                tooltip.appendChild($label(doc, {
                    "class": "activeInBackground",
                    value: Locale.$STR("enablement.active_in_background")
                }));
            }

            // Panel enablement status info
            tooltip.appendChild($label(doc, {
                "class": "enablement",
                value: Locale.$STR("enablement.Panel_activation_status")
            }));

            var statuses = this.getEnablementStatus();
            for (var i=0; i<statuses.length; i++)
            {
                var status = statuses[i];
                var parent = $el(doc, "hbox");
                tooltip.appendChild(parent);

                parent.appendChild($label(doc, {
                    "class": "panelName " + status.status,
                    value: status.name + ":"
                }));

                parent.appendChild($label(doc, {
                    "class": "panelStatus " + status.status,
                    value: status.statusLabel
                }));
            }
        }
    },

    // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
    // Error count

    showCount: function(errorCount)
    {
        var firebugButton = Firefox.getElementById("firebug-button");
        if (errorCount && Firebug.showErrorCount)
        {
            if (firebugButton)
            {
                firebugButton.setAttribute("showErrors", "true");
                firebugButton.setAttribute("errorCount", errorCount);
            }
        }
        else
        {
            if (firebugButton)
            {
                firebugButton.removeAttribute("showErrors");

                // Use '0', so the horizontal space for the number is still allocated.
                // The button will cause re-layout if there are more than 9 errors.
                firebugButton.setAttribute("errorCount", "0");
            }
        }
    },

    // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
    // Tooltip

    resetTooltip: function()
    {
        var firebugStatus = Firefox.getElementById("firebugStatus");
        if (!firebugStatus)
            return;

        // The start button is half/colorful if Firebug is active in the background.
        var contexts = Firebug.TabWatcher.contexts.length;
        var active = (contexts > 0) ? "background" : "false";

        // The start button is colorful if Firebug is active on the current page.
        active = Firebug.currentContext ? "true" : active;
        firebugStatus.setAttribute("firebugActive", active);

        if (FBTrace.DBG_TOOLTIP)
        {
            FBTrace.sysout("StartButton.resetTooltip; active: " + active +
                ", contexts: " + contexts);
        }
    },

    getEnablementStatus: function()
    {
        var firebugStatus = Firefox.getElementById("firebugStatus");
        if (!firebugStatus)
            return;

        var panels = Firebug.getActivablePanelTypes();
        var statuses = [];

        var strOn = Locale.$STR("enablement.on");
        var strOff = Locale.$STR("enablement.off");

        for (var i=0; i<panels.length; ++i)
        {
            var panelName = panels[i].prototype.name;
            var status = firebugStatus.getAttribute(panelName);
            var statusLabel = (status == "on") ? strOn : strOff;

            statuses.push({
                name: Firebug.getPanelTitle(panels[i]),
                status: status,
                statusLabel: statusLabel
            });
        }

        return statuses;
    },

    // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
    // Activation

    getSuspended: function()
    {
        var suspendMarker = Firefox.getElementById("firebugStatus");
        if (suspendMarker && suspendMarker.hasAttribute("suspended"))
            return suspendMarker.getAttribute("suspended");

        return null;
    },

    setSuspended: function(value)
    {
        var suspendMarker = Firefox.getElementById("firebugStatus");

        if (FBTrace.DBG_ACTIVATION)
            FBTrace.sysout("StartButton.setSuspended; to " + value + ". Browser: " +
                Firebug.chrome.window.document.title);

        if (value == "suspended")
            suspendMarker.setAttribute("suspended", value);
        else
            suspendMarker.removeAttribute("suspended");

        this.resetTooltip();
    }
});

// ********************************************************************************************* //
// Registration

Firebug.registerModule(Firebug.StartButton);

// ********************************************************************************************* //

return Firebug.StartButton;
});
