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

/**
 * Control the behavior of the menu.
 * Wrappper around images
 * function.
 * to be documented
 *
 */


/**
 * @name menuCtrl
 * @static
 * Base class to wrap image function in a simple method
 */
var menuCtrl = {};

/**
 * @name menuCtrl.save
 * @static
 * Wrap all the save function from the images outfit
 * Nothing magic, just boring stuff!
 */
menuCtrl.save = {};


/**
 * @name menuCtrl.save.lastDirPath
 * @static
 * The last save path selected by the user
 */
menuCtrl.save.lastDirPath = null;



/**
 * @name menuCtrl.save.check
 * @static
 * Check if there is a selection
 */
menuCtrl.save.check = function(){
  var menu = document.getElementById("file-popup");
  var node = document.getElementById('owImagesContainer').thumbs.current;
  //Are we in zoom mode
  var inZoom = document.getElementById('owImagesContainer').imageViewer.zoomStatus;
  
  if(!node || !inZoom){
    menu.childNodes[1].setAttribute("disabled","true");
    menu.childNodes[2].setAttribute("disabled","true");
  }
  else{
    menu.childNodes[1].removeAttribute("disabled");
    menu.childNodes[2].removeAttribute("disabled");
  }

};



/**
 * @name menuCtrl.save.selectedImage
 * @static
 * Save a selected image in the thumbs view.
 * If nothing is selected the menuitem should be disabled.
 *
 */
menuCtrl.save.selectedImage = function(){
  var node = document.getElementById('owImagesContainer').thumbs.current;
  if(node) {
    var folder = ow.app.kernel.prefs.getString("oWImages.autocatch.autosave.folder", "");
    outwitImages.download(node, folder || "default", (optionsCtrl.autoRename ? optionsCtrl.autoRenamePattern : ""));
  }
};



/**
 * @name menuCtrl.save.selectedImageAs
 * @static
 * Save a selected image in a specified dir.
 * Once we know the dir, just call saveSelectedAs
 */
menuCtrl.save.selectedImageAs = function(){
  var node = document.getElementById('owImagesContainer').thumbs.current;
  if(node)
    outwitImages.download(node, "ask");
};


/**
 * @name menuCtrl.save.all
 * @static
 * Save all image from a specified source
 * @param {string} aSource - Source of the data to take (url/catch)
 *
 */
menuCtrl.save.all = function(aSource, alwaysAsk) {
  if (aSource == "catch" && document.getElementById('mediacatch').childNodes.length > 0
      || aSource == "thumbs" && document.getElementById('owImagesContainer').thumbs._itemsDisplayed.length > 0) {
    popupMenuCtrl.from = aSource;
    popupMenuCtrl.saveAllImagesOn('', alwaysAsk);
  }
};


menuCtrl.view = {};
menuCtrl.view.check = function() {
  var menuViewCatch = document.getElementById("menu-catch-source");
  var catchSize = document.getElementById('mediacatch').childNodes.length;

  if (catchSize == 0) {
    menuViewCatch.setAttribute('disabled', 'true');
    return false;
  }
  else {
    menuViewCatch.removeAttribute('disabled');
    return true;
  }
};

/**
 * @name menuCtrl.catch
 * @static
 * Wrappper around the catch to do some ...
 */
menuCtrl.catch = {};


/**
 * @name menuCtrl.catch.load
 * @static
 * Load a catch file by asking the user the file to load
 */
menuCtrl.catch.load = function(){
  document.getElementById('mediacatch').loadFromSqliteFile(false);
};


/**
 * @name menuCtrl.catch.save
 * @static
 * Save a catch file at a specified point!
 * @param {string} aPath - The path to save the catch to
 */
menuCtrl.catch.save = function( aPath ) {
  var theCatch = document.getElementById('mediacatch');
  theCatch.db.saveTo( theCatch.childNodes );
};

/**
 * @name menuCtrl.catch.clean
 * @static
 * Empty the catch !!!
 */
menuCtrl.catch.clean = function(){
  document.getElementById('mediacatch').clear()
};

/**
 * @name menuCtrl.catch.play
 * @static
 * Slideshow the catch !!!
 */
menuCtrl.catch.play = function() {
  if (document.getElementById('mediacatch').childNodes.length) {
    var oldmode = outwitImages.UIControls.mode;
    menuCtrl.selectSource('catch');
    menuCtrl.startSlideshow(oldmode == 'url');
  }
};

/**
 * @name menuCtrl.catch.selectAll
 * @static
 * @private
 * For now this one is disabled so make doc later!
 */
menuCtrl.catch.selectAll = function(){
};

menuCtrl.catch.sendAllCatchImages = function(){
  var message = "\r\n\r\n";
  var catchImages = document.getElementById('mediacatch').childNodes;
  for (var i = 0; i < catchImages.length; ++i) {
    if (catchImages[i].mediaUrl.substr(0, 5) != "file:")
      message += catchImages[i].mediaUrl + "\r\n";
  }
  message +="\r\n\r\n\r\n"
    + coreKit.localeService.getString('mailSignatureSeparator', 'chrome://outwit-modules-images/locale/main.properties')
    + "\r\n"
    + coreKit.localeService.getString('mailSignaturePlural', 'chrome://outwit-modules-images/locale/main.properties');
  var subject = coreKit.localeService.getString('mailSubject', 'chrome://outwit-modules-images/locale/main.properties');
  coreKit.MailService.sendMessage(subject, message);
};


/*
 *
 * CAUTION - To be worked on!
 *
 */

menuCtrl.autoSequencesSwitch = function(all) {
  optionsCtrl.autoSequencesSwitch(all);
  if (optionsCtrl.autoSequences) {
    outwitImages.extractControls.runAdjacentFromGallery();
  }
};

/**
 * @name menuCtrl.selectSource
 * @static
 * Select the soure of data to displayed in the thumbnails area
 * @param {string} aSource - The source of data to display from
 */

menuCtrl.selectSource = function( aSource ){
  switch( aSource ){
    case "catch":
      outwitImages.UIControls.showFromCatch();
    break;

    case "url":
      outwitImages.UIControls.showFromUrl();
    break;
  }
};


/**
 * @name menuCtrl.startSlideshow
 * @static
 * Wrapper around the start slideshow
 * @param {boolean} if true, return to 'URL' mode on stop
 *
 */
menuCtrl.startSlideshow = function(goToUrlOnStop){
  document.getElementById('owImagesContainer').startSlideshow(0, goToUrlOnStop);
};


/**
 * @name menuCtrl.showOptions
 * @static
 * Show the options
 */
menuCtrl.showOptions = function(){
  document.getElementById('owImagesContainer').showOptions()
};


/**
 * @name menuCtrl.nav
 * Lazyness...
 */
menuCtrl.nav = {};

menuCtrl.nav.check = function(){
  var menu = document.getElementById("nav-popup");
  /*
  if(browserListener.timeout){
    menu.childNodes[0].setAttribute("disabled","true");
    menu.childNodes[1].setAttribute("disabled","true");
  }
  else{
    widgets.page.browser.nextUrl = outwitKit.nextPage.getNext(gBrowser.contentDocument,gBrowser.currentURI);
    menu.childNodes[0].removeAttribute("disabled");
      menu.childNodes[1].removeAttribute("disabled");
    
    if(!widgets.page.browser.nextUrl)
      menu.childNodes[1].setAttribute("disabled","true");
  }*/
};

menuCtrl.nav.back = function(){
  var webNav = widgets.page.browser.webNavigation;
  var sessionHistory = webNav.sessionHistory;
  var index = sessionHistory.index;
  var entry = sessionHistory.getEntryAtIndex(index-1, false);
  if(entry){
    var url = entry.URI.spec;
    if(url != "about:blank"){
      coreKit.coreMessager.observe("BROWSE_start", 1, 1, '');
      if (outwitImages.UIControls.clearOnNew) outwitImages.UIControls.container.clear();
      browserListener.stopIt();
      document.getElementById("urlbar").value = url;
      widgets.page.goBack();
    }
  }
};

menuCtrl.nav.next = function() {
  widgets.page.goNext();
};

menuCtrl.nav.reload = function(){
  outwitImages.UIControls.mode = outwitImages.UIControls.MODE_URL;
  widgets.page.reload();
};

menuCtrl.nav.stop = function(){
  browserListener.stopIt();
}

menuCtrl.open = {};

menuCtrl.open.fromDisk = function(){
  var fm = coreKit.fileManager.filePicker.get();
  if(fm){
    fm.QueryInterface(Components.interfaces.nsIFile);
    var path = "file://" + fm.path;
    outwitImages.UIControls.mode = outwitImages.UIControls.MODE_URL;
    outwitImages.browserControls.loadURI(path);
  }
};



menuCtrl.browser = {};

menuCtrl.browser.open = function( anURL ){
  var win = window.open(anURL);
};
