#include "menuiconoptions.h"
#include "ui_menuiconoptions.h"
#include "mainwindow.h"
#include "settings.h"
#include <QMessageBox>
#include <QDir>
#include <QUrl>
#include <QPropertyAnimation>
#include <Controller/menuscreencontroller.h>
#include <Common/constants.h>

MenuIconOptions::MenuIconOptions(QWidget *parent) :
    View(parent),
    ui(new Ui::MenuIconOptions)
{
    ui->setupUi(this);
    initElements();    
}

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

void MenuIconOptions::setController(MenuScreenController *controller)
{    
    this->controller = controller;
    connect(ui->hpIconBtn,SIGNAL(backExit()),((MenuScreenController *)this->controller),SLOT(backExit()));
}

void MenuIconOptions::initElements()
{    
    setToolTips();
    setValidators();
    ui->continueBtn->hide();
    ui->positionGroup->hide();
    ui->errorForm->hide();
}

void MenuIconOptions::on_browseBtn_clicked()
{        
    QString path = View::selectImage();

    setIcon(path);
    ui->positionGroup->show();
    ((MenuScreenController *)controller)->iconImage();


    isFullFill();
}

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

void MenuIconOptions::setProgress(int progress)
{
    QPropertyAnimation *a = new QPropertyAnimation(ui->progressBar,"value");
    a->setDuration(500);
    a->setStartValue(ui->progressBar->value());
    a->setEndValue(progress);
    a->setEasingCurve(QEasingCurve::InOutCubic);
    a->start(QPropertyAnimation::KeepWhenStopped);
}

void MenuIconOptions::setCurrentIcon(int iconRow,int iconCol)
{
    QString currentIcon = QString("Icono %1 x %2").arg(iconRow).arg(iconCol);
    ui->tittle->setText(currentIcon);
}

void MenuIconOptions::setEnabledOptions(bool hasTitle, bool isAutomatic)
{
    if(!hasTitle)
    {
        ui->titleOptionTxt->hide();
        ui->titleInput->hide();
        ui->secondLine->hide();
    }

    if(isAutomatic)
    {
        ui->TopPosition->hide();
        ui->LeftPosition->hide();
        ui->RightPosition->hide();
        ui->BottomPosition->hide();
    }

    isFullFill();

}

void MenuIconOptions::on_titleInput_textChanged()
{    
    isFullFill();
}

QString MenuIconOptions::getIconTitle(){
    return ui->titleInput->text();
}

void MenuIconOptions::isFullFill()
{
    bool complete =(ui->titleInput->isHidden() || !ui->titleInput->text().isEmpty()) &&
            (ui->TopPosition->isHidden() || (!ui->topMarginInput->text().isEmpty() &&
                                             !ui->bottomMarginInput->text().isEmpty() && !ui->leftMarginInput->text().isEmpty() &&
                                             !ui->rigthMarginInput->text().isEmpty())) &&
                                              ui->errorForm->isHidden() && !ui->positionGroup->isHidden();

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

void MenuIconOptions::setToolTips()
{
    ui->browseBtn->setToolTip(tr("Seleccionar icono para el elemento actual"));
}

void MenuIconOptions::on_continueBtn_clicked()
{     
   ((MenuScreenController *)controller)->nextIcon();
   isFullFill();
}

void MenuIconOptions::clear()
{
    ui->positionGroup->hide();
    ui->titleInput->clear();
    ui->topMarginInput->clear();
    ui->leftMarginInput->clear();
    ui->rigthMarginInput->clear();
    ui->bottomMarginInput->clear();    
}

void MenuIconOptions::on_titleInput_editingFinished()
{
    ((MenuScreenController *)controller)->iconTitle();
    isFullFill();
}

void MenuIconOptions::setValidators()
{   
    setIntValidators(ui->topMarginInput, Constants::General::MIN_NUM_ELEMENTS , Constants::General::MAX_SCREEN_HEIGHT);
    setIntValidators(ui->bottomMarginInput, Constants::General::MIN_NUM_ELEMENTS , Constants::General::MAX_SCREEN_HEIGHT);
    setIntValidators(ui->rigthMarginInput, Constants::General::MIN_NUM_ELEMENTS , Constants::General::MAX_SCREEN_WIDTH);
    setIntValidators(ui->leftMarginInput, Constants::General::MIN_NUM_ELEMENTS , Constants::General::MAX_SCREEN_WIDTH);
}


int MenuIconOptions::getTopMargin()
{
    return ui->topMarginInput->text().toInt();
}

int MenuIconOptions::getBottomMargin()
{
    return ui->bottomMarginInput->text().toInt();
}

int MenuIconOptions::getLeftMargin()
{
    return ui->leftMarginInput->text().toInt();
}

int MenuIconOptions::getRigthMargin()
{
    return ui->rigthMarginInput->text().toInt();
}

void MenuIconOptions::showError(QString error)
{
    ui->errorTxt->setText(error);
    if(error.isEmpty())
        ui->errorForm->hide();
    else
        ui->errorForm->show();
}

void MenuIconOptions::showSuccess(QString success)
{

}

void MenuIconOptions::showInfo(QString info)
{

}

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

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

void MenuIconOptions::setIcon(QString path)
{
    ui->positionGroup->show();
    if(path.isEmpty())
    {
        ui->iconBtn->setIcon(QIcon());
        showError(tr("Es necesario una imagen para cada icono"));
    }
    else
    {
        showError("");
        QPixmap pixmap(path);
        QIcon icon(pixmap);
        ui->iconBtn->setIcon(icon);
        ui->iconBtn->setAccessibleName(path);
    }
    isFullFill();
}

QString MenuIconOptions::getIconImage()
{
    return ui->iconBtn->accessibleName();
}

void MenuIconOptions::setIconInfo(QString iconPath, QString iconTitle, int top, int bottom, int left, int right)
{
    setIcon(iconPath);
    if(!ui->titleInput->isHidden())
    {
        ui->titleInput->setText(iconTitle);
    }

    if(!ui->topMarginInput->isHidden())
    {
        ui->topMarginInput->setText(QString::number(top));
        ui->bottomMarginInput->setText(QString::number(bottom));
        ui->leftMarginInput->setText(QString::number(left));
        ui->rigthMarginInput->setText(QString::number(right));
    }

}

void MenuIconOptions::on_topMarginInput_textEdited()
{
    ((MenuScreenController *)controller)->topMargin();
    isFullFill();
}

void MenuIconOptions::on_rigthMarginInput_textEdited()
{

    ((MenuScreenController *)controller)->rigthMargin();
    isFullFill();
}

void MenuIconOptions::on_bottomMarginInput_textEdited()
{
    ((MenuScreenController *)controller)->bottomMargin();
    isFullFill();
}

void MenuIconOptions::on_leftMarginInput_textEdited()
{
    ((MenuScreenController *)controller)->leftMargin();
    isFullFill();
}

void MenuIconOptions::on_plusrightBtn_clicked()
{
    int current = getRigthMargin();
    if(current < Constants::General::MAX_SCREEN_WIDTH)
    {
        ui->rigthMarginInput->setText(QString::number(++current));
        ((MenuScreenController *)controller)->rigthMargin();
    }
}

void MenuIconOptions::on_lessRightBtn_clicked()
{
    int current = getRigthMargin();
    if(current > Constants::General::MIN_SCREEN_WIDTH)
    {
        ui->rigthMarginInput->setText(QString::number(--current));
        ((MenuScreenController *)controller)->rigthMargin();
    }
}

void MenuIconOptions::on_plusTopBtn_clicked()
{
    int current = getTopMargin();
    if(current < Constants::General::MAX_SCREEN_HEIGHT)
    {
        ui->topMarginInput->setText(QString::number(++current));
        ((MenuScreenController *)controller)->topMargin();
    }
}

void MenuIconOptions::on_lessTopBtn_clicked()
{
    int current = getTopMargin();
    if(current > Constants::General::MIN_SCREEN_HEIGHT)
    {
        ui->topMarginInput->setText(QString::number(--current));
        ((MenuScreenController *)controller)->topMargin();
    }
}

void MenuIconOptions::on_plusLeftBtn_clicked()
{
    int current = getLeftMargin();
    if(current < Constants::General::MAX_SCREEN_WIDTH)
    {
        ui->leftMarginInput->setText(QString::number(++current));
        ((MenuScreenController *)controller)->leftMargin();
    }
}

void MenuIconOptions::on_lessLeftBtn_clicked()
{
    int current = getLeftMargin();
    if(current > Constants::General::MIN_SCREEN_WIDTH)
    {
        ui->leftMarginInput->setText(QString::number(--current));
        ((MenuScreenController *)controller)->leftMargin();
    }
}

void MenuIconOptions::on_plusBottomBtn_clicked()
{
    int current = getBottomMargin();

    if(current < Constants::General::MAX_SCREEN_HEIGHT)
    {
        ui->bottomMarginInput->setText(QString::number(++current));
        ((MenuScreenController *)controller)->bottomMargin();
    }
}

void MenuIconOptions::on_lessBottomBtn_clicked()
{
    int current = getBottomMargin();
    if(current > Constants::General::MIN_SCREEN_HEIGHT)
    {
        ui->bottomMarginInput->setText(QString::number(--current));
        ((MenuScreenController *)controller)->bottomMargin();
    }
}

void MenuIconOptions::on_hpIconBtn_clicked()
{    
    ((MenuScreenController *)controller)->goBack();
}
