#include "testApp.h"
#include "stdio.h"

//--------------------------------------------------------------
testApp::testApp()
{
    ofSetFrameRate(15);

    movie = new movieScrubber(0.75);

    printf("load font\n");
	franklinBook_big.loadFont("frabk.ttf",48);
	franklinBook.loadFont("frabk.ttf",16);

    srand((unsigned int)time((time_t *)NULL));

    // 0 output channels,
	// 2 input channels
	// 44100 samples per second
	// BUFFER_SIZE samples per buffer
	// 4 num buffers (latency)
	ofSoundStreamSetup(0,2,this, 44100,BUFFER_SIZE, 4);
	left = new float[BUFFER_SIZE];
	right = new float[BUFFER_SIZE];

	for (int i = 0; i < NUM_WINDOWS; i++)
	{
		for (int j = 0; j < BUFFER_SIZE/2; j++)
		{
			freq[i][j] = 0;
		}
	}

	 for (int i=0; i < 12; i++)
    {
        char fname[15];
        sprintf(fname, "veski%i.png",i+1);
        printf("images/%s\n",fname);
        //baby[i].loadImage(fname);
    }

    baby[0].loadImage("images/veski1.png");
    baby[1].loadImage("images/veski2.png");
    baby[2].loadImage("images/veski3.png");
    baby[3].loadImage("images/veski4.png");
    baby[4].loadImage("images/veski5.png");
    baby[5].loadImage("images/veski6.png");
    baby[6].loadImage("images/veski7.png");
    baby[7].loadImage("images/veski8.png");
    baby[8].loadImage("images/veski9.png");
    baby[9].loadImage("images/veski10.png");
    baby[10].loadImage("images/veski11.png");
    baby[11].loadImage("images/veski12.png");
    baby[12].loadImage("images/veski13.png");
    baby[13].loadImage("images/veski14.png");
    baby[14].loadImage("images/veski15.png");
    baby[15].loadImage("images/veski16.png");
    line.loadImage("images/line.png");
    frame = 1;
    maxframe = 11;
    minframe = 0;
}

//--------------------------------------------------------------
void testApp::setup()
{
	ofBackground(255, 0, 180);
    ofSetFrameRate(15);
    movie->update();
}

//--------------------------------------------------------------
void testApp::update()
{
    movie->update();
}

void testApp::exit()
{
    printf("exit\n");
    delete [] left;
    delete [] right;
    delete movie;
}

//--------------------------------------------------------------
void testApp::draw()
{
    static int index=0;
	float avg_power = 0.0f;
	float high_freq = 0.0f;
	static float intensity = 0.0f;
	static float soprano = 0.0f;

	if(index < 80)
		index += 1;
	else
		index = 0;

	/* do the FFT	*/
	myfft.powerSpectrum(0,(int)BUFFER_SIZE/2, left,BUFFER_SIZE,&magnitude[0],&phase[0],&power[0],&avg_power);

	/* start from 1 because mag[0] = DC component */
	/* and discard the upper half of the buffer */
	for(int j=1; j < BUFFER_SIZE/2; j++) {
		freq[index][j] = magnitude[j];
		if(j > 50) {
			high_freq += magnitude[j];
		}
	}

	/* draw the FFT */
	float new_intensity = (avg_power*100.0f);
	float new_soprano = (high_freq * 100.0f);

	if(new_intensity > (float) ofGetWidth()) {
		new_intensity = (float) ofGetWidth() - 300.0f;
		printf("new_intensity %f\n",new_intensity);
	}
	if(new_soprano > (float) ofGetWidth()) {
		new_soprano = (float) ofGetWidth() - 300.0f;
	}

	//	printf("avg %i\n",intensity);

	// let the smoothed calue sink to zero:
	intensity *= 0.9f;
	soprano *= 0.9f;

	// take the max, either the smoothed or the incoming:
	if (intensity < new_intensity) intensity = (new_intensity + intensity)/2;
	if (soprano < new_soprano) soprano = (new_soprano + soprano)/2;

    ofSetColor(0xf1e050);
	franklinBook_big.drawString("VESKI IS A BETTER MAN !?",30,60);

    ofSetColor(0x666666);
	ofRect(30.0f,450.0f,intensity/2,30.0f);
		ofSetColor(0x000000);
	franklinBook.drawString("Erectile Length",30,430);

    ofSetColor(0x666666);
	ofRect(30.0f,550.0f,soprano/2,30.0f);
		ofSetColor(0x000000);
	franklinBook.drawString("Flaccid Length", 30,530);

    movie->draw(780, 360, 640, 480);

    ofSetColor(0xFFFFFF);
	ofEnableAlphaBlending();
	baby[frame].draw(0,0);
	line.draw(50,0);
    ofDisableAlphaBlending();

    if(intensity > 400)  {
        maxframe = 15;
        minframe = 12;
    }
    else if(soprano > 400)  {
        maxframe = 11;
        minframe = 0;
    }
    else {
        maxframe = 0;
        minframe = 0;
    }

    frame++;

    if(frame > maxframe) {
        frame = minframe;
    }
}

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

    switch(key)
    {
        case ' ':
            movie->clearLoop();
            break;
    }
}

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

}

//--------------------------------------------------------------
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)
{
    //cout << "button: " << button << endl;
    switch(button)
    {
        case 0:
            movie->setLoopStart();
            break;
        case 2:
            movie->setLoopStop();
            break;
        case 1:
            movie->clearLoop();
            break;
    }
}

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

//--------------------------------------------------------------
void testApp::audioReceived(float * input, int bufferSize, int nChannels)
{

	// samples are "interleaved"
	for(int i = 0; i < bufferSize; i++)
	{
		left[i] = input[i*2];
		right[i] = input[i*2+1];
	}
	bufferCounter++;

}
