#include "animhandler.h"

void AnimHandler::start()
{
    mode = RUN;
}
void AnimHandler::pause()
{
    mode = PAUSE;
}
void AnimHandler::stop()
{
    mode = STOP;
}
void AnimHandler::enableExit()
{
    exitThread = true;
}

void AnimHandler::run() {
    qDebug() << "STARTED BITCHES";
    while (!exitThread) {
        if (lastmode != mode)
        {
            switch (mode) {
            case STOP: emit onStoped(); break;
            case RUN: emit onStarted(); break;
            case PAUSE: emit onPaused(); break;
            }
        }
        lastmode = mode;
        switch (mode) {
        case STOP: break;
        case PAUSE: break;
        case RUN:
            if (anim.eof())
            {
                mode = STOP;
                break;
            }
            if (spisender.canSend())
            {
                SpiFrame spiframe(anim.nextFrame());
                spisender.sendFrame(spiframe);
            }
            break;
        }
        progress++;
        emit progressUpdate(progress);
        usleep(100);
    }
    qDebug() << "KÖRTE";
    emit resultReady("ALMA");
}

AnimHandler::AnimHandler() : QThread(), spisender(26)
{
}

void AnimHandler::openFile(const QString filepath) {
    anim.openFile(filepath);
}