#include "menuscreen.h"
#include "ui_menuscreen.h"
#include <QColorDialog>
#include <QDir>
#include <QUrl>
#include <Common/constants.h>
#include <Controller/menuscreencontroller.h>
#include <Model/menu.h>
#include <QDebug>

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

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

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

void MenuScreen::finish()
{
    this->destroy();
}

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

}

void MenuScreen::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 MenuScreen::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 MenuScreen::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);
}

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

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

void MenuScreen::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 MenuScreen::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 MenuScreen::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);
    }
}

QString MenuScreen::getBackgroundImage()
{
    return ui->imageBackgroundBtn->accessibleName();
}
void MenuScreen::showSizeElements(){

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

}

void MenuScreen::isFullFill()
{
    bool complete = !ui->numberIconsBox->text().isEmpty() && ui->numberIconsBox->text() != "0" &&
            !ui->heightInput->text().isEmpty() && ui->heightInput->text() != "0" &&
            !ui->widthInput->text().isEmpty() && ui->widthInput->text() != "0" &&
            ui->positionCombo->currentIndex() != 0 &&
            !ui->rowInput->text().isEmpty() && ui->rowInput->text() != "0" &&
            !ui->colInput->text().isEmpty() && ui->colInput->text() != "0" &&
            ui->positionButtonGroup->checkedButton() != 0 &&
            ui->titleButtonGroup->checkedButton() != 0 &&
            ui->errorForm->isHidden() &&
            (ui->imageBackgroundBtn->isChecked() || ui->colorBackgroundBtn->isChecked());

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

void MenuScreen::on_titlePositiveOption_clicked()
{
    ((MenuScreenController *)controller)->on_title_options();
    isFullFill();
}

void MenuScreen::on_titleNegativeOption_clicked()
{
    ((MenuScreenController *)controller)->on_title_options();
    isFullFill();
}

void MenuScreen::on_continueBtn_clicked()
{       
    ((MenuScreenController *)controller)->on_continueBtn_clicked();
}

void MenuScreen::on_automaticPositionRadio_clicked()
{
    ((MenuScreenController *)controller)->automatic_icon_position();
    isFullFill();
}

void MenuScreen::on_customizePositionRadio_clicked()
{
    ((MenuScreenController *)controller)->automatic_icon_position();
    isFullFill();
}

void MenuScreen::on_rowInput_textEdited()
{
    ((MenuScreenController *)controller)->checkMenuSize();
    isFullFill();
}

void MenuScreen::on_colInput_textEdited()
{
    ((MenuScreenController *)controller)->checkMenuSize();
    isFullFill();
}

void MenuScreen::on_plusBtn_clicked()
{
    int numberOfIcons = ui->numberIconsBox->text().toInt();
    checkNumberIcons(++numberOfIcons);
    ((MenuScreenController *)controller)->number_of_icons();
    ((MenuScreenController *)controller)->checkMenuSize();
    isFullFill();
}

void MenuScreen::on_lessBtn_clicked()
{
    int numberOfIcons = ui->numberIconsBox->text().toInt();
    checkNumberIcons(--numberOfIcons);
    ((MenuScreenController *)controller)->number_of_icons();
    ((MenuScreenController *)controller)->checkMenuSize();
    isFullFill();
}

void MenuScreen::checkNumberIcons(int numberOfIcons)
{
    numberOfIcons = View::checkNumberElements(numberOfIcons);
    ui->numberIconsBox->setText(QString::number(numberOfIcons));
}

void MenuScreen::setToolTips()
{
    ui->imageBackgroundBtn->setToolTip(tr("Seleccionar imagen de fondo"));
    ui->colorBackgroundBtn->setToolTip(tr("Seleccionar color de fondo"));
}
void MenuScreen::on_heightInput_textEdited()
{
    ((MenuScreenController *)controller)->heightView();
    isFullFill();
}

void MenuScreen::on_widthInput_textEdited()
{ 
    ((MenuScreenController *)controller)->widthView();
    isFullFill();
}

bool MenuScreen::isAutomaticPosition()
{
    return ui->automaticPositionRadio->isChecked();
}

bool MenuScreen::isTitleOption()
{
    return ui->titlePositiveOption->isChecked();
}

void MenuScreen::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());
        ((MenuScreenController *)controller)->colorBackground();
    }

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

void MenuScreen::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);
        ((MenuScreenController *)controller)->imageBackground();
    }

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

QString MenuScreen::getBackgroundColor()
{
    return ui->colorBackgroundBtn->accessibleName();
}

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

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

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

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

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

void MenuScreen::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 = ((Menu *) interfaz)->getHasImageBackground();
    ui->imageBackgroundBtn->setChecked(imageBackgorund);
    ui->colorBackgroundBtn->setChecked(!imageBackgorund);

    bool automaticPosition = ((Menu *) interfaz)->isAutomaticPosition();
    ui->automaticPositionRadio->setChecked(automaticPosition);
    ui->customizePositionRadio->setChecked(!automaticPosition);

    bool hasTitle = ((Menu *) interfaz)->isTitleEnabled();
    ui->titlePositiveOption->setChecked(hasTitle);
    ui->titleNegativeOption->setChecked(!hasTitle);

    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));
    }

    ((MenuScreenController *)controller)->setModel(interfaz);

    isFullFill();
}

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

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

void MenuScreen::on_numberIconsBox_textEdited(const QString &arg1)
{
    ((MenuScreenController *)controller)->number_of_icons();
    ((MenuScreenController *)controller)->checkMenuSize();
    isFullFill();
}

void MenuScreen::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);
        ((MenuScreenController *)controller)->checkMenuSize();
        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);
        ((MenuScreenController *)controller)->checkMenuSize();
        break;
    }
    default:
        hideSizeElements();
        return;
    }

    isFullFill();
}
