#include "AccountStatistik.h"
#include "../../../Global/Initialisierung.h"
#include "../../../Global/Variablen.h"
#include <DateiSystem.h>

// Inhalt der AccountStatistik Klasse aus AccountStatistik.h
// Konstruktor
AccountStatistik::AccountStatistik( Schrift *zSchrift )
: Thread(),
  schrift( zSchrift->getThis() ),
  statistikF( initFenster( 810, 40, 780, 450, zSchrift, Fenster::Style::Sichtbar | Fenster::Style::Titel | Fenster::Style::TitelBuffered | Fenster::Style::Rahmen | Fenster::Style::Erlaubt, "Statistik von " ) ),
  sortSpalte( initAuswahlBox( 10, 10, 150, 20, zSchrift, ABSTYLE, { "Name", "Spiele", "Gewonnen", "Verloren", "Punkte", "BW0", "BW1", "BW2", "BW3", "BW4", "BW5" } ) ),
  sortRichtung( initAuswahlBox( 170, 10, 150, 20, zSchrift, ABSTYLE, { "Aufw�rts", "Abw�rts" } ) ),
  zur�ck( initKnopf( 590, 10, 20, 20, 0, 0, "" ) ),
  vor( initKnopf( 750, 10, 20, 20, 0, 0, "" ) ),
  seiteTF( initTextFeld( 620, 10, 120, 20, zSchrift, TextFeld::Style::Text | TextFeld::Style::Center, "Seite 0 von 0" ) ),
  tabelle( initObjTabelle( 10, 40, 760, 400, zSchrift, OTSTYLE, { { "Name", 108, 0, 0 }, { "Spiele", 70, 0, 0 }, { "Gewonnen", 70, 0, 0 },
  { "Verloren", 70, 0, 0 }, { "Punkte", 70, 0, 0 }, { "BW0", 60, 0, 0 }, { "BW1", 60, 0, 0 }, { "BW2", 60, 0, 0 }, { "BW3", 60, 0, 0 },
  { "BW4", 60, 0, 0 }, { "BW5", 60, 0, 0 } }, 20 ) ),
  laden( (Animation2D*)ladeAnimation->dublizieren() ),
  seite( 0 ),
  maxSeite( 0 ),
  status( 0 ),
  accId( 0 ),
  animation( 0 ),
  alpha( 255 ),
  tickVal( 0 ),
  rend( 0 )
{
	initToolTip( sortSpalte, "W�hle aus, nach welcher Spalte die\nTabelle sortiert werden soll.", zSchrift, hauptScreen );
	initToolTip( sortRichtung, "W�hle aus, ob Aufw�rts oder Abw�rts\nsortiert werden soll.", zSchrift, hauptScreen );
	Bild *zur�ckB = bilder->get( "data/client/bilder/shop.ltdb/zur�ck.png" );
	Bild *weiterB = bilder->get( "data/client/bilder/shop.ltdb/weiter.png" );
	laden->setSichtbar( 0 );
	laden->setPosition( 365, 200 );
	zur�ck->setStyle( Knopf::Style::Sichtbar | Knopf::Style::Hintergrund | Knopf::Style::HBild | Knopf::Style::KlickBuffer );
	zur�ck->setHintergrundBildZ( zur�ckB );
	initToolTip( zur�ck, "Eine Seite zur�ck bl�ttern.", zSchrift, hauptScreen );
	vor->setStyle( Knopf::Style::Sichtbar | Knopf::Style::Hintergrund | Knopf::Style::HBild | Knopf::Style::KlickBuffer );
	vor->setHintergrundBildZ( weiterB );
	initToolTip( vor, "Eine Seite weiter bl�ttern.", zSchrift, hauptScreen );
	statistikF->addMember( tabelle->getThis() );
	statistikF->addMember( zur�ck->getThis() );
	statistikF->addMember( seiteTF->getThis() );
	statistikF->addMember( vor->getThis() );
	statistikF->addMember( sortSpalte->getThis() );
	statistikF->addMember( sortRichtung->getThis() );
	statistikF->setMausEreignis( _ret1ME );
	vor->setMausEreignis( accountStatistikVorME );
	zur�ck->setMausEreignis( accountStatistikZur�ckME );
}

// Destruktor
AccountStatistik::~AccountStatistik()
{
	reset( 1 );
	statistikF->release();
	sortSpalte->release();
	sortRichtung->release();
	zur�ck->release();
	vor->release();
	seiteTF->release();
	tabelle->release();
	laden->release();
	schrift->release();
}

// nicht constant
void AccountStatistik::reset( bool �berschrift )
{
	statistikF->lockZeichnung();
	int zAnz = tabelle->getZeilenAnzahl();
	for( int i = !�berschrift; i < zAnz; i++ )
		tabelle->removeZeile( !�berschrift );
	statistikF->unlockZeichnung();
}

void AccountStatistik::ladeStatistik( int accId )
{
	if( this->accId == accId )
		return;
	this->status = 0;
	if( run )
	{
		warteAufThread( 1000 );
		ende();
	}
	if( ( animation | 0x1 ) == animation )
	{
		animation |= 0x4;
		this->accId = accId;
		this->status = 1;
		return;
	}
	this->accId = accId;
	start();
	this->status = 1;
}

void AccountStatistik::thread()
{
	laden->setSichtbar( 1 );
	reset();
	Text *name = infoClient->getSpielerName( accId );
	if( name )
	{
		name->insert( 0, "Statistik von " );
		statistikF->setTitel( *name );
		name->release();
	}
	RCArray< Array< int > > *values = new RCArray< Array< int > >();
	RCArray< Text > *namen = new RCArray< Text >();
	if( infoClient->getStatistikListe( accId, &seite, &maxSeite, sortSpalte->getAuswahl(), sortRichtung->getAuswahl(), values, namen ) )
	{
		statistikF->lockZeichnung();
		seiteTF->setText( "Seite " );
		seiteTF->zText()->append( seite );
		seiteTF->zText()->append( " von " );
		seiteTF->zText()->append( maxSeite );
		statistikF->unlockZeichnung();
		zur�ck->setStyle( Knopf::Style::Erlaubt, seite > 1 );
		vor->setStyle( Knopf::Style::Erlaubt, seite < maxSeite );
		int zAnz = values->getEintragAnzahl();
		if( zAnz )
		{
			int sAnz = values->z( 0 )->getEintragAnzahl();
			for( int i = 0; i < zAnz; i++ )
			{
				if( sAnz )
				{
					tabelle->addZeile( Text() += values->z( i )->get( 0 ) );
					Text *name = namen->z( i );
					if( name )
					{
						TextFeld *tf = initTextFeld( 0, 0, 0, 0, schrift, TextFeld::Style::Text | TextFeld::Style::Center, *name );
						tabelle->setZeichnungZ( 0, i + 1, tf );
					}
				}
				for( int j = 1; j < sAnz; j++ )
				{
					if( j < 1 )
					{
						TextFeld *tf = initTextFeld( 0, 0, 0, 0, schrift, TextFeld::Style::Text | TextFeld::Style::Center, "" );
						tf->zText()->append( values->z( i )->get( j ) );
						tabelle->setZeichnungZ( j, i + 1, tf );
					}
					if( j > 1 )
					{
						TextFeld *tf = initTextFeld( 0, 0, 0, 0, schrift, TextFeld::Style::Text | TextFeld::Style::Center, "" );
						tf->zText()->append( values->z( i )->get( j ) );
						tabelle->setZeichnungZ( j - 1, i + 1, tf );
					}
				}
			}
		}
	}
	else
		nachLogin->zNachrichtenListe()->addNachricht( new Text( "Fehler" ), new Text( "Die Statistiken dieses Spielers konnten nicht abgerufen werden." ), new Text( "Ok" ) );
	values->release();
	namen->release();
	laden->setSichtbar( 0 );
	animation &= ~0x4;
	status = 2;
	run = 0;
}

void AccountStatistik::setSichtbar( bool sichtbar, bool nachRechts )
{
	if( sichtbar )
	{
		if( ( animation | 0x1 ) != animation || ( ( nachRechts && ( animation | 0x2 ) != animation ) || !nachRechts && ( animation | 0x2 ) == animation ) )
		{
			if( nachRechts )
				statistikF->setPosition( -810, 40 );
			else
				statistikF->setPosition( 810, 40 );
		}
		animation |= 0x1;
	}
	else
		animation &= ~0x1;
	if( nachRechts )
		animation |= 0x2;
	else
		animation &= ~0x2;
}

bool AccountStatistik::vorME( MausEreignis &me )
{
	if( me.id == ME_RLinks )
	{
		seite++;
		start();
	}
	return 1;
}

bool AccountStatistik::zur�ckME( MausEreignis &me )
{
	if( me.id == ME_RLinks )
	{
		seite--;
		start();
	}
	return 1;
}

bool AccountStatistik::tick( double zeit )
{
	statistikF->lockZeichnung();
	rend |= statistikF->tick( zeit );
	statistikF->unlockZeichnung();
	rend |= laden->tick( zeit );
	tickVal += zeit;
	int valA = (int)( tickVal * 150 );
	int valB = (int)( tickVal * 500 );
	tickVal -= valA / 150.0;
	if( valA )
	{
		if( ( animation | 0x4 ) == animation && alpha )
		{
			if( alpha - valA <= 0 )
				alpha = 0;
			else
				alpha -= valA;
			rend = 1;
			if( !alpha )
			{
				reset();
				start();
			}
		}
		if( ( animation | 0x4 ) != animation && alpha != 255 )
		{
			if( alpha + valA >= 255 )
				alpha = 255;
			else
				alpha += valA;
			rend = 1;
		}
	}
	if( valB )
	{
		if( ( animation | 0x1 ) == animation )
		{ // Sichtbar
			if( ( animation | 0x2 ) == animation )
			{ // Nach Rechts
				if( statistikF->getX() != 10 )
				{
					if( statistikF->getX() + valB > 10 )
						statistikF->setPosition( 10, statistikF->getY() );
					else
						statistikF->setPosition( statistikF->getX() + valB, statistikF->getY() );
					rend = 1;
				}
			}
			else
			{ // Nach Links
				if( statistikF->getX() != 10 )
				{
					if( statistikF->getX() - valB < 10 )
						statistikF->setPosition( 10, statistikF->getY() );
					else
						statistikF->setPosition( statistikF->getX() - valB, statistikF->getY() );
					rend = 1;
				}
			}
		}
		else
		{ // Unsichtbar
			if( ( animation | 0x2 ) == animation )
			{ // Nach Rechts
				if( statistikF->getX() != 810 )
				{
					if( statistikF->getX() + valB > 810 )
						statistikF->setPosition( 810, statistikF->getY() );
					else
						statistikF->setPosition( statistikF->getX() + valB, statistikF->getY() );
					rend = 1;
				}
			}
			else
			{ // Nach Links
				if( statistikF->getX() != -810 )
				{
					if( statistikF->getX() - valB < -810 )
						statistikF->setPosition( -810, statistikF->getY() );
					else
						statistikF->setPosition( statistikF->getX() - valB, statistikF->getY() );
					rend = 1;
				}
			}
		}
	}
	bool ret = rend;
	rend = 0;
	return ret;
}

void AccountStatistik::doMausEreignis( MausEreignis &me )
{
	if( run )
		return;
	bool starten = 0;
	int sortSpalteS = sortSpalte->getAuswahl();
	int sortRichtungS = sortRichtung->getAuswahl();
	statistikF->lockZeichnung();
	statistikF->doMausEreignis( me );
	statistikF->unlockZeichnung();
	if( sortSpalte->getAuswahl() != sortSpalteS )
	{
		sortSpalte->einklappen();
		starten = 1;
	}
	if( sortRichtung->getAuswahl() != sortRichtungS )
	{
		sortRichtung->einklappen();
		starten = 1;
	}
	if( starten )
		start();
}

void AccountStatistik::render( Bild &zRObj )
{
	zRObj.setAlpha( alpha );
	statistikF->lockZeichnung();
	statistikF->render( zRObj );
	statistikF->unlockZeichnung();
	if( !zRObj.setDrawOptions( statistikF->getPosition(), statistikF->getSize() ) )
	{
		zRObj.releaseAlpha();
		return;
	}
	laden->render( zRObj );
	zRObj.releaseAlpha();
	zRObj.releaseDrawOptions();
}

// constant
int AccountStatistik::getStatus() const
{
	return status;
}


// Ereignisse
bool accountStatistikVorME( void *p, void *o, MausEreignis me )
{
	if( p )
		return ( (AccountStatistik*)p )->vorME( me );
	return 1;
}

bool accountStatistikZur�ckME( void *p, void *o, MausEreignis me )
{
	if( p )
		return ( (AccountStatistik*)p )->zur�ckME( me );
	return 1;
}