#include "inkscreen.h"
#include "ui_inkscreen.h"
#include <QColorDialog>
#include <Controller/inkscreencontroller.h>
#include <Model/ink.h>
#include <QDir>
#include <Common/constants.h>

InkScreen::InkScreen(QWidget *parent) :
    View(parent),
    ui(new Ui::InkScreen)
{
    ui->setupUi(this);
    controller = new InkScreenController(this);
    initElements();
}

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

void InkScreen::isFullFill()
{
    bool complete = ui->numberElementInput->text().toInt() != 0 &&
            ui->colInput->text().toInt() != 0 &&
            ui->rowInput->text().toInt() != 0 &&
            ui->widthInput->text().toInt() != 0 &&
            ui->heightInput->text().toInt() != 0 &&
            ui->errorForm->isHidden() &&
            ui->porcentageButtonGroup->checkedButton() != 0;

    if(complete){
        ui->continueBtn->show();
    }else{
        ui->continueBtn->hide();
    }
}

void InkScreen::checkNumberElements(int elements)
{
    elements = View::checkNumberElements(elements);
    ui->numberElementInput->setText(QString::number(elements));
}

void InkScreen::hideMessages()
{
    ui->infoForm->hide();
    ui->errorForm->hide();
}

void InkScreen::clearInkButtons()
{
    QLayoutItem *item;
    while((item = ui->inkElementsLyt->takeAt(0)) != 0)
    {
        delete item->widget();
        delete item;
    }
}

void InkScreen::setToolTips()
{

}

void InkScreen::showInfo(QString info)
{
    ui->infoTxt->setText(info);
    ui->infoForm->show();
}

void InkScreen::addInkButtons(int numberOfElements, int rows, int cols, QVector<QColor> *colors)
{
    int pos = 0;
    for(int row = 0; row < rows && numberOfElements > 0; row ++)
        for(int col = 0; col <cols && numberOfElements > 0; col++)
        {
            QString BtnName = QString(tr("Seleccionar color %1x%2")).arg(row + 1).arg(col + 1);
            QPushButton *btn = new QPushButton(BtnName, this);
            btn->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
            if(!colors->isEmpty())
            {                
                btn->setStyleSheet(QString("background-color:%1").arg(colors->at(pos).name()));
                btn->setFixedHeight(40);
                btn->setFixedWidth(200);
            }
            connect(btn, SIGNAL(clicked()), this, SLOT(selectBtnColor()));
            ui->inkElementsLyt->addWidget(btn,row,col,Qt::AlignCenter);
            pos ++;
            numberOfElements --;
        }

    ui->inkElementsScroll->setWidgetResizable(true);
    ui->inkElementsScroll->show();
}

void InkScreen::clearElements()
{
    clearInkButtons();
    hideMessages();
    ui->inkElementsScroll->hide();
}

InkTextOption InkScreen::getTextOption()
{
    QAbstractButton *checkedOption = ui->porcentageButtonGroup->checkedButton();
    QString buttonName = checkedOption->text();

    if(buttonName == "Nada") return InkTextOption::Nothing;
    if(buttonName == "Porcentaje") return InkTextOption::Percentage;
    if(buttonName == "Número") return InkTextOption::Number;

    return InkTextOption::Nothing;

}

QColor InkScreen::getInkColor(int position)
{
    QColor color = QColorDialog::getColor(Qt::white, this );
    if(color.isValid())
    {
        QLayoutItem* item = ui->inkElementsLyt->itemAt(position);
        QWidget* widget = item->widget();
        QPushButton* button = dynamic_cast<QPushButton*>(widget);
        button->setStyleSheet(QString("background-color:%1").arg(color.name()));
        return color;
    }
    else
    {
        return Qt::white;
    }


}

void InkScreen::setEditModel(Interfaz *interfaz)
{
    ((InkScreenController *)controller)->setModel(interfaz);
    int numberOfElements = interfaz->getNumberOfElements();
    int cols = interfaz->getNumberOfCols();
    int rows = interfaz->getNumberOfRows();

    ui->heightInput->setText(QString::number(interfaz->getHeight()));
    ui->widthInput->setText(QString::number(interfaz->getWidth()));
    ui->numberElementInput->setText(QString::number(numberOfElements));
    ui->rowInput->setText(QString::number(rows));
    ui->colInput->setText(QString::number(cols));

    //addInkButtons(numberOfElements, rows, cols);
    if(cols == 1)
    {
        on_positionCombo_activated(2);
        ui->positionCombo->setCurrentIndex(2);
    }
    else if(rows == 1)
    {
        on_positionCombo_activated(3);
        ui->positionCombo->setCurrentIndex(3);
    }
    else
    {
        on_positionCombo_activated(1);
        ui->positionCombo->setCurrentIndex(1);

    }


    isFullFill();
}

void InkScreen::hideSizeElements()
{
    ui->rowTxt->hide();
    ui->rowInput->hide();
    ui->colTxt->hide();
    ui->colInput->hide();
}

void InkScreen::on_positionCombo_activated(int index)
{

    if(index != 0) showSizeElements();

    QLineEdit *rows = ui->rowInput;
    QLineEdit *cols = ui->colInput;

    rows->setEnabled(true);
    cols->setEnabled(true);

    switch (index) {
    case MATRIX_MENU_OPTION:
    {
        ((InkScreenController *)controller)->checkMenuSize();
        break;
    }
    case LIST_MENU_OPTION:
    {
        cols->setText(QString::number(Constants::General::COLS_LIST));
        if(ui->numberElementInput->text().toInt() != 0)
            rows->setText(ui->numberElementInput->text());

        cols->setDisabled(true);
        rows->setDisabled(true);
        ((InkScreenController *)controller)->checkMenuSize();
        break;
    }
    case LINE_MENU_OPTION:
    {     
        rows->setText(QString::number(Constants::General::ROWS_LINE));
        if(ui->numberElementInput->text().toInt() != 0)
            cols->setText(ui->numberElementInput->text());

        rows->setDisabled(true);
        cols->setDisabled(true);
        ((InkScreenController *)controller)->checkMenuSize();
        break;
    }
    default:
        hideSizeElements();
        return;
    }


    isFullFill();

}

void InkScreen::showSizeElements(){

    ui->rowTxt->show();
    ui->rowInput->show();
    ui->colTxt->show();
    ui->colInput->show();

}

void InkScreen::on_homeBtn_clicked()
{
    controller->on_homeBtn_clicked();
}

void InkScreen::on_settingsBtn_clicked()
{
    controller->on_settingsBtn_clicked();
}

void InkScreen::on_helpBtn_clicked()
{
    QString url = getRootHelpPath().append("/VDA-Help/interfaz-ink.html");
    controller->on_helpBtn_clicked(url, currentPosition);
}

void InkScreen::showError(QString error)
{
    ui->errorTxt->setText(error);
    ui->errorForm->show();
}

void InkScreen::showSuccess(QString success)
{

}

int InkScreen::getNumberOfElements()
{
    return ui->numberElementInput->text().toInt();
}

int InkScreen::getNumberOfCols()
{
    return ui->colInput->text().toInt();
}

int InkScreen::getNumberOfRows()
{
    return ui->rowInput->text().toInt();
}

int InkScreen::getWidth()
{
    return ui->widthInput->text().toInt();
}

int InkScreen::getHeight()
{
    return ui->heightInput->text().toInt();
}

void InkScreen::initElements()
{    
    ui->continueBtn->hide();
    ui->inkElementsScroll->hide();
    hideMessages();
    hideSizeElements();
    setValidators();
}

void InkScreen::setValidators()
{
    setIntValidators(ui->numberElementInput, Constants::General::MIN_NUM_ELEMENTS,Constants::General::MAX_NUM_ELEMENTS);
    setIntValidators(ui->rowInput, Constants::General::MIN_NUM_ELEMENTS, Constants::General::MAX_NUM_ELEMENTS);
    setIntValidators(ui->colInput, Constants::General::MIN_NUM_ELEMENTS, Constants::General::MAX_NUM_ELEMENTS);
    setIntValidators(ui->widthInput, Constants::General::MIN_SCREEN_WIDTH, Constants::General::MAX_SCREEN_WIDTH);
    setIntValidators(ui->heightInput, Constants::General::MIN_SCREEN_HEIGHT, Constants::General::MAX_SCREEN_HEIGHT);
}

void InkScreen::on_plusBtn_clicked()
{
    int numberOfIcons = ui->numberElementInput->text().toInt();
    checkNumberElements(++numberOfIcons);
    ((InkScreenController *)controller)->checkMenuSize();
    isFullFill();
}

void InkScreen::on_lessBtn_clicked()
{
    int numberOfIcons = ui->numberElementInput->text().toInt();
    checkNumberElements(--numberOfIcons);
    ((InkScreenController *)controller)->checkMenuSize();
    isFullFill();
}

void InkScreen::on_rowInput_textEdited()
{
    ((InkScreenController *)controller)->checkMenuSize();
    isFullFill();
}

void InkScreen::on_colInput_textEdited()
{
    ((InkScreenController *)controller)->checkMenuSize();
    isFullFill();
}

void InkScreen::on_numberElementInput_textEdited()
{
    ((InkScreenController *)controller)->checkMenuSize();
    isFullFill();
}

void InkScreen::selectBtnColor()
{
    QPushButton* buttonSender = qobject_cast<QPushButton*>(sender());
    int btnPosition = ui->inkElementsLyt->indexOf(buttonSender);
    ((InkScreenController *)controller)->inkColor(btnPosition);
}

void InkScreen::on_porcentageRadio_clicked()
{
    ((InkScreenController *)controller)->textProperty();
    isFullFill();
}

void InkScreen::on_numberRadio_clicked()
{
    ((InkScreenController *)controller)->textProperty();
    isFullFill();
}

void InkScreen::on_noneRadio_clicked()
{
    ((InkScreenController *)controller)->textProperty();
    isFullFill();
}

void InkScreen::on_continueBtn_clicked()
{
    ((InkScreenController *)controller)->finish();
}

void InkScreen::on_heightInput_textEdited()
{
    ((InkScreenController *)controller)->heightView();
    isFullFill();
}

void InkScreen::on_widthInput_textEdited()
{
    ((InkScreenController *)controller)->widthView();
    isFullFill();
}
