#include "Spieler.h"
#include "SSKlient.h"
#include <Text.h>

// Inhalt der Klient Klasse aus Klient.h
// Konstruktor
Klient::Klient( SSKlientV *klient )
{
	this->klient = klient;
	ref = 1;
}

// Destruktor
Klient::~Klient()
{
	if( klient )
		klient->release();
}

// nicht constant
void Klient::offline()
{
	klient = klient->release();
}

void Klient::online( SSKlientV *zKlient )
{
	if( klient )
		klient = klient->release();
	klient = zKlient->getThis();
}

void Klient::sendeInit( RCArray< Spieler > *zSpieler, int spielZeit )
{
	if( !klient )
		return;
	short len = (short)( 6 + zSpieler->getEintragAnzahl() * 8 );
	char *bytes = new char[ len ];
	*(int*)bytes = spielZeit;
	*(char*)( bytes + 4 ) = 6;
	*(char*)( bytes + 5 ) = (char)zSpieler->getEintragAnzahl();
	for( int i = 0; i < zSpieler->getEintragAnzahl(); i++ )
	{
		*(int*)( bytes + 6 + i * 8 ) = zSpieler->z( i )->getSpielerNummer();
		*(int*)( bytes + 10 + i * 8 ) = zSpieler->z( i )->getAccountId();
	}
	klient->spielNachricht( len, bytes );
	delete[] bytes;
}

void Klient::sendeSpielerNummer( int sNum, int spielZeit )
{
	if( !klient )
		return;
	short len = 9;
	char *bytes = new char[ len ];
	*(int*)bytes = spielZeit;
	*(char*)( bytes + 4 ) = 9;
	*(int*)( bytes + 5 ) = sNum;
	klient->spielNachricht( len, bytes );
	delete[] bytes;
}

void Klient::sendeStart( int spielZeit )
{
	if( !klient )
		return;
	short len = 5;
	char *bytes = new char[ len ];
	*(int*)bytes = spielZeit;
	*(char*)( bytes + 4 ) = 0xA;
	klient->spielNachricht( len, bytes );
	delete[] bytes;
}

void Klient::sendeTastaturStatus( int spielerId, TastaturStatus ts, bool aktiv, int spielZeit )
{
	if( !klient )
		return;
	short len = 9;
	char *bytes = new char[ len ];
	*(int*)bytes = spielZeit;
	*(char*)( bytes + 4 ) = (char)( (char)ts * 2 + (char)!aktiv );
	*(int*)( bytes + 5 ) = spielerId;
	klient->spielNachricht( len, bytes );
	delete[] bytes;
}

void Klient::sendeSkillNachricht( int sNum, char art, int spielZeit )
{
	if( !klient )
		return;
	short len = 10;
	char *bytes = new char[ len ];
	*(int*)bytes = spielZeit;
	*(char*)( bytes + 4 ) = 0xC;
	*(int*)( bytes + 5 ) = sNum;
	*(char*)( bytes + 9 ) = art;
	klient->spielNachricht( len, bytes );
	delete[] bytes;
}

void Klient::sendeSchuss( int id, int sNum, Vec2< double > pos, Vec2< double > speed, double intensity, int spielZeit )
{
	if( !klient )
		return;
	short len = 53;
	char *bytes = new char[ len ];
	*(int*)bytes = spielZeit;
	*(char*)( bytes + 4 ) = 0xD;
	*(int*)( bytes + 5 ) = id;
	*(int*)( bytes + 9 ) = sNum;
	*(double*)( bytes + 13 ) = pos.x;
	*(double*)( bytes + 21 ) = pos.y;
	*(double*)( bytes + 29 ) = speed.x;
	*(double*)( bytes + 37 ) = speed.y;
	*(double*)( bytes + 45 ) = intensity;
	klient->spielNachricht( len, bytes );
	delete[] bytes;
}

void Klient::sendeTreffer( int id, int sNum, int spielZeit )
{
	if( !klient )
		return;
	short len = 13;
	char *bytes = new char[ len ];
	*(int*)bytes = spielZeit;
	*(char*)( bytes + 4 ) = 0xE;
	*(int*)( bytes + 5 ) = id;
	*(int*)( bytes + 9 ) = sNum;
	klient->spielNachricht( len, bytes );
	delete[] bytes;
}

void Klient::sendeWiederbelebung( int sNum, int spielZeit )
{
	if( !klient )
		return;
	short len = 9;
	char *bytes = new char[ len ];
	*(int*)bytes = spielZeit;
	*(char*)( bytes + 4 ) = 0xF;
	*(int*)( bytes + 5 ) = sNum;
	klient->spielNachricht( len, bytes );
	delete[] bytes;
}

void Klient::sendeTod( int sNum, int killSNum, int spielZeit )
{
	if( !klient )
		return;
	short len = 13;
	char *bytes = new char[ len ];
	*(int*)bytes = spielZeit;
	*(char*)( bytes + 4 ) = 0x10;
	*(int*)( bytes + 5 ) = sNum;
	*(int*)( bytes + 9 ) = killSNum;
	klient->spielNachricht( len, bytes );
	delete[] bytes;
}

void Klient::sendeSpielEnde( char gewonnen, int spielZeit )
{
	if( !klient )
		return;
	short len = 6;
	char *bytes = new char[ len ];
	*(int*)bytes = spielZeit;
	*(char*)( bytes + 4 ) = 0xB;
	*(char*)( bytes + 5 ) = gewonnen;
	klient->spielNachricht( len, bytes );
	delete[] bytes;
}

void Klient::sendeChatNachricht( char *txt, int spielZeit )
{
	if( !klient )
		return;
	short len = (short)( 5 + textLength( txt ) );
	char *bytes = new char[ len ];
	*(int*)bytes = spielZeit;
	*(char*)( bytes + 4 ) = 0x8;
	for( int i = 5; i < len; i++ )
		bytes[ i ] = txt[ i - 5 ];
	klient->spielNachricht( len, bytes );
	delete[] bytes;
}

void Klient::sendeStatistikChatNachricht( int vonAccount, char *txt )
{
	if( !klient )
		return;
	short len = (short)( 5 + textLength( txt ) );
	char *bytes = new char[ len ];
	*(char*)( bytes ) = 3;
	*(int*)( bytes + 1 ) = vonAccount;
	for( int i = 5; i < len; i++ )
		bytes[ i ] = txt[ i - 5 ];
	klient->statistikNachricht( len, bytes );
	delete[] bytes;
}

void Klient::sendeStatistikSpielerOffline( int account )
{
	if( !klient )
		return;
	char *bytes = new char[ 5 ];
	*(char*)( bytes ) = 4;
	*(int*)( bytes + 1 ) = account;
	klient->statistikNachricht( 5, bytes );
	delete[] bytes;
}

void Klient::sendeSpielerStatistik( SpielerStatistik *zS )
{
	if( !zS || !klient )
		return;
	char snl = (char)zS->zSpielerName()->getLength();
	char tnl = (char)zS->zTeamName()->getLength();
	int len = 55 + snl + tnl;
	char *bytes = new char[ len ];
	bytes[ 0 ] = 0;
	*(int*)( bytes + 1 ) = zS->getSpielerNummer();
	*(char*)( bytes + 5 ) = (char)zS->zSpielerName()->getLength();
	for( int i = 0; i < snl; i++ )
		bytes[ i + 6 ] = zS->zSpielerName()->getText()[ i ];
	*(char*)( bytes + 6 + snl ) = tnl;
	for( int i = 0; i < tnl; i++ )
		bytes[ i + 7 + snl ] = zS->zTeamName()->getText()[ i ];
	*(int*)( bytes + 7 + snl + tnl ) = zS->getSpielerFarbe();
	*(int*)( bytes + 11 + snl + tnl ) = zS->getTeamFarbe();
	*(int*)( bytes + 15 + snl + tnl ) = zS->getSchadenBekommen();
	*(int*)( bytes + 19 + snl + tnl ) = zS->getSchadenGemacht();
	*(int*)( bytes + 23 + snl + tnl ) = zS->getTreibstoffVerbraucht();
	*(int*)( bytes + 27 + snl + tnl ) = zS->getShots();
	*(int*)( bytes + 31 + snl + tnl ) = zS->getTreffer();
	*(int*)( bytes + 35 + snl + tnl ) = zS->getPunkte();
	*(int*)( bytes + 39 + snl + tnl ) = zS->getKills();
	*(int*)( bytes + 43 + snl + tnl ) = zS->getTode();
	*(int*)( bytes + 47 + snl + tnl ) = zS->getZeitAmLeben();
	*(int*)( bytes + 51 + snl + tnl ) = zS->getZeitTod();
	klient->statistikNachricht( (short)len, bytes );
	delete[] bytes;
}

void Klient::sendeTeamStatistik( TeamStatistik *zS )
{
	if( !zS || !klient )
		return;
	char tnl = (char)zS->zTeamName()->getLength();
	int len = 43 + tnl;
	char *bytes = new char[ len ];
	bytes[ 0 ] = 1;
	*(int*)( bytes + 1 ) = zS->getTeamNummer();
	*(char*)( bytes + 5 ) = tnl;
	for( int i = 0; i < tnl; i++ )
		bytes[ i + 6 ] = zS->zTeamName()->getText()[ i ];
	*(int*)( bytes + 6 + tnl ) = zS->getTeamFarbe();
	*(int*)( bytes + 10 + tnl ) = zS->getSchadenBekommen();
	*(int*)( bytes + 14 + tnl ) = zS->getSchadenGemacht();
	*(int*)( bytes + 18 + tnl ) = zS->getTreibstoffVerbraucht();
	*(int*)( bytes + 22 + tnl ) = zS->getShots();
	*(int*)( bytes + 26 + tnl ) = zS->getTreffer();
	*(int*)( bytes + 30 + tnl ) = zS->getPunkte();
	*(int*)( bytes + 34 + tnl ) = zS->getKills();
	*(int*)( bytes + 38 + tnl ) = zS->getTode();
	*( bytes + 42 + tnl ) = (char)zS->hatGewonnen();
	klient->statistikNachricht( (short)len, bytes );
	delete[] bytes;
}

void Klient::sendeStatistikLadenFertig()
{
	if( !klient )
		return;
	char byte = 2;
	klient->statistikNachricht( 1, &byte );
}

// constant
bool Klient::istOnline() const
{
	return klient != 0;
}

// reference Counting
Klient *Klient::getThis()
{
	ref++;
	return this;
}

Klient *Klient::release()
{
	ref--;
	if( !ref )
		delete this;
	return 0;
}