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

/**
 * Implementation of a category manager who keep tracks of the current user
 * research over the time and store the search result inside a name category
 * 
 * We keep a special catagory called catch to be able to mark object that already
 * exist in the user catch
 *
 * @author vn
 * @version 0.1
 */

var CategoryManager = {
  _category:{},

  add:function( anURL, extensions, aSearch, anEngine, aConf){
    var catName = this.getCategoryName(anURL, aSearch, anEngine);
    if( !this._category[catName] )
      this._category[catName] = new CM_category( catName, anURL, extensions, aSearch, anEngine, aConf );
    else {
      this._category[catName].setExtensions(extensions);
      this._category[catName].setLastUrl(anURL);
    }
    return catName;
  },
  
  addItem:function( aCategory, anItem ){
    var cat = this.getCategory( aCategory );
    if( cat == undefined )
      this.add( aCategory );
    cat = this.getCategory( aCategory );
    cat.add( anItem );
  },

  remove:function( aCategory ){
    if( this._category[aCategory] )
      delete this._category[aCategory];
  },

/* Not used ? 
  getByIndex:function( anIndex ){
    let i = 0;
    for (cat in this._category){
      if( i == anIndex )
        return this._category[cat];
      i++;
    }
    return null;
  },
*/
  getCategoryName: function(aCategory, aSearch, anEngine) {
    return (aSearch ? anEngine + '##' + aSearch : aCategory);
  },

  getCategory:function( aCategory ){
    return this._category[aCategory];
  },

  getAll:function(){
    var cats = [];
    for( cat in this._category )
      cats.push( this._category[cat] );
    return cats;
  },

  length:function(){
    return this.getAll().length;
  },
};



var CM_category = function( name, url, extensions, searchWords, searchEngine, aConf ){
  var _stock = {};
  var _stockLength = 0;
  var _selfName = name;
  var _selfUrl = url;
  var _lastUrl = url;
  var _searchWords = searchWords;
  var _searchEngine = searchEngine;

  var _conf = aConf;

  var _extensions = extensions;
  
  this.add = function( aBean ){
    if (_stock[aBean.mediaUrl]) return false;
    else {
      _stock[aBean.mediaUrl] = aBean;
      ++_stockLength;
      return true;
    }
  };

  this.remove = function( aMediaUrl ){
    if (_stock[aMediaUrl]) {
      delete _stock[aMediaUrl]; 
      --_stockLength;
      return true;
    }
    else
      return false;
  };

  this.length = function(){
    return _stockLength;
  };

  this.get = function(aMediaUrl){
    return _stock[aMediaUrl];
  };

  this.getAll = function(){
    return _stock;
  };

  this.getName = function(){
    return _selfName;
  };

  this.getUrl = function(){
    return _selfUrl;
  };

  this.getExtensions = function() {
    return _extensions;
  };

  this.setExtensions = function(extArray) {
    _extensions = extArray;
  };

  this.getLastUrl = function(){
    return _lastUrl;
  };

  this.getNextUrl = function(){
    var bean = outwitKit.URLHistoryService.getBeanFromString(_lastUrl, true);
    return (bean.nextPageURI ? bean.nextPageURI.spec : null);
  };

  this.setLastUrl = function(url){
    _lastUrl = url;
  };

  this.getWords = function(){
    return _searchWords;
  };

  this.getEngine = function(){
    return _searchEngine;
  };

  this.getViewName = function() {
    return (_searchWords ? _searchWords + " [" + _searchEngine + "]" : _selfName);
  };

  this.mark = function( aMediaUrl ){
    if(_selfName == 'catch') return;
    var items = this.getAll();
    for( let i in items){
      if( items[i].mediaUrl == aMediaUrl ){
        items[i].inCatch = true;
        items[i].isInCatch = true;
        items[i].emblems.add(items[i].emblems.kCatch);
        break;
      }
    }
  };

  this.unmark = function( aMediaUrl ){
    if(_selfName == 'catch') return;
    var items = this.getAll();
    for( let i in items ){
      if( items[i].mediaUrl == aMediaUrl ){
        items[i].inCatch = false;
        items[i].isInCatch = false;
        items[i].emblems.remove(items[i].emblems.kCatch);
        break;
      }
    }
  };

  this.clear = function(){
    _stock = [];
    _stockLength = 0;
  };
};
