#include "Dialog.h"
#include "Text.h"
#include "Fenster.h"
#include "Bildschirm.h"
#include "AsynchronCall.h"
#include "RenderThread.h"
#include "AuswahlBox.h"
#include "Schrift.h"
#include "Knopf.h"

using namespace Framework;

// Inhalt der MultiplChoiceDialog Klasse aus Dialog.h
MultiplChoiceDialog::MultiplChoiceDialog()
{
    entrys = new RCArray<Text>();
    ids = new Array<void*>();
    ref = 1;
}

MultiplChoiceDialog::~MultiplChoiceDialog()
{
    entrys->release();
    ids->release();
}

// F�gt eine Auswahlm�glichkeit hinzu
void MultiplChoiceDialog::addChoice( const char *text, void *id )
{
    entrys->add( new Text( text ) );
    ids->add( id );
}

// Zeigt den dialog an und wartet auf benutzereingabe
void *MultiplChoiceDialog::anzeigen( Schrift *zSchrift )
{
    void *result = 0;
    bool ex = 0;

    WNDCLASS wc = F_Normal( 0 );
    wc.lpszClassName = "Dialog";
    WFenster *f = new WFenster();
    f->setVSchlie�Aktion( [ &ex ]( void *p, void *o )
    {
        ex = true;
    } );
    f->setMausAktion( _ret1ME );
    f->setTastaturAktion( _ret1TE );
    f->erstellen( WS_OVERLAPPEDWINDOW, wc );
    f->setSize( 200, 200 );
    f->setPosition( Bildschirmmitte( f->getThis() ) );
    f->setVerschiebbar( 1 );
    f->setAnzeigeModus( 1 );
    Bildschirm *b = new Bildschirm2D( f->getThis() );
    f->setBildschirm( b->getThis() );
    b->update();

    RenderTh *r = new RenderTh();
    r->setBildschirm( b->getThis() );
    r->beginn();

    AuswahlBox *ab = new AuswahlBox();
    ab->setPosition( 10, 10 );
    ab->setSize( 180, 20 );
    ab->setHintergrundFarbe( 0xFF000000 );
    ab->setRahmenBreite( 1 );
    ab->setRahmenFarbe( 0xFFFFFFFF );
    ab->setMaxAuskappHeight( 120 );
    ab->setMausRahmenBreite( 1 );
    ab->setMausRahmenFarbe( 0xFF005500 );
    ab->setMausAlphaFeldFarbe( 0x00008700 );
    ab->setMausAlphaFeldStrength( -8 );
    ab->setAuswRahmenBreite( 1 );
    ab->setAuswRahmenFarbe( 0xFF00FF00 );
    ab->setAuswAlphaFeldFarbe( 0x0000FF00 );
    ab->setAuswAlphaFeldStrength( -8 );
    ab->setStyle( AuswahlBox::Style::Normal );
    ab->setSchriftZ( zSchrift->getThis() );
    for( auto i = entrys->getIterator(); i; i++ )
        ab->addEintrag( i->getText() );
    ab->setMausEreignis( _ret1ME );
    b->addMember( ab );

    Knopf *ok = new Knopf();
    ok->setStyle( Knopf::Style::Normal );
    ok->setPosition( 50, 150 );
    ok->setSize( 100, 20 );
    ok->setSchriftZ( zSchrift->getThis() );
    ok->setText( "Ok" );
    ok->setMausEreignis( [ this, &ex, &result, ab ]( void *p, void *o, MausEreignis me )
    {
        if( me.id == ME_RLinks )
        {
            result = ids->get( ab->getAuswahl() );
            ex = true;
        }
        return 1;
    } );
    b->addMember( ok );

    MSG msg;
    while( GetMessage( &msg, NULL, 0, 0 ) > 0 && !ex )
    {
        if( !ex )
        {
            TranslateMessage( &msg );
            DispatchMessage( &msg );
        }
    }

    r->beenden();
    r->release();

    b->release();
    f->setBildschirm( 0 );
    f->setAnzeigeModus( 0 );
    f->zerst�ren();
    f->release();

    ok->release();
    ab->release();

    return result;
}

// Reference Counting
MultiplChoiceDialog *MultiplChoiceDialog::getThis()
{
    ref++;
    return this;
}

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