#include "movieScrubber.h"

movieScrubber::movieScrubber(float start)
{
    movie.loadMovie("Viimane Vaatus - Anne Veski.avi");
	soundtrack.loadSound("Veski.wav");
	soundtrack.setVolume(0.75f);

    start_pos = start;
	loop_start = start;
	movie.setPosition(start);
	soundtrack.setPosition(start);
	movie.setLoopState(true);
	soundtrack.setLoop(true);
	loop_stop = 1;//movie.getDuration();
	loop = true;

	movie.play();
	soundtrack.play();
}

movieScrubber::~movieScrubber()
{
    //dtor
}

void movieScrubber::update()
{
    movie.idleMovie();
}

void movieScrubber::draw(int x, int y)
{
    draw(x, y, movie.width, movie.height);
}

void movieScrubber::draw(int x, int y, int w, int h)
{
    // looping
    if(loop && movie.getPosition() >= loop_stop)
    {
        movie.setPosition(loop_start);
        soundtrack.setPosition(loop_start);
    }

    // end of movie looping
    if(movie.getPosition() >= 1)//movie.getDuration())
    {
        if(loop)
        {
            movie.setPosition(loop_start);
            soundtrack.setPosition(loop_start);
        }
        else
        {
            movie.setPosition(start_pos);
            soundtrack.setPosition(start_pos);
        }
    }

    ofSetColor(0xFFFFFF);
    movie.draw(x, y, w, h);

//    ostringstream temp, temp2;
//    temp << "movie: " << movie.getPosition() << " "
//         << "soundtrack: " << soundtrack.getPosition();
//    ofDrawBitmapString(temp.str(), 100, 600);
//    temp2 << "start: " << loop_start << " "
//          << "stop: "  << loop_stop;
//    ofDrawBitmapString(temp2.str(), 100, 650);
}

void movieScrubber::setSpeed(float speed)
{
    movie.setSpeed(speed);
    soundtrack.setSpeed(speed);
}

void movieScrubber::setLoopStart()
{
    loop_start = movie.getPosition();
}

void movieScrubber::setLoopStop()
{
    loop_stop = movie.getPosition();
}

void movieScrubber::clearLoop()
{
    loop_start = start_pos;
	loop_stop = 1;//movie.getDuration();
}
