#include "informationscreen.h"
#include "ui_informationscreen.h"
#include <QPixmap>
#include <QColorDialog>
#include <QDir>
#include <Model/information.h>
#include <Controller/informationscreencontroller.h>
#include <Common/constants.h>


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

}

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

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

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

bool InformationScreen::getHasIcons()
{    
    return ui->iconPositiveOption->isChecked();
}

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

int InformationScreen::getNumberOfIcons()
{
    return ui->numberIconsBox->text().toInt();
}

int InformationScreen::getWidthView()
{
    return ui->widthInput->text().toInt();
}

int InformationScreen::getHeightView()
{
    return ui->heightInput->text().toInt();
}

QColor InformationScreen::getBackgroundColor()
{    
    return ui->colorBackgroundBtn->accessibleName();
}

QString InformationScreen::getBackgroundImage()
{
    return ui->imageBackgroundBtn->accessibleName();
}

void InformationScreen::initElements()
{
    ui->continueBtn->hide();
    ui->errorForm->hide();
    ui->infoForm->hide();
    ui->successForm->hide();
    hideCardSizeElements();
    hideSizeElements();
    setValidators();
    setDefaultIcons();
    setToolTips();

}

void InformationScreen::setDefaultIcons()
{
    setDefaultIcon(ui->imageBackgroundBtn, QString(":/resources/icon/imageOn.png"), QString(":/resources/icon/imageOff.png"));
    setDefaultIcon(ui->colorBackgroundBtn, QString(":/resources/icon/colorOn.png"), QString(":/resources/icon/colorOff.png"));
}

void InformationScreen::setDefaultIcon(QPushButton *button, QString iconPathOn, QString iconPathOff)
{
    button->setCheckable(true);

    QIcon *ico = new QIcon();
    ico->addPixmap(QPixmap(iconPathOn),QIcon::Normal,QIcon::On);
    ico->addPixmap(QPixmap(iconPathOff),QIcon::Normal,QIcon::Off);
    button->setIcon(*ico);

}

void InformationScreen::setValidators()
{
    setIntValidators(ui->numberIconsBox, 0, 100);
    setIntValidators(ui->rowInput, 0, 100);
    setIntValidators(ui->colInput, 0, 100);
    setIntValidators(ui->heightInput, 0, 10000);
    setIntValidators(ui->widthInput, 0, 10000);
    setIntValidators(ui->colInput_2, 0, 100);
    setIntValidators(ui->rowInput_2, 0, 100);
}

void InformationScreen::checkNumberOfElements(int elements)
{
    elements = View::checkNumberElements(elements);
    ui->numberIconsBox->setText(QString::number(elements));
}

void InformationScreen::setToolTips()
{
    ui->imageBackgroundBtn->setToolTip(tr("Establece una imagen como fondo de la interfaz"));
    ui->colorBackgroundBtn->setToolTip(tr("Permite seleccionar un color como fondo de la interfaz"));
}

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

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

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

void InformationScreen::isFullFill(){

    bool complete = !ui->heightInput->text().isEmpty() && ui->heightInput->text() != "0" &&
            !ui->widthInput->text().isEmpty() && ui->widthInput->text() != "0" &&
            (ui->imageBackgroundBtn->isChecked() || ui->colorBackgroundBtn->isChecked()) &&
            !ui->numberIconsBox->text().isEmpty() && ui->numberIconsBox->text() != "0" &&
            ui->positionCombo->currentIndex() != 0 &&
            ui->rowInput->text() != "0" && !ui->rowInput->text().isEmpty() &&
            ui->colInput->text() != "0" && !ui->colInput->text().isEmpty() &&
            ui->iconButtonGroup->checkedButton() != 0 &&
            ui->errorForm->isHidden() &&
            (ui->positionCombo_2->currentIndex() != 0 &&
            ui->rowInput_2->text().toInt() != 0 &&
            ui->colInput_2->text().toInt() != 0);

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

void InformationScreen::on_positionCombo_activated(int index)
{
    if(index != 0) showSizeElements();

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

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

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

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

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

    isFullFill();
}

void InformationScreen::on_continueBtn_clicked()
{
    ((InformationScreenController *)controller)->on_continueBtn_clicked();
}

void InformationScreen::on_imageBackgroundBtn_clicked(bool clicked)
{

    if(!clicked)
    {
        showInfo(tr("No ha seleccionado ningún fondo para la interfaz"));
        showSuccess("");
        isFullFill();
        return;
    }

    QString path = View::selectImage();
    if(path.isEmpty())
    {
        ui->imageBackgroundBtn->setChecked(false);
        showInfo(tr("No ha seleccionado ningún fondo para la interfaz"));
    }
    else
    {
        showSuccess(tr("Imagen: ") + path + tr(" cargada correctamente"));
        showInfo("");
        ui->imageBackgroundBtn->setAccessibleName(path);
        ((InformationScreenController *)controller)->imageBackground();
    }

    ui->colorBackgroundBtn->setChecked(false);
    isFullFill();

}

void InformationScreen::on_colorBackgroundBtn_clicked(bool clicked)
{

    if(!clicked)
    {
        showInfo(tr("No ha seleccionado ningún fondo para la interfaz"));
        showSuccess("");
        isFullFill();
        ui->colorBackgroundBtn->setChecked(false);
        return;
    }

    QColor color = QColorDialog::getColor(Qt::blue, this );
    if(!color.isValid())
    {
        showInfo(tr("No ha seleccionado ningún fondo para la interfaz"));
        showSuccess("");
        ui->colorBackgroundBtn->setChecked(false);
    }
    else
    {
        showSuccess(tr("Color de fondo establecido"));
        showInfo("");
        ui->colorBackgroundBtn->setAccessibleName(color.name());
        ((InformationScreenController *)controller)->colorBackground();
    }

    ui->imageBackgroundBtn->setChecked(false);
    isFullFill();
}

void InformationScreen::showError(QString error)
{
    if(error.isEmpty())
        ui->errorForm->hide();
    else
    {
        ui->errorForm->show();
        QFontMetrics metrics(ui->errorTxt->font());
        if(ui->errorTxt->width() < metrics.width(error))
        {
            ui->errorForm->setToolTip(error);
            error = ui->errorTxt->fontMetrics().elidedText(error, Qt::ElideMiddle, ui->errorTxt->width());
        }
        else
        {
            ui->errorForm->setToolTip("");
        }

        ui->errorTxt->setText(error);
    }
}

void InformationScreen::showInfo(QString info)
{
    if(info.isEmpty())
        ui->infoForm->hide();
    else
    {
        ui->infoForm->show();
        QFontMetrics metrics(ui->infoTxt->font());
        if(ui->infoTxt->width() < metrics.width(info))
        {
            ui->infoForm->setToolTip(info);
            info = ui->infoTxt->fontMetrics().elidedText(info, Qt::ElideMiddle, ui->infoTxt->width());
        }
        else
        {
            ui->infoForm->setToolTip("");
        }

        ui->infoTxt->setText(info);
    }
}

void InformationScreen::showSuccess(QString success)
{
    if(success.isEmpty())
        ui->successForm->hide();
    else
    {
        ui->successForm->show();
        QFontMetrics metrics(ui->successTxt->font());
        if(ui->successTxt->width() < metrics.width(success))
        {
            ui->successForm->setToolTip(success);
            success = ui->successTxt->fontMetrics().elidedText(success, Qt::ElideMiddle, ui->successTxt->width());
        }
        else
        {
            ui->successForm->setToolTip("");
        }

        ui->successTxt->setText(success);
    }
}

void InformationScreen::setEditModel(Interfaz *interfaz)
{    
    ui->heightInput->setText(QString::number(interfaz->getHeight()));
    ui->widthInput->setText(QString::number(interfaz->getWidth()));
    ui->numberIconsBox->setText(QString::number((interfaz->getNumberOfElements())));

    bool imageBackgorund = interfaz->getHasImageBackground();
    ui->imageBackgroundBtn->setChecked(imageBackgorund);
    ui->colorBackgroundBtn->setChecked(!imageBackgorund);

    bool hasIcons = ((Information*) interfaz)->getHasIcons();
    ui->iconPositiveOption->setChecked(hasIcons);
    ui->iconNegativeOption->setChecked(!hasIcons);

    int cols = interfaz->getNumberOfCols();
    int rows = interfaz->getNumberOfRows();

    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);
        ui->rowInput->setText(QString::number(rows));
        ui->colInput->setText(QString::number(cols));

    }

    int cardRows = ((Information *) interfaz)->getCardRows();
    int cardCols = ((Information *) interfaz)->getCardCols();
    if(cardRows == 1 && cardCols != 1)
    {
        changePositionCombo(3, cardRows, cardCols);
    }
    else if(cardCols == 1 && cardRows != 1)
    {
        changePositionCombo(2, cardRows, cardCols);
    }else
    {
        changePositionCombo(1, cardRows, cardCols);
    }

    ((InformationScreenController *)controller)->setModel(interfaz);
    isFullFill();
}

void InformationScreen::changePositionCombo(int pos, int row, int col)
{
    on_positionCombo_2_currentIndexChanged(pos);
    ui->positionCombo_2->setCurrentIndex(pos);
    ui->rowInput_2->setText(QString::number(row));
    ui->colInput_2->setText(QString::number(col));
}

int InformationScreen::getCardRows()
{
    return ui->rowInput_2->text().toInt();
}

int InformationScreen::getCardCols()
{
    return ui->colInput_2->text().toInt();
}

void InformationScreen::on_plusBtn_clicked()
{
    int numberOfIcons = ui->numberIconsBox->text().toInt();
    checkNumberOfElements(++numberOfIcons);
    ((InformationScreenController *)controller)->number_of_icons();
    ((InformationScreenController *)controller)->checkGeneralMenuSize();
    isFullFill();
}

void InformationScreen::on_lessBtn_clicked()
{
    int numberOfIcons = ui->numberIconsBox->text().toInt();
    checkNumberOfElements(--numberOfIcons);
    ((InformationScreenController *)controller)->number_of_icons();
    ((InformationScreenController *)controller)->checkGeneralMenuSize();
    isFullFill();
}

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

void InformationScreen::showSizeElements()
{
    ui->rowTxt->show();
    ui->rowInput->show();
    ui->colTxt->show();
    ui->colInput->show();
}

void InformationScreen::on_rowInput_textEdited()
{
    ((InformationScreenController *)controller)->checkGeneralMenuSize();
    isFullFill();

}

void InformationScreen::on_colInput_textEdited()
{
    ((InformationScreenController *)controller)->checkGeneralMenuSize();
    isFullFill();
}


void InformationScreen::on_iconPositiveOption_clicked()
{
    ((InformationScreenController *)controller)->on_icon_options();
    isFullFill();
}

void InformationScreen::on_iconNegativeOption_clicked()
{
    ((InformationScreenController *)controller)->on_icon_options();
    isFullFill();
}

void InformationScreen::on_positionCombo_2_currentIndexChanged(int index)
{
    QLineEdit *rows = ui->rowInput_2;
    QLineEdit *cols = ui->colInput_2;

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

    switch (index) {
    case MATRIX_MENU_OPTION:
    {
        showCardSizeElements();

        break;
    }
    case LIST_MENU_OPTION:
    {
        showCardSizeElements();
        cols->setText(QString::number(Constants::General::COLS_LIST));
        cols->setDisabled(true);
        break;
    }
    case LINE_MENU_OPTION:
    {
        showCardSizeElements();
        rows->setText(QString::number(Constants::General::ROWS_LINE));
        rows->setDisabled(true);
        break;
    }
    default:
        hideCardSizeElements();
        return;
    }

    isFullFill();
}

void InformationScreen::hideCardSizeElements()
{
    ui->rowTxt_2->hide();
    ui->rowInput_2->hide();
    ui->colTxt_2->hide();
    ui->colInput_2->hide();
}

void InformationScreen::showCardSizeElements()
{
    ui->rowTxt_2->show();
    ui->rowInput_2->show();
    ui->colTxt_2->show();
    ui->colInput_2->show();
}

void InformationScreen::on_rowInput_2_textEdited(const QString &arg1)
{
    ((InformationScreenController *)controller)->cardSize();
    isFullFill();
}

void InformationScreen::on_colInput_2_textEdited(const QString &arg1)
{
    ((InformationScreenController *)controller)->cardSize();
    isFullFill();
}

void InformationScreen::on_heightInput_textEdited(const QString &arg1)
{
    ((InformationScreenController *)controller)->heightView();
    isFullFill();
}

void InformationScreen::on_widthInput_textEdited(const QString &arg1)
{
    ((InformationScreenController *)controller)->widthView();
    isFullFill();
}

void InformationScreen::on_numberIconsBox_textEdited(const QString &arg1)
{
    ((InformationScreenController *)controller)->checkGeneralMenuSize();
    ((InformationScreenController *)controller)->number_of_icons();
    isFullFill();
}
