#include "beforestart.h"
#include "ui_beforestart.h"
#include <Manager/settingsmanager.h>
#include <QPoint>


BeforeStart::BeforeStart(QWidget *mainWindow, QWidget *parent) :
    QWidget(parent),
    ui(new Ui::BeforeStart)
{
    ui->setupUi(this);
    this->mainwindow = mainWindow;
    initElements();
    nextScreenInfo(1);
}

BeforeStart::~BeforeStart()
{
    delete ui;
}

void BeforeStart::show(QSize size, QPoint position)
{
    int cw = (size.width() - this->width()) / 2;
    int ch = (size.height() - this->height()) / 2;
    move(position.x() + cw, position.y() + ch);

    QWidget::show();
}

void BeforeStart::on_nextBtn_clicked()
{
    int id = ui->pageRadios->id(ui->pageRadios->checkedButton());

    switch (id) {
    case 1:
    {
        ui->prevBtn->setDisabled(false);
        ui->secondPageRadio->setChecked(true);
        break;
    }
    case 2:
    {
        ui->thirdPageRadio->setChecked(true);
        break;
    }
    case 3:
    {
        ui->fourthPageRadio->setChecked(true);
        break;
    }
    case 4:
    {
        ui->nextBtn->setDisabled(true);
        break;
    }
    default:
        break;
    }

    nextScreenInfo(id + 1);
}

void BeforeStart::on_prevBtn_clicked()
{
    int id = ui->pageRadios->id(ui->pageRadios->checkedButton());

    switch (id) {
    case 1:
    {

        break;
    }
    case 2:
    {
        ui->prevBtn->setDisabled(true);
        ui->firstPageRadio->setChecked(true);
        break;
    }
    case 3:
    {
        ui->secondPageRadio->setChecked(true);
        break;
    }
    case 4:
    {
        ui->nextBtn->setDisabled(false);
        ui->thirdPageRadio->setChecked(true);
        break;
    }
    default:
        break;
    }

    setNormalState();
    nextScreenInfo(id - 1);
}

void BeforeStart::initElements()
{
    setAttribute(Qt::WA_DeleteOnClose);
    setWindowFlags(Qt::WindowCloseButtonHint);
    ui->pageRadios->setId(ui->firstPageRadio, 1);
    ui->pageRadios->setId(ui->secondPageRadio, 2);
    ui->pageRadios->setId(ui->thirdPageRadio, 3);
    ui->pageRadios->setId(ui->fourthPageRadio, 4);
    ui->startBtn->hide();
    ui->initTipsCheck->hide();
    QObject::connect(this, SIGNAL(startMainWindow()), mainwindow,SLOT(on_startBtn_clicked()));
    QObject::connect(this, SIGNAL(destroyed(QObject*)), mainwindow,SLOT(on_startBtn_clicked()));
}

void BeforeStart::setInfo(QString imagePath, QString text, QString leftImage, QString rightImage)
{    
    if(leftImage.isEmpty() && rightImage.isEmpty())
    {
        ui->imageCenterBtn->setIcon(QIcon(QPixmap(imagePath).scaled(200,200,Qt::KeepAspectRatio, Qt::SmoothTransformation)));
        ui->imageLeftBtn->setIcon(QIcon(QPixmap(leftImage)));
        ui->imageRightBtn->setIcon(QIcon(QPixmap(rightImage)));
    }
    else
    {
        ui->imageCenterBtn->setIcon(QIcon(QPixmap(imagePath).scaled(100,100,Qt::KeepAspectRatio, Qt::SmoothTransformation)));
        ui->imageLeftBtn->setIcon(QIcon(QPixmap(leftImage).scaled(100,100,Qt::KeepAspectRatio, Qt::SmoothTransformation)));
        ui->imageRightBtn->setIcon(QIcon(QPixmap(rightImage).scaled(100,100,Qt::KeepAspectRatio, Qt::SmoothTransformation)));
    }
    ui->textTxt->setText(text);
}

void BeforeStart::nextScreenInfo(int index)
{
    switch (index) {
    case 1:
    {
        setInfo(":/resources/icon/hplogo.png", tr("Te damos la bienvenida a HP Views Designer Assitant.\n Vamos a ver rápidamente como funciona el asistente"));
        break;
    }
    case 2:
    {
        setInfo(":/resources/icon/infoMenu.png",tr("Puedes crear Menús\nPantalas de información o de niveles de Tinta "),":/resources/icon/menu.png",":/resources/icon/ink.png");
        break;
    }
    case 3:
    {
        setInfo(":/resources/icon/edit.png", tr("Puedes editar las interfaces creadas"));
        break;
    }
    case 4:
    {
        setInfo(":/resources/icon/question.png", tr("Siempre que tengas alguna duda, utiliza la ayuda"));
        break;
    }
    case 5:
    {
        ui->imageCenterBtn->hide();
        ui->textTxt->hide();
        ui->startBtn->show();
        ui->initTipsCheck->show();
    }
    default:
        break;
    }
}

void BeforeStart::on_firstPageRadio_clicked()
{
    ui->prevBtn->setDisabled(true);
    ui->nextBtn->setDisabled(false);
    setNormalState();
    nextScreenInfo(1);
}

void BeforeStart::on_secondPageRadio_clicked()
{
    ui->prevBtn->setDisabled(false);
    ui->nextBtn->setDisabled(false);
    setNormalState();
    nextScreenInfo(2);
}

void BeforeStart::on_thirdPageRadio_clicked()
{
    ui->prevBtn->setDisabled(false);
    ui->nextBtn->setDisabled(false);
    setNormalState();
    nextScreenInfo(3);
}

void BeforeStart::on_startBtn_clicked()
{
    emit startMainWindow();
    this->close();
}

void BeforeStart::setNormalState()
{
    ui->imageCenterBtn->show();
    ui->textTxt->show();
    ui->startBtn->hide();
    ui->initTipsCheck->hide();
}

void BeforeStart::on_fourthPageRadio_clicked()
{
    ui->prevBtn->setDisabled(false);
    ui->nextBtn->setDisabled(false);
    setNormalState();
    nextScreenInfo(4);
}

void BeforeStart::on_initTipsCheck_clicked(bool checked)
{
    SettingsManager::saveShowInitTips(!checked);
}
