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

"use strict";

module.metadata = {
  "stability": "experimental"
};

var self = require("sdk/self");

const { BasePanel } = require("../chrome/basePanel");
const { Class } = require("sdk/core/heritage");
const { Trace, TraceError } = require("../core/trace.js").get(module.id);
const { Tool } = require("dev/toolbox");
const { Locale } = require("../core/locale.js");
const { HelloWorldSidePanel1 } = require("./helloWorldSidePanel1.js");
const { HelloWorldSidePanel2 } = require("./helloWorldSidePanel2.js");

/**
 * @panel An example panel object. This object shows how to create
 * a new panel within the {@Toolbox} and customize its behavior through
 * framework hooks.
 * This code should turn into an example extension eventually.
 */
const HelloWorldPanel = Class(
/** @lends HelloWorldPanel */
{
  extends: BasePanel,

  label: Locale.$STR("helloWorldPanelTitle"),
  tooltip: "Debug client example",
  icon: "./icon-16.png",
  url: "./helloWorld.html",

  setup: function({debuggee, frame}) {
    BasePanel.prototype.setup.apply(this, arguments);

    Trace.sysout("helloWorldPanel.setup;", frame);
  },

  onReady: function() {
    BasePanel.prototype.onReady.apply(this, arguments);

    Trace.sysout("helloWorldPanel.onReady;");

    this.debuggee.start();
    this.postMessage("RDP", [this.debuggee]);

    // Load content script and handle 'onSendMessage' sent from it.
    let { messageManager } = this.panelFrame.frameLoader;
    let url = self.data.url("helloWorldContentScript.js");
    messageManager.loadFrameScript(url, false);
    messageManager.addMessageListener("message", this.onMessage.bind(this));
  },

  onLoad: function() {
    BasePanel.prototype.onLoad.apply(this, arguments);

    Trace.sysout("helloWorldPanel.onLoad;");
  },

  onError: function() {
    BasePanel.prototype.onError.apply(this, arguments);

    Trace.sysout("helloWorldPanel.onError;");
  },

  onMessage: function(event) {
    Trace.sysout("helloWorldPanel.onMessage; (from content)", event);
  },

  getSidePanels: function() {
    return [HelloWorldSidePanel1, HelloWorldSidePanel2];
  },

  /**
   * Returns list of buttons that should be displayed within
   * the panel toolbar.
   */
  getPanelToolbarButtons: function() {
    var buttons = [];

    buttons.push({
      image: self.data.url("./breakOn.svg"),
      tooltiptext: "This is a tooltip",
      command: this.onHello.bind(this)
    });

    buttons.push("-");

    buttons.push({
      label: "Send Message",
      tooltiptext: "Send testing message to content script",
      command: this.onMessage.bind(this)
    });

    // xxxHonza: implement Menu
    buttons.push({
      type: "menu",
      label: "My Popup Menu",
      tooltiptext: "This is a tooltip",
      items: this.getMenuButtonItems()
    });

    return buttons;
  },

  /**
   * Returns list of menu items for a toolbar menu-button.
   */
  getMenuButtonItems: function() {
    var items = [];

    items.push({
      nol10n: true,
      label: "Item 1",
      command: this.onHello.bind(this)
    });

    items.push({
      nol10n: true,
      label: "Item 2",
      items: this.getSubMenuItems()
    });

    items.push({
      nol10n: true,
      label: "Item 3",
      type: "checkbox",
      checked: true,
    });

    return items;
  },

  getSubMenuItems: function() {
    var items = [];

    items.push({
      nol10n: true,
      label: "Item 2-1",
      type: "radio",
    });

    items.push({
      nol10n: true,
      label: "Item 2-2",
      type: "radio",
    });

    items.push({
      nol10n: true,
      label: "Item 2-3",
      type: "radio",
      checked: true,
    });

    return items;
  },

  /**
   * Returns a list of menu items for panel options menu.
   */
  getOptionsMenuItems: function() {
    return this.getMenuButtonItems();
  },

  /**
   * Returns a list of menu items for panel context menu.
   */
  getContextMenuItems: function() {
    return this.getMenuButtonItems();
  },

  // Command handlers

  onHello: function() {
    Trace.sysout("Hello World!");
  },
});

// Panel registration
const helloWorldTool = new Tool({
  name: "Hello World Tool",
  panels: {
    helloWorld: HelloWorldPanel
  }
});

// Exports from this module
exports.HelloWorldPanel = HelloWorldPanel;

@@ -1816,32 +1816,132 @@
 unction() %7B%0A    
+// xxxHonza: fix me:%0A    // See also: https://bugzilla.mozilla.org/show_bug.cgi?id=980410#c16%0A    //
 BasePanel.protot
@@ -1968,32 +1968,32 @@
 is, arguments);%0A
-
 %0A    Trace.sysou
@@ -2403,24 +2403,44 @@
 tons.push(%7B%0A
+      nol10n: true,%0A
       image:
@@ -2599,24 +2599,44 @@
 tons.push(%7B%0A
+      nol10n: true,%0A
       label:
@@ -2795,16 +2795,16 @@
 nt Menu%0A
-
     butt
@@ -2810,24 +2810,44 @@
 tons.push(%7B%0A
+      nol10n: true,%0A
       type: 

Fix localization