/***************************************************************************

    supportware
    Copyright (C) 2001-2008 by c.holzheuer
    c.holzheuer@sourceworx.org

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

***************************************************************************/


#include <dditemfactory.h>
#include <dditemlink.h>
#include <dditemtree.h>
#include <dditemset.h>


/**
	Constructor. Erzeugt klonier-bare dummies für die Faktory. 
*/

dditemfactory::dditemfactory()
{
	_items.add_by_key( "dp_value",     				new dditem      		  ("(factory dummy)") );
	_items.add_by_key( "dp_call",      				new dditem      		  ("(factory dummy)") );
	_items.add_by_key( "dp_section",   				new dditemtree  	  	("(factory dummy)") );
	_items.add_by_key( "dp_link",      				new dditemlink  	  	("(factory dummy)") );	
	_items.add_by_key( "dp_item",      				new dditemtree  		  ("(factory dummy)") );
	_items.add_by_key( "dp_system",    				new dditemsysval			("(factory dummy)") ); 
	_items.add_by_key( "dp_translate", 				new dditemtrval 			("(factory dummy)") ); 
	_items.add_by_key( "dp_checkbox", 				new dditemcheckbox		("(factory dummy)") ); 
	_items.add_by_key( "dp_select", 					new dditemselect 			("(factory dummy)") );  
}


/**
	Destructor.
*/

dditemfactory::~dditemfactory()
{

}


/**
	@return Gibt eine Referenz auf den accessmanager zurück.
	@see ddaccessmanager
*/

ddaccessmanager& dditemfactory::get_access_manager()
{
	return _accessmanager;
}




/**
	Erzeugt ein item.
*/

dditem* dditemfactory::create_item( dditemset& itemset, ddstringrow& row, bool& is_section )
{
	// no type given?
	if( row.size()<2 )
		row.add( "dp_value" );
  ddstring file;
  size_t hash = row[1].find( '#' );
  if( hash != ddstring::npos )
  {
    file = row[1].substr( hash+1 );
		row[1] = row[1].substr( 0, hash );		
  }
	
	//dummy lookup
	int i = _items.index_of( row[1] );
	// not found, lookup parent
	if( i<0 )
		return 0;
	if( row[1] == "dp_section" )	
		is_section = true;
	
	dditem* item = _items[i]->clone();
	item->set_name( row[0] );

  // FIX!
	// hier müssen mehr parameter rein
	// besser: name:type( wert1, wert2 ... )

	// der dritte wert ist entweder die Callback-Funktion
	// oder ein default wert.

  // PFUSCH!
  
	if( row.size() > 2 )
  {
		if( row[1] == "dp_value" || row[1] == "dp_checkbox" )
			item->from_string( row[2] );
		else if( row[2] != "dp_access" )
			item->set_param( row[2] );
	}
  if( !file.empty() )
		itemset.create_from_extend( (dditemtree&) *item, file );

	return config_item( row, item );     
	
}


/**
	Setzt die Zugriffsrechte (admin only etc. ) für ein Item. 
*/

dditem* dditemfactory::config_item( ddstringrow& row, dditem* item )
{
	// setup access rights
	int xx = row.size();
	if( xx > 3 && row[xx-2] == "dp_access" )
		item->set_access( get_access_manager()[ row[xx-1] ] );
	return item;
}
	
	
