'use strict';
const Cc = Components.classes;
const Ci = Components.interfaces;
const Cr = Components.results;
const Cu = Components.utils;
Cu.import('resource://gre/modules/XPCOMUtils.jsm');
Cu.import('resource://gre/modules/Services.jsm');
Cu.import('resource://gre/modules/PlacesUtils.jsm');

XPCOMUtils.defineLazyModuleGetter(this,
      "PlacesUIUtils", "chrome://eplaces/content/PlacesUIUtils.jsm");
      
XPCOMUtils.defineLazyServiceGetter(this, 
  "KMeleon", "@kmeleon/jsbridge;1","nsIJSBridge");
        
/*let bundle = Cc["@mozilla.org/intl/stringbundle;1"].
         getService(Ci.nsIStringBundleService).
         createBundle("chrome://eplaces/locale/places.properties");*/
                           
let Places = {
  iconNew: {path:"chrome://eplaces/skin/bookmark.png", top:0, bottom:16, left:0, right:16},
  iconUpd: {path:"chrome://eplaces/skin/bookmark.png", top:0, bottom:16, left:16, right:32},
  //QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver]),
  init: function() {
    KMeleon.RegisterCmd("placesOpenBookmarks", "Show all Bookmarks", this.openBookmark);
    KMeleon.RegisterCmd("placesAddBookmark", "Add Bookmark", this.addBookmark, this.iconNew );
    KMeleon.SetMenu("placesContext", KMeleon.MENU_COMMAND, "Open Bookmarks", "placesOpenBookmarks");
    KMeleon.AddButton('Browser Con&figuration', "placesAddBookmark", 'placesContext');
    KMeleon.AddListener(this);
  },
  
  uninit: function() {
    KMeleon.SetMenu("placesContext", KMeleon.MENU_COMMAND, "", "placesOpenBookmarks");
    KMeleon.RemoveButton('Browser Con&figuration', "placesAddBookmark");
    KMeleon.RemoveListener(this);
    Cu.unload("chrome://eplaces/content/PlacesUIUtils.jsm");
  },
  
  openBookmark: function(dom) {
    dom.top.openDialog("chrome://eplaces/content/places.xul#bookmark", "",  "centerscreen,chrome,modal,resizable=yes");    
  },
  
  addBookmark: function(dom) {
    let doc = dom.document;
    let uri = doc.documentURIObject;  
    let itemId = PlacesUtils.getMostRecentBookmarkForURI(uri);
    if (itemId == -1) {
       var title = doc.title;
       var description = PlacesUIUtils.getDescriptionFromDocument(doc);
       PlacesUIUtils.showBookmarkDialog({ action: "add"
                                        , type: "bookmark"
                                        , uri: uri
                                        , title: title
                                        , description: description
                                        , hiddenRows: [ "description"
                                                      , "location"
                                                      , "loadInSidebar"
                                                      , "keyword" ]
                                        }, dom.top);
     }
     else {
       PlacesUIUtils.showBookmarkDialog({ action: "edit"
                                        , type: "bookmark"
                                        , itemId: itemId
                                        }, dom.top);
     }
     this.updateIcon(win);
  },
  
  updateIcon: function(win) {
    let uri = win.document.documentURIObject;
    if (!uri || PlacesUtils.getMostRecentBookmarkForURI(uri) == -1) {
      KMeleon.SetCmdIcon("placesAddBookmark", this.iconNew)
    } else {
      KMeleon.SetCmdIcon("placesAddBookmark", this.iconUpd)
    }
  },
  
  observe: function(win, topic) {
    if (topic == 'kmeleon-switchtab') {
      this.updateIcon(win); 
    }
  }
};

function startup(data, reason) {
  try {
    Components.classes['@kmeleon/jsbridge;1'].getService(Components.interfaces.nsIJSBridge);
    Places.init();
  } catch (e) {
    Services.obs.addObserver(
      {
        observe: function(win, topic) {
            Places.init();
            Services.obs.removeObserver(this, 'kmeleon-init'); 
        }
      },
      'kmeleon-init', 
      false
    );   
  }
}

function shutdown(data, reason) {
  Places.uninit();  
}

function install() {}
function uninstall() {}
