//
// BEGIN FLOCK GPL
//
// Copyright Flock Inc. 2005-2009
// 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 RDFS = Components.classes["@mozilla.org/rdf/rdf-service;1"].getService (Components.interfaces.nsIRDFService);

var $ = function (x) { return document.getElementById(x); };

function onLoad() {
  //loadList('rdf:flock-favorites');  // Caution! Very time consuming operation!!!
}

function addItemToListBox(aListBox, aSubject, aPredicate, aObject) {
 var listItem = document.createElement('listitem');
 var listCell = null;
 // Add each listcell
 // First Subject
 listCell = document.createElement('listcell');
 listCell.setAttribute('label', aSubject);
 listItem.appendChild(listCell);
 // Then Predicate
 listCell = document.createElement('listcell');
 listCell.setAttribute('label', aPredicate);
 listItem.appendChild(listCell);
 // Then Object
  listCell = document.createElement('listcell');
  listCell.setAttribute('label', aObject);
  listItem.appendChild(listCell);
  // Finally add this new listitem to the listbox
  aListBox.appendChild(listItem);
}

function enumerateResources(aDataSource, aResource, aListBox) {
  
  var targets = aDataSource.ArcLabelsOut(aResource);

  while (targets.hasMoreElements()) {
    var predicate = targets.getNext();
    addItemToListBox(aListBox, aResource.Value, predicate.Value, "-");

    if (predicate instanceof Components.interfaces.nsIRDFResource) {
      enumerateResources(aDataSource, predicate, aListBox);
    } else if (predicate instanceof Components.interfaces.nsIRDFLiteral) {
      var target = faves_ds.GetTarget(aResource, predicate, true);
      // Add this aResource, predicate, target to the listbox
      addItemToListBox(aResource.Value, predicate.Value, target.Value);
    }
  }
}

function loadList(aDataSourceUrl) {
  
  //Components.classes["@mozilla.org/rdf/datasource;1?name=flock-favorites"].getService(Components.interfaces.nsIRDFDataSource);  
  var aDataSource = RDFS.GetDataSourceBlocking(aDataSourceUrl);
  
  if (!aDataSource) {
    alert("Unable to get datasource requested: " + aDataSourceUrl);
    return;
  }
  
  var rdfList = $('rdfListBox');
  var resources = aDataSource.GetAllResources();
  
  while (resources.hasMoreElements()) {
    var iResource = resources.getNext();
    addItemToListBox(rdfList, iResource.Value, "-", "-");
    enumerateResources(aDataSource, iResource, rdfList);
  }
  

}

function testStreamEnumerator() {
  dump("TEST testStreamEnumerator: getting streamUtils\n");
  var streamUtils = Components.classes["@flock.com/stream-utils;1"].createInstance(Components.interfaces.flockIStreamUtils); 

  if (!streamUtils) {
    dump("TEST testStreamEnumerator: Unable to get StreamUtils!\n");
    return;
  }

  dump("TEST testStreamEnumerator: getting faves_ds\n");
  var faves_ds = RDFS.GetDataSourceBlocking('rdf:flock-favorites')
  
  if (!faves_ds) {
    dump("TEST testStreamEnumerator: Unable to get StreamUtils!\n");
    return;
  }

  var testResource = RDFS.GetResource("http://www.w3.org/1999/02/22-rdf-syntax-ns#_1");
  dump("TEST testStreamEnumerator: calling enumerateStreams with resource:" + testResource.Value + "\n");
  var myEnumerator = streamUtils.enumerateStreams(faves_ds, testResource, true, streamUtils.STREAMS_UNSEEN);
  dump("TEST testStreamEnumerator: starting loop\n");
  while (myEnumerator && myEnumerator.hasMoreElements()) 
  {
    var resource = myEnumerator.getNext();
    if (resource instanceof Components.interfaces.nsIRDFResource) {
      dump("TEST testStreamEnumerator: unSeen Resource = " + resource.Value + "\n");
    }
  }
  dump("TEST testStreamEnumerator: Complete\n");
}

