#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 );

extern Punkt p1;
extern Punkt p2;
Model2DObject *obj;
Model2DObject *obj2;

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

    Welt2D w2d;
    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 );
        obj2->setDrehung( obj->getDrehung() );
        Vertex speed( 1, 0 );
        speed = speed.rotation( obj->getDrehung() );
        float faktor = -1;
        if( obj->getDrehung() > PI )
            faktor = -faktor;
        if( obj->getDrehung() < -PI )
            faktor = -faktor;
        obj2->addDrehung( faktor * speed.angle( Vertex( 1, 0 ) ) );
    } );

    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 );
    obj = new Model2DObject();
    obj->setModel( m->getThis() );
    obj->setTextur( txt.getThis(), "ship" );
    obj->setDrehungSpeed( -0.5 );
    w2d.addObject( obj );
    obj2 = new Model2DObject();
    obj2->setPosition( 200, 0 );
    obj2->setModel( m->getThis() );
    obj2->setTextur( txt.getThis(), "ship" );
    w2d.addObject( obj2 );

    rth.beginn();

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

    StartNachrichtenSchleife();

    rth.beenden();

    return 0;
}