#include <Fenster.h>
#include <Bildschirm.h>
#include <RenderThread.h>
#include <Welt2D.h>
#include <M2Datei.h>
#include <Model2D.h>
#include <DateiSystem.h>
#include <Textur2D.h>
#include <Kamera2D.h>
#include <iostream>
#include <Bild.h>

using namespace Framework;

namespace Framework
{
    void initFramework();
    void releaseFramework();
}

void FClose( void *p, void *zF );

Punkt p1;
Punkt p2;

int kamera2DTest()
{
    p1 = Punkt( -1, -1 );
    initFramework();

    Welt2D w2d;
    w2d.setAirResistance( 0.001 );
    w2d.setCircular( 1 );
    w2d.setSize( 5000, 5000 );
    w2d.setSize( 1 );
    Welt2D *w2 = &w2d;
    Kamera2D kam2d;
    Kamera2D *kam2 = &kam2d;

    WNDCLASS wndC = Framework::F_Normal( 0 );
    wndC.lpszClassName = "Kamera 2D Test";
    WFenster f;
    f.erstellen( WS_OVERLAPPEDWINDOW, wndC );
    f.setPosition( Punkt( 100, 100 ) );
    f.setSize( 900, 900 );
    f.setVSchließAktion( FClose );
    f.setMausAktion( [ w2, kam2 ]( void *p, void *o, MausEreignis me )
    {
        p2 = Punkt( me.mx, me.my );
        if( me.id == ME_RRechts )
        {
            Vertex wp = kam2->getWorldCoordinates( Punkt( me.mx, me.my ) );
            w2->explosion( wp, 100000, 10000 );
        }
        if( me.id == ME_RLinks )
        {
            if( p1 == Punkt( -1, -1 ) )
                p1 = Punkt( me.mx, me.my );
            else
            {
                w2->impuls( kam2->getWorldCoordinates( p1 ), kam2->getWorldDirection( Vertex( me.mx, me.my ) - (Vertex)p1 ) );
                p1 = Punkt( -1, -1 );
            }
        }
        return 1;
    } );
    f.setTastaturAktion( _ret1TE );
    f.setAnzeigeModus( 1 );

    Bildschirm2D b( f.getThis() );
    b.update();
    b.setTestRend( 0 );
    b.setFill( 0 );
    f.setBildschirm( b.getThis() );
    
    RenderTh rth;
    rth.setBildschirm( b.getThis() );
    rth.setRenderFunktion( []( void*a, void*rth, Bild*c )
    {
        c->fillRegion( 0, 0, c->getBreite(), c->getHeight(), 0xFF000000 );
        std::cout << "FPS: " << (1 / (( RenderTh*)rth)->getRenderTickZeit()) << " | Zeit pro Tick:" << ( (RenderTh*)rth )->getRenderTickZeit() << "\n";
        if( p1 != Punkt( -1, -1 ) )
            c->drawLinie( p1, p2, 0xFFFFFFFF );
    } );
    rth.beginn();

    M2Datei d;
    d.setPfad( "ship.m2" );
    d.leseDaten();
    Model2DData *m = d.ladeModel( "ship" );
    LTDBDatei td;
    td.setDatei( new Text( "ship.ltdb" ) );
    td.leseDaten( 0 );
    Bild *bt = td.laden( 0, new Text( "a.png" ) );
    Textur2D txt;
    txt.setTexturZ( bt );
    for( int i = 0; i < 100; i++ )
    {
        Model2DObject *mdl = new Model2DObject();
        mdl->setModel( m->getThis() );
        mdl->setPosition( (float)(rand() / 16 - RAND_MAX / 32), (float)(rand() / 16 - RAND_MAX / 32) );
        mdl->setSpeed( (float)rand() / 128 - (float)rand() / 128, (float)rand() / 128 - (float)rand() / 128 );
        mdl->setDrehungSpeed( (float)rand() / ( RAND_MAX / 2 ) - (float)rand() / ( RAND_MAX / 2 ) );
        mdl->setTextur( txt.getThis(), "ship" );
        w2d.addObject( mdl );
    }

    kam2d.setStyle( Kamera2D::Style::Sichtbar );
    kam2d.setSize( 900, 900 );
    kam2d.setWelt( w2d.getThis() );
    kam2d.setZoom( 0.3f );
    
    b.addMember( &kam2d );

    StartNachrichtenSchleife();

    rth.beenden();

    return 0;
}