// BEGIN FLOCK GPL
//
// Copyright Flock Inc. 2005-2008
// http://flock.com
//
// This file may be used under the terms of the
// GNU General Public License Version 2 or later (the "GPL"),
// http://www.gnu.org/licenses/gpl.html
//
// Software distributed under the License is distributed on an "AS IS" basis,
// WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
// for the specific language governing rights and limitations under the
// License.
//
// END FLOCK GPL

const CC = Components.classes;
const CI = Components.interfaces;
const CU = Components.utils;

CU.import("resource:///modules/FlockXPCOMUtils.jsm");

const FLOCK_SHELF_CLASS_NAME = "Flock Shelf Service";
const FLOCK_SHELF_CLASS_ID =
  Components.ID("{ee266aa8-bc64-4e6a-9a20-1143b0444fbd}");
const FLOCK_SHELF_CONTRACTID =
  "@mozilla.org/rdf/datasource;1?name=flock-shelf";

const FLOCK_NS = "http://flock.com/rdf#";
const FLOCK_SHELF_ROOT = "urn:flock:shelfroot";


function flockShelfService() {
  this._coop = CC["@flock.com/singleton;1"]
               .getService(CI.flockISingleton)
               .getSingleton("chrome://flock/content/common/load-faves-coop.js")
               .wrappedJSObject;

  this.shelfRoot = this._coop.get(FLOCK_SHELF_ROOT);
  if (!this.shelfRoot) {
    this.shelfRoot = new this._coop.Folder(FLOCK_SHELF_ROOT);
    this._coop.favorites_root.children.add(this.shelfRoot);
  }
}

flockShelfService.prototype = new FlockXPCOMUtils.genericComponent(
  FLOCK_SHELF_CLASS_NAME,
  FLOCK_SHELF_CLASS_ID,
  FLOCK_SHELF_CONTRACTID,
  flockShelfService,
  CI.nsIClassInfo.SINGLETON,
  [CI.flockIShelfService]
);

// Shelf Interface Implementation
flockShelfService.prototype.elementsCount =
function SHELF_elementsCount() {
  return this._getChildCount(this.shelfRoot);
}

flockShelfService.prototype.insert =
function SHELF_insert(aTitle, aContent, aUrl, aType, aPos, aParent) {
  var date = new Date();
  var id = FLOCK_SHELF_ROOT
           + ":obj_" + date.getTime()
           + ":" + Math.round(100 * Math.random());

  var item = new this._coop.WebSnippet(id,
    {
      content: aContent, 
      name: aTitle, 
      creationDate: date, 
      URL: aUrl, 
      snippetType: aType
    });

  var parentNode = aParent ? this._coop.get(aParent) : this.shelfRoot;
  if (aPos < 0) {
    parentNode.children.add(item);
  } else {
    parentNode.children.insertAt(item, aPos);
  }

  this._refreshCount(parentNode);
  return id;
}


flockShelfService.prototype.createFolder =
function SHELF_createFolder(aName) {
  var id = FLOCK_SHELF_ROOT + ":folder:" + aName;

  var folder = new this._coop.Folder(id, { name: aName });
  this.shelfRoot.children.add(folder);

  return id;
}

flockShelfService.prototype.moveTo =
function SHELF_moveTo(aId, aFolder) {
  var folderId = FLOCK_SHELF_ROOT + ":folder:" + aFolder;

  var item = this._coop.get(aId);
  if (!item) {
    debug("*** ERROR: Can't find "+aId+"\n");
    return;
  }

  var folder = this._coop.get(folderId); 
  if (!folder) {
    this.createFolder(aFolder);
    folder = this._coop.get(folderId); 
  }

  if (folder.children.indexOf(item) > 0) {
    // Already in the folder
    return;
  }

  var parent = item.getParent();
  parent.children.remove(item);
  folder.children.add(item);

  this._refreshCount(parent);
  this._refreshCount(folder);
}

flockShelfService.prototype.changeAttribute =
function SHELF_changeAttribute(aUrn, aAttribute, aValue){
  var snippet = this._coop.get(aUrn);
  snippet[aAttribute] = aValue;
}

flockShelfService.prototype.getPositionOf =
function SHELF_getPositionOf(aUrn) {
  var snippet = this._coop.get(aUrn);

  var parent = snippet.getParent();
  return parent.children.indexOf(snippet);
}

flockShelfService.prototype.getTargetOf =
function SHELF_getTargetOf(aUrn, aTarget) {
  var snippet = this._coop.get(aUrn);
  return (snippet ? snippet[aTarget] : null);
}

flockShelfService.prototype.remove =
function SHELF_remove(aUrn) {
  var snippet = this._coop.get(aUrn);

  var parent = snippet.getParent();
  parent.children.remove(snippet);
  this._refreshCount(parent);

  snippet.destroy();
}

flockShelfService.prototype.clear =
function SHELF_clear() {
  var children = this.shelfRoot.children.enumerate();
  var elts = [];
  while (children.hasMoreElements()) {
    elts.push(children.getNext());
  }
  while (elts.length > 0) {
    var elt = elts.pop();
    this.shelfRoot.children.remove(elt);
    elt.destroy();
  }
}

flockShelfService.prototype._getChildCount =
function SHELF__getChildCount(obj) {
  var count = 0;

  var children = obj.children.enumerate();
  while (children.hasMoreElements()) {
    children.getNext();
    count++;
  }

  return count;
}

flockShelfService.prototype._refreshCount =
function SHELF__refreshCount(aFolder) {
  if (!aFolder.children) {
    return;
  }

  aFolder.count = this._getChildCount(aFolder);
}

var NSGetModule = FlockXPCOMUtils.generateNSGetModule(FLOCK_SHELF_CLASS_NAME,
                                                      [flockShelfService]);
