/*
 * Copyright (c) OutWit Technologies 2007-2017.
 * All rights reserved.
 */

/**
 * Control the behavior of the context menu.
 */

/**
 * @name popupMenuCtrl
 * @static
 */
var popupMenuCtrl = {};

popupMenuCtrl.from = null;
popupMenuCtrl.viewSourceWindow = null;

/**
 * Call this method on contextmenu event
 *
 * @param anEvent the event
 * @param anElement the source element
 */
popupMenuCtrl.onPopupShowing = function() {
  switch (document.popupNode.nodeName) {
    case "mediaitem":
      popupMenuCtrl.from = gCatch;
      document.getElementById('owDocsContextMenuRemove').disabled = false;
      document.getElementById('owDocsContextMenuSendToCatch').disabled = true;
      break;
    case "IMG":
    case "SPAN":
    case "CENTER":
    case "treechildren":
      popupMenuCtrl.from = document.getElementById("gFolder");
      document.getElementById('owDocsContextMenuRemove').disabled = true;
      document.getElementById('owDocsContextMenuSendToCatch').disabled = false;
      break;
    case "HTML":
    case "mediacatch":
    default:
      return false;
      break;
  }

  switch (popupMenuCtrl.from.selectedItems.length) {
    case 1:
      document.getElementById('owDocsContextMenuExplore').disabled = false;
      break;
    default:
      document.getElementById('owDocsContextMenuExplore').disabled = true;
      break;
  }
  //var selection = popupMenuCtrl.from.selectedItems;
  return true;
};


/**
 * Open a document
 */
popupMenuCtrl.openDocument = function pMC_openDocument() {
  popupMenuCtrl.from.open();
};


/**
 * Open source Urls of selected element in a new window
 *
 * @TODO multiple selection ?
 */
popupMenuCtrl.openSourceURL = function pMC_openSourceURL() {
  var items = popupMenuCtrl.from.selectedItems;
  for (var i = 0; i < items.length; ++i) {
    menuCtrl.browser.open(items[i].sourceUrl);
  }
};


/**
 * ??
 */
popupMenuCtrl.explore = function pMC_explore() {
  var items = popupMenuCtrl.from.selectedItems[0];
  /*
  var upUrl = urlAnalyser.canClimb(items.mediaUrl);
  if (upUrl) historyHandler.newGo(upUrl);
  */

  var bean = outwitKit.URLHistoryService.getBeanFromString(items.mediaUrl, true);
  if (bean.upPageURI) {
    widgets.page.loadURI(bean.upPageURI, items.mediaUrl);
  }
  else {
    try {
      coreKit.urlHelper.getUpURL(bean.URI, function(resultBean) {
        if (resultBean) {
          var bean = outwitKit.URLHistoryService.getBeanFromString(items.mediaUrl, true);
          outwitKit.URLHistoryService.setBeanData(bean, "upPageURI", resultBean.URI);
          historyHandler.newGo(resultBean.URI.spec, null, null, items.mediaUrl);
        }
      });
    }
    catch(e) {
      ow.log.error("Error getting up page", e);
    }
  }

};


/**
 * Remove selected elements
 */
popupMenuCtrl.remove = function pMC_remove() {
  popupMenuCtrl.from.removeSelection();
};


/**
 * Save selected elements on (desktop/last choosed dir) ??
 */
popupMenuCtrl.save = function pMC_save() {
  return menuCtrl.file.saveSelected(popupMenuCtrl.from);
};


/**
 * Ask for a directory and save selected elements
 */
popupMenuCtrl.saveAs = function pMC_saveAs() {
  return menuCtrl.file.saveSelectedAs(popupMenuCtrl.from);
};


/**
 * Save all documents
 */
popupMenuCtrl.saveAll = function pMC_saveAll() {
  return menuCtrl.file.saveAll(popupMenuCtrl.from);
};


/**
 * Copy selected documents links to clipboard
 */
popupMenuCtrl.copyLink = function pMC_copyLink() {
  var items = popupMenuCtrl.from.selectedItems;
  var str = "";
  for (var i = 0; i < items.length; ++i) {
    str += items[i].mediaUrl;
    if (i != items.length - 1) str += "\r\n";
  }
  var clipboardHelper = Components.classes["@mozilla.org/widget/clipboardhelper;1"]
                                  .getService(Components.interfaces.nsIClipboardHelper);
  clipboardHelper.copyString(str);
};


/**
 * Send selected documents links by mail
 */
popupMenuCtrl.sendLink = function pMC_sendLink() {
  try {
  var items = popupMenuCtrl.from.selectedItems;
  var message = "\r\n\r\n";
  for (var i = 0; i < items.length; ++i) {
    message += items[i].mediaUrl + "\r\n";
  }
  message += "\r\n\r\n\r\n"
          + coreKit.localeService.getString('mailSignatureSeparator', 'chrome://outwit-modules-docs/locale/main.properties')
          + "\r\n"
          + coreKit.localeService.getString((items.length > 1 ? 'mailSignaturePlural' : 'mailSignatureSingular'), 'chrome://outwit-modules-docs/locale/main.properties');

  var subject = coreKit.localeService.getString('mailSubject', 'chrome://outwit-modules-docs/locale/main.properties');
  coreKit.MailService.sendMessage(subject, message);
  }
  catch(e) {
    ow.log.error("Error sending mail", e);
  }
};


/**
 * Send selected document to catch
 */
popupMenuCtrl.sendToCatch = function pMC_sendToCatch() {

  gContainer.handleEnter();
};


/**
 * Extract selected elements text
 */
popupMenuCtrl.extractText = function pMC_extractText() {

};
