#include "testApp.h"



//--------------------------------------------------------------
void testApp::setup(){
	// listen on the given port
	#ifdef DEBUG
	std::cout << "listening for osc messages on port " << PORT << "\n";
	#endif
	receiver.setup( PORT );

	current_msg_string = 0;
    artlevel = 0.0f;
    font_counter = 0;

	ofBackground( 0, 0, 0 );

    detector[0].loadImage("detector.png");
    detector[1].loadImage("detector2.png");
    artFont.loadFont("PigiarniqBold.ttf",96);
}

//--------------------------------------------------------------
void testApp::update(){

    if(counter > 0) counter--;
    if(font_counter > 0) font_counter--;

	// hide old messages
	for ( int i=0; i<NUM_MSG_STRINGS; i++ )
	{
		if ( timers[i] < ofGetElapsedTimef() )
			msg_strings[i] = "";
	}

	// check for waiting messages
	while( receiver.hasWaitingMessages() )
	{
		// get the next message
		ofxOscMessage m;
		receiver.getNextMessage( &m );

		// check for mouse moved message
		if ( strcmp( m.getAddress(), "/artSense/intensity" ) == 0 )
		{
		    if(m.getArgType(0) == OFXOSC_TYPE_FLOAT)
                artlevel = m.getArgAsFloat( 0 );
            else if(m.getArgType(0) == OFXOSC_TYPE_INT32)
                artlevel = m.getArgAsInt32(0);
		}

		if ( strcmp( m.getAddress(), "/artSense/blip" ) == 0 )
		{
			counter = BEEP_DURATION;
			//beep = m.getArgAsInt32( 0 );
			beep = 1;

		}
#ifdef DEBUG
			// unrecognized message: display on the bottom of the screen
			char msg_string[16384];
			strcpy( msg_string, m.getAddress() );
			strcat( msg_string, ": " );
			for ( int i=0; i<m.getNumArgs(); i++ )
			{
				// get the argument type
				strcat( msg_string, m.getArgTypeName( i ) );
				strcat( msg_string, ":" );
				// display the argument - make sure we get the right type
				if( m.getArgType( i ) == OFXOSC_TYPE_INT32 )
					sprintf( msg_string, "%s%d ", msg_string, m.getArgAsInt32( i ) );
				else if( m.getArgType( i ) == OFXOSC_TYPE_FLOAT )
					sprintf( msg_string, "%s%f ", msg_string, m.getArgAsFloat( i ) );
				else if( m.getArgType( i ) == OFXOSC_TYPE_STRING )
					sprintf( msg_string, "%s\"%s\" ", msg_string, m.getArgAsString( i ) );
				else
					strcat( msg_string, "unknown" );


			// add to the list of strings to display
			msg_strings[current_msg_string] = msg_string;
			timers[current_msg_string] = ofGetElapsedTimef() + 5.0f;
			current_msg_string = ( current_msg_string + 1 ) % NUM_MSG_STRINGS;
			// clear the next line
			msg_strings[current_msg_string] = "";
		}
#endif

	}
}


//--------------------------------------------------------------
void testApp::draw(){
    ofSetColor(0xFFFFFF);

	#ifdef DEBUG
	char buf[256];

	sprintf( buf, "listening for osc messages on port %d", PORT );
	ofDrawBitmapString( buf, 10, 20 );

	// draw artlevel state
	sprintf( buf, "artlevel: % 4d ", artlevel );
	ofDrawBitmapString( buf, 430, 20 );

	for ( int i=0; i<NUM_MSG_STRINGS; i++ )
	{
		ofDrawBitmapString( (char*)msg_strings[i].c_str(), 10, 40+15*i );
	}
	#endif

    int x = (ofGetWidth()-detector[beep].width)/2;
    ofEnableAlphaBlending();
    detector[beep].draw(x, 100);
    ofDisableAlphaBlending();

    if(counter == 0)
        beep = 0;
    else
        beep = 1;


    if(artlevel > 0.8) {
        font_counter = 120;
        if(font_counter > 0) {
            ofSetColor(0x00FF00);
            artFont.drawString("GOOD", 50,ofGetHeight() - 50);

            ofSetColor(0x666666);
            artFont.drawString("ART", 1000,ofGetHeight() - 50);
        }
    }
    else if(artlevel > 0.4 && artlevel < 0.6)
    {
        font_counter = 120;
        if(font_counter > 0) {
            ofSetColor(0xFFFF00);
            artFont.drawString("MEDIOCRE", 50, ofGetHeight() - 50);

            ofSetColor(0x666666);
            artFont.drawString("ART", 1000,ofGetHeight() - 50);
        }
    }
    else if(artlevel < 0.2)
    {
        font_counter = 120;
        if(font_counter > 0) {
            ofSetColor(0xFF0000);
            artFont.drawString("CRAP", 50,ofGetHeight() - 50);

            ofSetColor(0x666666);
            artFont.drawString("ART", 1000,ofGetHeight() - 50);
        }
    }

}


//--------------------------------------------------------------
void testApp::keyPressed  (int key){

    if (key == 'a') {
        counter = BEEP_DURATION;
    }
}

//--------------------------------------------------------------
void testApp::mouseMoved(int x, int y ){

}

//--------------------------------------------------------------
void testApp::mouseDragged(int x, int y, int button){

}

//--------------------------------------------------------------
void testApp::mousePressed(int x, int y, int button){
}

//--------------------------------------------------------------
void testApp::mouseReleased(){
}
