#include "autocookwindow.h"
#include "ui_autocookwindow.h"

AutoCookWindow::AutoCookWindow(QWidget *parent, Oven *oven, AbstractCook *cook) :
    QMainWindow(parent),
    ui(new Ui::AutoCookWindow),
    oven(oven),
    cook(cook),
    started(false),
    done(false),
    selectedStepIndex(0)
{
    ui->setupUi(this);

    ui->clockContainer->setParent(ui->upperStack);
    setAttribute(Qt::WA_DeleteOnClose);

    connect(oven, SIGNAL(changed(Oven*)), SLOT(onOvenUpdated()));
    oven->setDefault(Oven::CombinationMode);

    lastHumidity = oven->currentHumidity();
    lastTemp = oven->currentTemp();
    lastDoorView = Cook::Invalid;

    QTimer *cookStartTimer = new QTimer(this);
    cookStartTimer->setSingleShot(true);
    cookStartTimer->setInterval(3000);
    connect(cookStartTimer, SIGNAL(timeout()), SLOT(start()));

    connect(ui->configButton_1, SIGNAL(pressed()), SIGNAL(stopCookStartTimer()));
    connect(ui->configButton_2, SIGNAL(pressed()), SIGNAL(stopCookStartTimer()));
    connect(ui->configButton_3, SIGNAL(pressed()), SIGNAL(stopCookStartTimer()));
    connect(ui->configButton_4, SIGNAL(pressed()), SIGNAL(stopCookStartTimer()));
    connect(ui->configButton_5, SIGNAL(pressed()), SIGNAL(stopCookStartTimer()));
    connect(ui->configButton_1, SIGNAL(released()), SIGNAL(startCookStartTimer()));
    connect(ui->configButton_2, SIGNAL(released()), SIGNAL(startCookStartTimer()));
    connect(ui->configButton_3, SIGNAL(released()), SIGNAL(startCookStartTimer()));
    connect(ui->configButton_4, SIGNAL(released()), SIGNAL(startCookStartTimer()));
    connect(ui->configButton_5, SIGNAL(released()), SIGNAL(startCookStartTimer()));

    connect(ui->configSlider_1, SIGNAL(sliderPressed()), SIGNAL(stopCookStartTimer()));
    connect(ui->configSlider_2, SIGNAL(sliderPressed()), SIGNAL(stopCookStartTimer()));
    connect(ui->configSlider_3, SIGNAL(sliderPressed()), SIGNAL(stopCookStartTimer()));
    connect(ui->configSlider_4, SIGNAL(sliderPressed()), SIGNAL(stopCookStartTimer()));
    connect(ui->configSlider_5, SIGNAL(sliderPressed()), SIGNAL(stopCookStartTimer()));
    connect(ui->configSlider_1, SIGNAL(actionTriggered(int)), SIGNAL(startCookStartTimer()));
    connect(ui->configSlider_2, SIGNAL(actionTriggered(int)), SIGNAL(startCookStartTimer()));
    connect(ui->configSlider_3, SIGNAL(actionTriggered(int)), SIGNAL(startCookStartTimer()));
    connect(ui->configSlider_4, SIGNAL(actionTriggered(int)), SIGNAL(startCookStartTimer()));
    connect(ui->configSlider_5, SIGNAL(actionTriggered(int)), SIGNAL(startCookStartTimer()));

    connect(this, SIGNAL(stopCookStartTimer()), cookStartTimer, SLOT(stop()));
    connect(this, SIGNAL(startCookStartTimer()), cookStartTimer, SLOT(start()));

    connect(ui->configSlider_1, SIGNAL(valueChanged(int)), SLOT(onConfigChanged()));
    connect(ui->configSlider_2, SIGNAL(valueChanged(int)), SLOT(onConfigChanged()));
    connect(ui->configSlider_3, SIGNAL(valueChanged(int)), SLOT(onConfigChanged()));
    connect(ui->configSlider_4, SIGNAL(valueChanged(int)), SLOT(onConfigChanged()));
    connect(ui->configSlider_5, SIGNAL(valueChanged(int)), SLOT(onConfigChanged()));

    connect(ui->configSlider_1, SIGNAL(sliderMoved(int)), SLOT(updateView()));
    connect(ui->configSlider_2, SIGNAL(sliderMoved(int)), SLOT(updateView()));
    connect(ui->configSlider_3, SIGNAL(sliderMoved(int)), SLOT(updateView()));
    connect(ui->configSlider_4, SIGNAL(sliderMoved(int)), SLOT(updateView()));
    connect(ui->configSlider_5, SIGNAL(sliderMoved(int)), SLOT(updateView()));
    connect(ui->configSlider_1, SIGNAL(valueChanged(int)), SLOT(updateView()));
    connect(ui->configSlider_2, SIGNAL(valueChanged(int)), SLOT(updateView()));
    connect(ui->configSlider_3, SIGNAL(valueChanged(int)), SLOT(updateView()));
    connect(ui->configSlider_4, SIGNAL(valueChanged(int)), SLOT(updateView()));
    connect(ui->configSlider_5, SIGNAL(valueChanged(int)), SLOT(updateView()));


    checkCookTimer.setInterval(1000);
    connect(&checkCookTimer, SIGNAL(timeout()), SLOT(checkCook()));


    returnToCurrentStepTimer.setSingleShot(true);
    returnToCurrentStepTimer.setInterval(3000);
    connect(&returnToCurrentStepTimer, SIGNAL(timeout()), SLOT(returnToCurrentStep()));


    showingCurrentHumidity = false;
    showCurrentHumidityTimer.setSingleShot(true);
    showCurrentHumidityTimer.setInterval(3000);
    connect(&showCurrentHumidityTimer, SIGNAL(timeout()), SLOT(showCurrentHumidity()));

    showingCurrentTemp = false;
    showCurrentTempTimer.setSingleShot(true);
    showCurrentTempTimer.setInterval(3000);
    connect(&showCurrentTempTimer, SIGNAL(timeout()), SLOT(showCurrentTemp()));

    setupUi();

    cookStartTimer->start();
}

AutoCookWindow::~AutoCookWindow()
{
    delete ui;

    delete cook;
}

void AutoCookWindow::setupUi()
{
    bulletPixmap.load(":/images/symbol/step_bullet.png");
    selectedBulletPixmap.load(":/images/symbol/selected_step_bullet.png");
    steamModeIcon.load(":/images/cook_mode/small_steam.png");
    dryModeIcon.load(":/images/cook_mode/small_dryheat.png");
    combiModeIcon.load(":/images/cook_mode/small_combi.png");

    QString cookTypeIcon = Cook::icon(cook->type());
    ui->cookTypeIcon_1->setPixmap(cookTypeIcon);
    ui->cookTypeIcon_2->setPixmap(cookTypeIcon);

    QString name = cook->name();
    ui->selectCookButton_1->setText(name);
    ui->selectCookButton_2->setText(name);

    for (int idx = 0; idx < 5; idx++)
    {
        QPushButton *icon;
        QLabel *minLabel;
        QLabel *maxLabel;
        QLabel *currentLabel;
        QSlider *slider;

        switch (idx)
        {
        case 0:
            icon = ui->configButton_1;
            minLabel = ui->configMinLabel_1;
            maxLabel = ui->configMaxLabel_1;
            currentLabel = ui->configCurrentLabel_1;
            slider = ui->configSlider_1;
            break;
        case 1:
            icon = ui->configButton_2;
            minLabel = ui->configMinLabel_2;
            maxLabel = ui->configMaxLabel_2;
            currentLabel = ui->configCurrentLabel_2;
            slider = ui->configSlider_2;
            break;
        case 2:
            icon = ui->configButton_3;
            minLabel = ui->configMinLabel_3;
            maxLabel = ui->configMaxLabel_3;
            currentLabel = ui->configCurrentLabel_3;
            slider = ui->configSlider_3;
            break;
        case 3:
            icon = ui->configButton_4;
            minLabel = ui->configMinLabel_4;
            maxLabel = ui->configMaxLabel_4;
            currentLabel = ui->configCurrentLabel_4;
            slider = ui->configSlider_4;
            break;
        case 4:
            icon = ui->configButton_5;
            minLabel = ui->configMinLabel_5;
            maxLabel = ui->configMaxLabel_5;
            currentLabel = ui->configCurrentLabel_5;
            slider = ui->configSlider_5;
            break;
        }

        AbstractCookConfig *config = cook->configurations[idx];
        if (config != NULL)
        {
            icon->show();
            icon->setStyleSheet("QPushButton { image: url("
                                + config->icon()
                                + ") } QPushButton::pressed { image: url("
                                + config->overlayIcon()
                                + ") }");
            minLabel->show();
            minLabel->setText(config->minLabel());
            maxLabel->show();
            maxLabel->setText(config->maxLabel());

            currentLabel->show();

            slider->show();
            slider->blockSignals(true);
            slider->setMinimum(1);
            slider->setMaximum(config->count());
            slider->setValue(config->current());
            slider->blockSignals(false);

            switch (config->type())
            {
            case Cook::Time:
                slider->setProperty("sliderColor", QString("white"));
                break;
            case Cook::BurnDegree:
                slider->setProperty("sliderColor", QString("yellow"));
                break;
            case Cook::Brightness:
                slider->setProperty("sliderColor", QString("red"));
                break;
            default:
                slider->setProperty("sliderColor", QString("blue"));
                break;
            }

            slider->style()->unpolish(slider);
            slider->style()->polish(slider);
            slider->update();
        }
        else
        {
            icon->hide();
            minLabel->hide();
            maxLabel->hide();
            currentLabel->hide();
            slider->hide();
        }
    }

    if (cook->interTempEnabled())
    {
        ui->configButton_4->show();
        ui->configButton_4->setStyleSheet(
                    QString("QPushButton { image: url(")
                    + ":/images/slider_icon/core_temp_enabled.png"
                    + ") } QPushButton::pressed { image: url("
                    + ":/images/slider_icon/core_temp_ov.png"
                    + ") }"
                    );
        ui->configMinLabel_4->hide();
        ui->configMaxLabel_4->hide();
        ui->configCurrentLabel_4->hide();
        ui->configSlider_4->hide();

        interTempEnabled = true;
    }

    int offsetX = (900 - (cook->stepCount() * 19 * 2 - 19)) / 2;
    int offsetY = 1150;
    for (int idx = 0; idx < cook->stepCount(); idx++)
    {
        QLabel *bullet = new QLabel(ui->cookPage);
        bullet->setPixmap(bulletPixmap);
        bullet->setGeometry(offsetX + 19 * 2 * idx, offsetY, 19, 19);
        bullets.append(bullet);
    }

    ui->cookStepAnimation->start(300);

    ui->humidityGauge->setMinimum(0);
    ui->humidityGauge->setMaximum(100);
    ui->humidityGauge->setValue(0);

    ui->heatGauge->setMinimum(30);
    ui->heatGauge->setMaximum(300);
    ui->heatGauge->setValue(30);

    ui->openDoorAnimation->load(":/images/animation/door_01.png");
    ui->openDoorAnimation->load(":/images/animation/door_02.png");
    ui->openDoorAnimation->load(":/images/animation/door_03.png");
    ui->openDoorAnimation->load(":/images/animation/door_04.png");
    ui->openDoorAnimation->load(":/images/animation/door_05.png");
    ui->openDoorAnimation->load(":/images/animation/door_06.png");
    ui->openDoorAnimation->load(":/images/animation/door_07.png");
    ui->openDoorAnimation->load(":/images/animation/door_08.png");
    ui->openDoorAnimation->load(":/images/animation/door_09.png");
    ui->openDoorAnimation->start(300);
    ui->openDoorAnimation->hide();
    ui->openDoorArrow->hide();

    ui->closeDoorAnimation->load(":/images/animation/door_09.png");
    ui->closeDoorAnimation->load(":/images/animation/door_08.png");
    ui->closeDoorAnimation->load(":/images/animation/door_07.png");
    ui->closeDoorAnimation->load(":/images/animation/door_06.png");
    ui->closeDoorAnimation->load(":/images/animation/door_05.png");
    ui->closeDoorAnimation->load(":/images/animation/door_04.png");
    ui->closeDoorAnimation->load(":/images/animation/door_03.png");
    ui->closeDoorAnimation->load(":/images/animation/door_02.png");
    ui->closeDoorAnimation->load(":/images/animation/door_01.png");
    ui->closeDoorAnimation->start(300);
    ui->closeDoorAnimation->hide();
    ui->closeDoorArrow->hide();

    updateView();
}

void AutoCookWindow::updateView()
{
//    qDebug() << "updateView";
    if (started)
    {
        ui->stackedWidget->setCurrentIndex(1);

        for (int idx = 0; idx < bullets.length(); idx++)
        {
            QLabel *bullet = bullets.at(idx);
            if (idx == selectedStepIndex)
                bullet->setPixmap(selectedBulletPixmap);
            else
                bullet->setPixmap(bulletPixmap);
        }

        viewStep(cook->step(selectedStepIndex));

        int time = oven->time();
        if (time >= 3600)
            ui->timeLabel->setText(QString().sprintf("%d<span style=\"font-size:11pt;\">시간</span> %02d<span style=\"font-size:11pt;\">분</span>", time / 3600, (time % 3600) / 60));
        else if (time >= 60)
            ui->timeLabel->setText(QString().sprintf("%d<span style=\"font-size:11pt;\">분</span> %02d<span style=\"font-size:11pt;\">초</span>", time / 60, time % 60));
        else
            ui->timeLabel->setText(QString().sprintf("%d<span style=\"font-size:11pt;\">초</span>", time));

        int curInterTemp = oven->currentInterTemp();
        if (interTempEnabled)
        {
            int interTemp = oven->interTemp();
            ui->interTempLabel->setText(QString().sprintf("%d<span style=\"font-size:11pt;\">℃ / %d</span><span style=\"font-size:9pt;\">℃</span>", curInterTemp, interTemp));
        }
        else
            ui->interTempLabel->setText(QString().sprintf("%d<span style=\"font-size:11pt;\">℃</span>", curInterTemp));

        if (!done)
        {
            if (oven->door())
            {
                ui->closeDoorAnimation->show();
                ui->closeDoorArrow->show();
                ui->openDoorAnimation->hide();
                ui->openDoorArrow->hide();
            }
            else
            {
                ui->closeDoorAnimation->hide();
                ui->closeDoorArrow->hide();

                Cook::Step step = cook->currentStep();
                if (Cook::classify(step.type) == Cook::DoorClass)
                {
                    ui->openDoorAnimation->show();
                    ui->openDoorArrow->show();
                }
                else
                {
                    ui->openDoorAnimation->hide();
                    ui->openDoorArrow->hide();
                }
            }
        }
    }
    else
    {
        ui->stackedWidget->setCurrentIndex(0);

        for (int idx = 0; idx < 5; idx++)
        {
            AbstractCookConfig *config = cook->configurations[idx];
            if (config == NULL)
                continue;

            QLabel *l;
            QSlider *s;
            switch (idx)
            {
            case 0:
                l = ui->configCurrentLabel_1;
                s = ui->configSlider_1;
                break;
            case 1:
                l = ui->configCurrentLabel_2;
                s = ui->configSlider_2;
                break;
            case 2:
                l = ui->configCurrentLabel_3;
                s = ui->configSlider_3;
                break;
            case 3:
                l = ui->configCurrentLabel_4;
                s = ui->configSlider_4;
                break;
            case 4:
                l = ui->configCurrentLabel_5;
                s = ui->configSlider_5;
                break;
            }

            int time = cook->time();
            int interTemp = cook->interTemp();

            switch (config->type())
            {
            case Cook::Time:
                if (time >= 3600)
                    l->setText(QString().sprintf("%d<span style=\"font-size:11pt;\">시간</span> %02d<span style=\"font-size:11pt;\">분</span>", time / 3600, (time % 3600) / 60));
                else if (time >= 60)
                    l->setText(QString().sprintf("%d<span style=\"font-size:11pt;\">분</span> %02d<span style=\"font-size:11pt;\">초</span>", time / 60, time % 60));
                else
                    l->setText(QString().sprintf("%d<span style=\"font-size:11pt;\">초</span>", time));
                break;
            case Cook::BurnDegree:
                l->setText(QString().sprintf("%d<span style=\"font-size:11pt;\">℃</span>", interTemp));
                break;
            default:
                l->setText(QString().sprintf("%d<span style=\"font-size:11pt;\">/%d</span>", config->current(), config->count()));
                break;
            }
        }

        if (interTempEnabled)
        {
            ui->configButton_4->setStyleSheet(
                        QString("QPushButton { image: url(")
                        + ":/images/slider_icon/core_temp_enabled.png"
                        + ") } QPushButton::pressed { image: url("
                        + ":/images/slider_icon/core_temp_ov.png"
                        + ") }");
        }
        else
        {
            ui->configButton_4->setStyleSheet(
                        QString("QPushButton { image: url(")
                        + ":/images/slider_icon/core_temp.png"
                        + ") } QPushButton::pressed { image: url("
                        + ":/images/slider_icon/core_temp_ov.png"
                        + ") }");
        }
    }
}

void AutoCookWindow::viewStep(Cook::Step step)
{
//    qDebug() << "viewStep" << step.type;
    switch (Cook::classify(step.type))
    {
    case Cook::PreheatClass:
        viewPreheatStep(step);
        break;
    case Cook::DoorClass:
        viewDoorStep(step);
        break;
    case Cook::CookClass:
        viewCookStep(step);
        break;
    default:
        break;
    }
}

void AutoCookWindow::viewPreheatStep(Cook::Step step)
{
    ui->cookStepIcon->show();
    ui->cookStepIcon->setPixmap(QPixmap(":/images/cook_step_type/sys_icon_05.png"));
    ui->cookStepLabel->show();
    ui->cookStepLabel->setText("예열");
    ui->doorStepLabel->hide();
    ui->cookStepAnimation->hide();

    ui->humidityGauge->show();
    ui->humidityGauge->setValue(step.humidity);
    ui->humidityLabel->show();
    if (showingCurrentHumidity)
        ui->humidityLabel->setText(QString().sprintf("%d%%", oven->currentHumidity()));
    else
        ui->humidityLabel->setText(QString().sprintf("%d%%", step.humidity));

    ui->heatGauge->show();
    ui->heatGauge->setValue(step.temp);
    ui->heatLabel->show();
    if (showingCurrentTemp)
        ui->heatLabel->setText(QString().sprintf("%d℃", oven->currentTemp()));
    else
        ui->heatLabel->setText(QString().sprintf("%d℃", step.temp));

    ui->cookModeIcon->show();
    switch (step.mode)
    {
    case Cook::SteamMode:
        ui->cookModeIcon->setPixmap(steamModeIcon);
        break;
    case Cook::DryMode:
        ui->cookModeIcon->setPixmap(dryModeIcon);
        break;
    case Cook::CombiMode:
        ui->cookModeIcon->setPixmap(combiModeIcon);
        break;
    }
}

void AutoCookWindow::viewDoorStep(Cook::Step step)
{
//    qDebug() << "viewDoorStep";
    ui->cookStepLabel->hide();
    ui->cookStepIcon->hide();
    ui->doorStepLabel->show();
    ui->cookStepAnimation->show();

    ui->humidityGauge->hide();
    ui->humidityLabel->hide();
    ui->heatGauge->hide();
    ui->heatLabel->hide();
    ui->cookModeIcon->hide();

    if (lastDoorView != step.type)
    {
        lastDoorView = step.type;
        qDebug() << "clearStepAnimation";

        ui->cookStepAnimation->clear();
        switch (step.type)
        {
        case Cook::PutThermometer:
            ui->doorStepLabel->setText("중심 온도계 삽입");
            ui->cookStepAnimation->load(":/images/animation/thermometer_01.png");
            ui->cookStepAnimation->load(":/images/animation/thermometer_02.png");
            ui->cookStepAnimation->setGeometry((900-210)/2, 800, 210, 307);
            break;
        case Cook::Load:
            ui->doorStepLabel->setText("식재료 적재");
            ui->cookStepAnimation->load(":/images/animation/load_01.png");
            ui->cookStepAnimation->load(":/images/animation/load_02.png");
            ui->cookStepAnimation->load(":/images/animation/load_03.png");
            ui->cookStepAnimation->load(":/images/animation/load_04.png");
            ui->cookStepAnimation->load(":/images/animation/load_03.png");
            ui->cookStepAnimation->load(":/images/animation/load_02.png");
            ui->cookStepAnimation->load(":/images/animation/load_01.png");
            ui->cookStepAnimation->setGeometry((900-264)/2, 800, 264, 307);
            break;
        case Cook::Cut:
            ui->doorStepLabel->setText("자르기");
            ui->cookStepAnimation->load(":/images/animation/cut_01.png");
            ui->cookStepAnimation->load(":/images/animation/cut_02.png");
            ui->cookStepAnimation->load(":/images/animation/cut_03.png");
            ui->cookStepAnimation->setGeometry((900-264)/2, 800, 264, 307);
            break;
        case Cook::Pour:
            ui->doorStepLabel->setText("물 붓기");
            ui->cookStepAnimation->load(":/images/animation/pour_01.png");
            ui->cookStepAnimation->load(":/images/animation/pour_02.png");
            ui->cookStepAnimation->load(":/images/animation/pour_03.png");
            ui->cookStepAnimation->load(":/images/animation/pour_04.png");
            ui->cookStepAnimation->setGeometry((900-264)/2, 800, 264, 307);
            break;
        default:
            ui->doorStepLabel->hide();
        }
    }
}

void AutoCookWindow::viewCookStep(Cook::Step step)
{
    ui->cookStepLabel->show();
    ui->cookStepIcon->show();
    ui->doorStepLabel->hide();
    ui->cookStepAnimation->hide();

    ui->humidityGauge->show();
    ui->humidityGauge->setValue(step.humidity);
    ui->humidityLabel->show();
    if (showingCurrentHumidity)
        ui->humidityLabel->setText(QString().sprintf("%d%%", oven->currentHumidity()));
    else
        ui->humidityLabel->setText(QString().sprintf("%d%%", step.humidity));

    ui->heatGauge->show();
    ui->heatGauge->setValue(step.temp);
    ui->heatLabel->show();
    if (showingCurrentTemp)
        ui->heatLabel->setText(QString().sprintf("%d℃", oven->currentTemp()));
    else
        ui->heatLabel->setText(QString().sprintf("%d℃", step.temp));

    ui->cookModeIcon->show();
    switch (step.mode)
    {
    case Cook::SteamMode:
        ui->cookModeIcon->setPixmap(steamModeIcon);
        break;
    case Cook::DryMode:
        ui->cookModeIcon->setPixmap(dryModeIcon);
        break;
    case Cook::CombiMode:
        ui->cookModeIcon->setPixmap(combiModeIcon);
        break;
    }

    ui->cookStepLabel->setText(Cook::name(step.type));
    ui->cookStepIcon->setPixmap(QPixmap(Cook::icon(step.type)));
}

void AutoCookWindow::doPreheatStep(Cook::Step step)
{
//    oven->setHumidity(step.humidity);
    oven->setHumidity(0);
    oven->setTemp(step.temp);
    oven->startPreheating();

    lastHumidity = oven->currentHumidity();
    lastTemp = oven->currentTemp();

    ui->preheatIcon->show();
    ui->preheatLabel->show();
    ui->preheatGauge->show();
    ui->preheatGauge->setMaximum(step.temp);
    ui->preheatGauge->setMinimum(lastTemp);
    ui->preheatGauge->setValue(lastTemp);
}

void AutoCookWindow::doDoorStep(Cook::Step /*step*/)
{
    opened = false;
}

void AutoCookWindow::doCookStep(Cook::Step step)
{
//    oven->setHumidity(step.humidity);
    oven->setHumidity(0);
    oven->setTemp(step.temp);
    oven->startCooking();

    lastHumidity = oven->currentHumidity();
    lastTemp = oven->currentTemp();
}

static bool checkReached(int target, int last, int current)
{
    qDebug() << "checkReached" << target << last << current;
    return ((target >= last) && (target <= current))
            || ((target <= last) && (target >= current));
}

void AutoCookWindow::checkPreheatStep(Cook::Step step)
{
    int curTemp = oven->currentTemp();

    ui->preheatGauge->setValue(curTemp);

    if (checkReached(step.humidity, lastHumidity, oven->currentHumidity())
            && checkReached(step.temp, lastTemp, curTemp))
    {
        if (selectedStepIndex == cook->currentStepIndex())
            selectedStepIndex = cook->currentStepIndex() + 1;

        ui->preheatIcon->hide();
        ui->preheatLabel->hide();
        ui->preheatGauge->hide();

        cook->setCurrentStepIndex(cook->currentStepIndex() + 1);
        doStep();
    }
}

void AutoCookWindow::checkDoorStep(Cook::Step /*step*/)
{
//    qDebug() << "checkDoorStep" << opened << oven->door();
    if (opened)
    {
        if (!oven->door())
        {
            if (selectedStepIndex == cook->currentStepIndex())
                selectedStepIndex = cook->currentStepIndex() + 1;

            cook->setCurrentStepIndex(cook->currentStepIndex() + 1);
            doStep();
        }
    }
    else
    {
        if (oven->door())
            opened = true;
    }
}

void AutoCookWindow::checkCookStep(Cook::Step step)
{
    if (cook->currentStepIndex() + 1 < cook->stepCount())
    {
        if (checkReached(step.humidity, lastHumidity, oven->currentHumidity())
                && checkReached(step.temp, lastTemp, oven->currentTemp()))
        {
            if (selectedStepIndex == cook->currentStepIndex())
                selectedStepIndex = cook->currentStepIndex() + 1;

            cook->setCurrentStepIndex(cook->currentStepIndex() + 1);
            doStep();
        }
        else if (interTempEnabled)
        {
            if (cook->interTemp() <= oven->currentInterTemp())
            {
                if (selectedStepIndex == cook->currentStepIndex())
                    selectedStepIndex = cook->stepCount() - 1;

                cook->setCurrentStepIndex(cook->stepCount() - 1);
                doStep();
            }
        }
    }
    else /*if current step == last step*/
    {
        if (done)
        {

        }
        else
        {
            if ((interTempEnabled && cook->interTemp() <= oven->currentInterTemp())
                    || oven->time() == 0)
            {
                done = true;
                ui->openDoorAnimation->show();
                ui->openDoorArrow->show();
                ui->closeDoorAnimation->hide();
                ui->closeDoorArrow->hide();

                oven->setTime(0);
            }
        }
    }
}

void AutoCookWindow::start()
{
    qDebug() << "start";

    started = true;
    oven->setTime(cook->time());
    oven->setInterTemp(cook->interTemp());
    oven->setInterTempEnabled(interTempEnabled);

    doStep();
    checkCookTimer.start();

    updateView();
}

void AutoCookWindow::stop()
{
    qDebug() << "stop";
    oven->stop();
    checkCookTimer.stop();

    updateView();
}

void AutoCookWindow::doStep()
{
    qDebug() << "doStep";
    Cook::Step step = cook->currentStep();
    switch (Cook::classify(step.type))
    {
    case Cook::PreheatClass:
        doPreheatStep(step);
        break;
    case Cook::DoorClass:
        doDoorStep(step);
        break;
    case Cook::CookClass:
        doCookStep(step);
        break;
    default:
        break;
    }
}

void AutoCookWindow::checkCook()
{
//    qDebug() << "checkCook";
    Cook::Step step = cook->currentStep();
    switch (Cook::classify(step.type))
    {
    case Cook::PreheatClass:
        checkPreheatStep(step);
        break;
    case Cook::DoorClass:
        checkDoorStep(step);
        break;
    case Cook::CookClass:
        checkCookStep(step);
        break;
    default:
        qDebug() << "Checking Invalid Cook";
    }

    updateView();
}

void AutoCookWindow::onOvenUpdated()
{
    updateView();

    qDebug() << "onOvenUpdated()";
}

void AutoCookWindow::on_showPrevStepButton_clicked()
{
    if (selectedStepIndex > 0)
        selectedStepIndex--;

    updateView();

    returnToCurrentStepTimer.start();
}

void AutoCookWindow::on_showNextStepButton_clicked()
{
    if (selectedStepIndex + 1 < cook->stepCount())
        selectedStepIndex++;

    updateView();

    returnToCurrentStepTimer.start();
}

void AutoCookWindow::returnToCurrentStep()
{
    selectedStepIndex = cook->currentStepIndex();

    updateView();
}

void AutoCookWindow::onConfigChanged()
{
    for (int idx = 0; idx < 5; idx++)
    {
        AbstractCookConfig *config = cook->configurations[idx];
        if (config == NULL)
            continue;

        QSlider *slider;
        switch (idx)
        {
        case 0:
            slider = ui->configSlider_1;
            break;
        case 1:
            slider = ui->configSlider_2;
            break;
        case 2:
            slider = ui->configSlider_3;
            break;
        case 3:
            slider = ui->configSlider_4;
            break;
        case 4:
            slider = ui->configSlider_5;
            break;
        }

        config->setCurrent(slider->value());
    }
}

void AutoCookWindow::on_backButton_clicked()
{
    stop();
    close();
}

void AutoCookWindow::on_configButton_4_clicked()
{
    if (cook->interTempEnabled())
    {
        interTempEnabled = !interTempEnabled;

        updateView();
    }
}

void AutoCookWindow::on_humidityGaugeButton_pressed()
{
    showCurrentHumidityTimer.start();
}

void AutoCookWindow::on_humidityGaugeButton_released()
{
    showCurrentHumidityTimer.stop();

    if (showingCurrentHumidity)
    {
        showingCurrentHumidity = false;
        updateView();
    }
}

void AutoCookWindow::on_heatGaugeButton_pressed()
{
    showCurrentTempTimer.start();
}

void AutoCookWindow::on_heatGaugeButton_released()
{
    showCurrentTempTimer.stop();

    if (showingCurrentTemp)
    {
        showingCurrentTemp = false;
        updateView();
    }
}

void AutoCookWindow::showCurrentHumidity()
{
    showingCurrentHumidity = true;

    updateView();
}

void AutoCookWindow::showCurrentTemp()
{
    showingCurrentTemp = true;

    updateView();
}