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

    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.

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


#ifndef DDUSERMANAGER_H
#define DDUSERMANAGER_H

#include <dddatatable.h>
#include <ddwebconnection.h>
#include <ddwebview.h>
#include <ddthreadmutex.h>
#include <ddusersessionmanager.h>
#include <ddmailmanager.h>

/// ddusermanager: Eine Datatable mit pointern auf die gültigen userids.
/**
	Achtung: der Index ist mit den nicknames bestückt, nicht mit den IDs
	
	Dummy-Neuimplementierung wg. Dauercrash
*/

typedef map<ddstring, ddstringmap> ddstringmapmap;

//class ddusermanager : public dddatatable< dduserid >
class ddusermanager : public ddbaseobject
{

//friend class ddwebsite;

public: 
	
	enum dd_login_error
	{
		usr_nickname_not_found=-2,
		usr_passwd_wrong=-3,		
    usr_mail_wrong=-4,
    usr_nickname_exists=-5,
		usr_nickname_invalid=-6
	};	
	
	ddusermanager();		
	~ddusermanager();
	
	void						init_data( ddconfig& config );
	
	// alt
	//size_t          create_result( MYSQL_RES* result );
	
	// neu
	//void 						add_entry( size_t num_fields, MYSQL_ROW row );
	

  dduserid*       get_session_id( ddwebconnection& connection, ddusersessionmanager& sessionmanager );
	//int				      prepare_login( ddwebconnection& connection, const ddstring& login, const ddstring& passwd );
 	int             check_login( const ddstring& nickname, const ddstring& passwd );

	void            send_pager_message( dduserid& sender, const ddstring& recnick, ddwebcgi& cgi, ddmailmanager& mm );
	void            send_admin_pager_message( dduserid& sender, ddwebcgi& cgi );
	
	const ddstring& get_nick_name( const ddstring& id );
	void            init_index();  
   
  void            usr_create( ddwebcgi& data );
  void            usr_change_nickname( const ddstring& oldname, const ddstring& newname );
  void  					usr_change_passwd( dduserid& user, const ddstring& passwd );
  void            usr_update( ddwebcgi& data );
  void            usr_update_single( const ddstring& id, const ddstring& field, const ddstring& value, bool is_string=false ); 
 	void            usr_remove( ddwebcgi& data );
	void            usr_block( ddwebcgi& data );
	void						usr_inc_score( const ddstring& receiverid, size_t points );
	  
	void						clear_pager();
	
	bool						check_nickname_valid( const ddstring& src );
	void  					create_auto_nickname( dduserid& user );
	
	ddstring  			create_session_key();
 
 
	// neu
	dduserid& operator[]( size_t index  )
	{
		return _mydata[ index ];
	}
	
	const dduserid& operator[]( size_t index  ) const
	{
		return _mydata[ index ];
	}

	dduserid& operator[]( const ddstring& key )
	{
		return _mydata[ key ];
	}

	const dduserid& operator[]( const ddstring& key ) const
	{
		return _mydata[ key ];
	}
	
	int index_of( const ddstring& key ) const
	{
		return _mydata.index_of( key );
	}
	
	int index_of_id( const ddstring& id ) const
	{  
		if( _mydata.empty() )
			return -1;
		int x = _mydata.size();
		for( int i=0; i < x; ++i )
		{
			// gefunden
			if( _mydata[i].get_id() == id )
				return i;
		}
		return -1;
	}
	
	inline int size() const
	{
		return _mydata.size();
	}
	
	
		
	ddstringmap& 		get_new_postings_count( dduserid& uid, const ddstring& dbname );

	
protected:

  bool 						prepare_login_data( ddwebconnection& connection, ddstring& key, ddstring& login, ddstring& passwd );
 
  ddstringmap		  _nickindex;
  
  ddthreadmutex		_loginlock; 	
	ddthreadmutex		_updatelock;
  ddthreadmutex		_pagerlock;
	
	//  neu 
	ddwebview						_mytable;
	ddmaprow<dduserid>	_mydata;
	
	ddstringmapmap	_postingcount;
	ddwebview				_newpostings;

private:

		ddusermanager( const ddusermanager& src )
		//: dddbtablebase<dduserid>()
		{
			(void) src;
		}

		ddusermanager& operator=( const ddusermanager& src )
		{
			return *this;
			(void) src;
		}
		
};


#endif


