#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 = (SSKlientV *)klient->release();
}

void Klient::online( SSKlientV * zKlient )
{
    if( klient )
        klient = (SSKlientV *)klient->release();
    klient = (SSKlientV *)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::sendeUseSkillNachricht( int sNum, char id, int spielZeit )
{
    if( !klient )
        return;
    short len = 10;
    char *bytes = new char[ len ];
    *(int *)bytes = spielZeit;
    *(char *)( bytes + 4 ) = 0x15;
    *(int *)( bytes + 5 ) = sNum;
    *(char *)( bytes + 9 ) = id;
    klient->spielNachricht( len, bytes );
    delete[] bytes;
}

void Klient::sendeAsteroid( int id, Vertex pos, Vertex speed, float rot, float rotS, int index, int spielZeit )
{
    if( !klient )
        return;
    short len = 37;
    char *bytes = new char[ len ];
    *(int *)bytes = spielZeit;
    *(char *)( bytes + 4 ) = 0x11;
    *(int *)( bytes + 5 ) = id;
    *(float *)( bytes + 9 ) = pos.x;
    *(float *)( bytes + 13 ) = pos.y;
    *(float *)( bytes + 17 ) = speed.x;
    *(float *)( bytes + 21 ) = speed.y;
    *(float *)( bytes + 25 ) = rot;
    *(float *)( bytes + 29 ) = rotS;
    *(int *)( bytes + 33 ) = index;
    klient->spielNachricht( len, bytes );
    delete[] bytes;
}

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

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

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

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

void Klient::sendeObjektTreffer( int id, int oId, int spielZeit, float ep, int skillP )
{
    if( !klient )
        return;
    short len = 21;
    char *bytes = new char[ len ];
    *(int *)bytes = spielZeit;
    *(char *)( bytes + 4 ) = 0x16;
    *(int *)( bytes + 5 ) = id;
    *(int *)( bytes + 9 ) = oId;
    *(float *)( bytes + 13 ) = ep;
    *(int *)( bytes + 17 ) = skillP;
    klient->spielNachricht( len, bytes );
    delete[] bytes;
}

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

void Klient::sendeAsteroidTreffer( int asteroidId, int newAsteroidId, int schussId, Vertex pos, __int64 seed, int spielZeit, float ep, int skillP )
{
    if( !klient )
        return;
    short len = 41;
    char *bytes = new char[ len ];
    *(int *)bytes = spielZeit;
    *(char *)( bytes + 4 ) = 0x12;
    *(int *)( bytes + 5 ) = schussId;
    *(int *)( bytes + 9 ) = asteroidId;
    *(float *)( bytes + 13 ) = pos.x;
    *(float *)( bytes + 17 ) = pos.y;
    *(__int64 *)( bytes + 21 ) = seed;
    *(int *)( bytes + 29 ) = newAsteroidId;
    *(float *)( bytes + 33 ) = ep;
    *(int *)( bytes + 37 ) = skillP;
    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;
}