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

    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 <dduserid.h>
#include <iostream>
#include <sys/time.h>
#include <time.h>
#include <stdio.h>
#include <ddusermanager.h>


using namespace std;


/**
	Standard constructor: Baut einen 'gast'
*/

dduserid::dduserid( ddusermanager* parent )
: _parent( parent), _useraccessrights( 1 ), _useritemperpage( DDDEFAULTITEMSPERPAGE ), 
	_lastaccess(0), _lastpost(0), _lastalert(0), 
	_hits( 0 ), _acceptterms( false ), _mailalert( true ),
	_lockcount(0)
{
	
	// create guest user
	_usr_id = "1";
	_usr_nick_name = DDGUESTNICK;

	_sessionkey = parent->create_session_key();
	
}


/**
	Standard constructor: Baut einen 'member'
*/

dduserid::dduserid( ddusermanager* parent, dddatastaterow& row )
: _parent( parent), _useraccessrights( 1 ), _useritemperpage( DDDEFAULTITEMSPERPAGE ), 
	_lastaccess(0), _lastpost(0), _lastalert(0), 
	_hits( 0 ), _acceptterms( false ), _mailalert( true ),
	_lockcount(0)
{

	// konfigurieren
	
	//select id id_user, sn_nickname, sn_cryptopass, sn_nickmail, sn_addressbook, 
	//sn_googlechannel1, sn_googlechannel2, sn_itemsperpage, sn_mailalert from tr_user
	
	set_access_rights( 2 );
	set_items_per_page( row["sn_itemsperpage"] );
	// angemeldete nutzer haben schon akzeptiert.
	_acceptterms = true;
	_mailalert = ( row["sn_mailalert"] ==  "1" ? true : false );

	// neu
	_usr_id = row["id_user"];
	_usr_nick_name = row["sn_nickname"];
	_usr_passwd = row["sn_cryptopass"];
 	_usr_nick_mail = row["sn_nickmail"];
	_usr_address_book = row["sn_addressbook"];   		
 
	_usr_google_channel1 = row["sn_googlechannel1"];
	_usr_google_channel2 = row["sn_googlechannel2"];
	
	_propertiesstring = row["sn_properties"];
	
	_sessionkey = parent->create_session_key();

}

// xx_prop_eins=true|xx_prop_zwo=false|xx_prop_drei=42
void dduserid::init_properties()
{
	ddstringrow row( _propertiesstring, "|" );
	for( size_t i=0; i<row.size(); ++i )	
	{
		ddstring key, value;
		row[i].split( "=", key, value );
		_properties[ key ] = value;
	}
	
}


void dduserid::save_properties()
{
	
	ddstring result;
	ddstringmap::const_iterator pos = _properties.begin();
	for( ; pos != _properties.end(); ++pos )
	{
		// alle props fangen mit DDUSERPROPERTY (= xx_ prop )an
		if( !(*pos).first.contains( DDUSERPROPERTY ) )
			continue;
		if( !result.empty() )
			result +=  "|";
		result +=  (*pos).first + "=" + (*pos).second;
	}
	_parent->usr_update_single( get_id(), "sn_properties", result, true );
	_propertiesstring = result;
	
}


/**
	'ungecheckte' checkboxen werden im cgi nicht übertragen,
	also müssen wir alle vorhandenen properties erstmal löschen		
*/

void dduserid::reset_properties()
{

  

	ddstringmap::iterator pos = _properties.begin();
	for( ; pos != _properties.end(); ++pos )
	{
		// alle props fangen mit DDUSERPROPERTY (= xx_ prop )an
		if( !(*pos).first.contains( DDUSERPROPERTY ) )
			continue;
	
		 (*pos).second = "0";
	}
	
}


/**
	Copy constructor
*/

/*
dduserid::dduserid( const dduserid& src )
: dddbobject()
{
	*this = src;
}

*/
/**
	Assignment operator.
*/
/*
dduserid& dduserid::operator=( const dduserid& src )
{
	if( this == &src )
		return *this;
	throw runtime_error("dduserid::operator=( const dduserid& src ): not done yet!");
	return *this;
}
*/

/**
	Destructor
*/

dduserid::~dduserid()
{
	//log_error( " --- UKILL: " +   get_id() + " : " + get_nick_name() );
}


/**
	@return Das Flag, ob die AGB schonmal akzeptiert
	worden. Ist bei members default true, weil die 
	das schon bei der Anmeldung durchhaben.
*/

bool dduserid::get_accepting_terms_flag() const
{
	// Sonderbarkeiten mitloggen
	if( !is_guest() && !_acceptterms )
		log_debug( "_acceptterms false für:" + get_nick_name() + ": " + get_id() );
	return _acceptterms;
}




/**	
	Set a new nickname. Works for guest users only.
*/
	
void dduserid::set_nick_name( const ddstring& newnick )
{
	
	int idx = get_user_manager().index_of( newnick.as_lower() );
	// user already exists
	if( idx >= 0 )
  	return;
	_usr_nick_name = newnick;
	
}


/**	
	Set a new nickmail. Works for guest users only.
*/
	
void dduserid::set_nick_mail( const ddstring& newnick )
{
	// root darf das
	//if( is_member() )
	//	throw runtime_error( "set_nick_mail: can't set new nickmail for members!" );
	//(*this)[dduserid::usr_nick_mail].assign( newnick );
	_usr_nick_mail = newnick;
	
}


/**
	@return Setzt das Mail-Alert-Flag
*/

bool dduserid::set_mail_alert_flag( bool mailalert )
{
	if( _mailalert != mailalert )
		get_user_manager().usr_update_single( get_id(), "sn_mailalert", mailalert ? "1" : "0" );
	_mailalert = mailalert;
	return _mailalert;
}


/**
	Create a string representation.
	@return a the session is (hex) ddstring
*/

ddstring dduserid::to_string() const
{
	return _hostip + ":" + get_nick_name() + " " + get_passwd() + " " + get_nick_mail() +  " access:" + ddstrutil::from_int(_useraccessrights) + " items:" + ddstrutil::from_int(_useritemperpage);
}


/**
	Write user data ( nickname, nickmail etc) into a form
	@param item the form
	@param user the user data object
*/

void dduserid::sync( ddwebcgi& cgi )
{
	//cgi.dump();
	//throw runtime_error( "dduserid::sync( ddwebcgi& cgi )" );
	/*
		// alt
 	size_t			_useraccessrights;
	int         _useritemperpage;
	ddstringmap	_userdata;
	
	time_t			_lastaccess;
	time_t			_lastpost;
	time_t			_lastalert;
	int 				_hits;		
	bool 				_acceptterms;
	bool				_mailalert;
	int 				_lockcount;
	ddstring		_hostip;
	ddstring		_sessionkey;
 	ddstring		_pagercount; 
	ddstring		_pagerinboxcache; 
	ddstring		_pageroutboxcache; 

	// neu
	ddstring				_usr_id;
	ddstring				_usr_nick_name;
	ddstring				_usr_passwd;
 	ddstring				_usr_nick_mail;
	ddstring				_usr_address_book;   		
 
	ddstring				_usr_google_channel1;
	ddstring				_usr_google_channel2;

	key: <id> value: <19748>
	key: <sx_action> value: <adminupdate>
	key: <id_group> value: <>
	key: <id_user> value: <19748>
	key: <sn_addressbook> value: <ake,Locke,Mickey>
	key: <sn_city> value: <>
	key: <sn_firstname> value: <Christoph>
	key: <sn_homepageimage> value: <>
	key: <sn_homepagelink> value: <>
	key: <sn_homepagetext> value: <code is poetry ;-) test>
	key: <sn_icqs> value: <0>
	key: <sn_lastname> value: <Holzheuer>
	key: <sn_msgbody> value: <>
	key: <sn_nickmail> value: <c.holzheuer@sourceworx.org>
	key: <sn_nickname> value: <cyc>
	key: <sx_action> value: <adminupdate>

	*/
	
	// davon bleiben dann nur 2 Felder übrig:
	_usr_nick_mail =  cgi["sn_nickmail"];
	_usr_address_book =  cgi["sn_addressbook"];
	
}



