#include "informationscreenoptions.h"
#include "ui_informationscreenoptions.h"
#include <Controller/informationscreencontroller.h>

#include <QDir>
#include <QPropertyAnimation>
#include <Common/constants.h>

#include <QDebug>
InformationScreenOptions::InformationScreenOptions(bool hasIcons, QWidget *parent) :
    View(parent),
    ui(new Ui::InformationScreenOptions)
{
    ui->setupUi(this);
    initElements();
    if(!hasIcons)hideIconElements();
}

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

void InformationScreenOptions::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 InformationScreenOptions::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 InformationScreenOptions::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 InformationScreenOptions::setController(Controller *controller)
{
    this->controller = controller;
}

QString InformationScreenOptions::getCardTitle()
{
    return ui->titleInput->text();
}

void InformationScreenOptions::hideIconElements()
{
    ui->iconBox->hide();
    ui->imageBtn->hide();
    ui->browseBtn->hide();
    ui->iconTxt->hide();
}

void InformationScreenOptions::isPropertyFill()
{
    bool complete = !ui->propertyInput->text().isEmpty() &&
            !ui->valueInput->text().isEmpty();

    ui->addPropertyBtn->setEnabled(complete);
    ui->removePropertyBtn->setEnabled(complete);
    ui->editPropertyBtn->setEnabled(complete);
}

void InformationScreenOptions::clearPropertyInput()
{
    ui->propertyInput->clear();
    ui->valueInput->clear();
}

void InformationScreenOptions::showPropertyOptions()
{
    ui->removePropertyBtn->setEnabled(true);
    ui->editPropertyBtn->setEnabled(true);
}

void InformationScreenOptions::setToolTips()
{
    ui->browseBtn->setToolTip(tr("Seleccionar icono"));
    ui->addPropertyBtn->setToolTip(tr("Añadir propiedad"));
    ui->removePropertyBtn->setToolTip(tr("Eliminar propiedad"));
    ui->editPropertyBtn->setToolTip(tr("Editar propiedad"));
}

void InformationScreenOptions::setIconImage(QString path)
{
    QPixmap pixmap(path);
    QIcon icon(pixmap);
    ui->imageBtn->setIcon(icon);
}

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

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

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

void InformationScreenOptions::initElements()
{        
    setValidators();
    setToolTips();
    ui->edit_spacer->invalidate();
    ui->infoForm->hide();
    ui->addPropertyBtn->setDisabled(true);
    ui->editPropertyBtn->setDisabled(true);
    ui->removePropertyBtn->setDisabled(true);
    ui->errorForm->hide();
    ui->successForm->hide();
    ui->continueBtn->hide();

}

void InformationScreenOptions::setValidators()
{

}

void InformationScreenOptions::isFullFill()
{
    bool complete = !ui->titleInput->text().isEmpty() &&
            ui->propertiesList->count() != 0 &&
            ((!ui->successForm->isHidden() ||
             (ui->imageBtn->accessibleName() != ":/resources/icon/image.png" &&
            !ui->imageBtn->accessibleName().isEmpty()))
            || ui->iconBox->isHidden());

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

void InformationScreenOptions::on_titleInput_textEdited()
{
    ((InformationScreenController *)controller)->cardTitle();
    isFullFill();
}


void InformationScreenOptions::on_propertyInput_textChanged()
{
    isPropertyFill();
}

void InformationScreenOptions::on_valueInput_textChanged()
{
    isPropertyFill();
}

void InformationScreenOptions::on_addPropertyBtn_clicked()
{
    showError("");
    ((InformationScreenController *)controller)->addProperty();
    clearPropertyInput();
    isPropertyFill();
    isFullFill();
}

void InformationScreenOptions::on_removePropertyBtn_clicked()
{
    ((InformationScreenController *)controller)->removeProperty();
    isFullFill();
}

QString InformationScreenOptions::getPropertyName()
{
    return ui->propertyInput->text();
}

QString InformationScreenOptions::getPropertyValue()
{
    return ui->valueInput->text();
}

void InformationScreenOptions::addProperty(QString name)
{
    ui->propertiesList->addItem(name);
    isFullFill();
}

void InformationScreenOptions::showProperty(QStringList property)
{
    ui->propertyInput->setText(property.takeFirst());
    ui->valueInput->setText(property.takeFirst());
    showPropertyOptions();
}


int InformationScreenOptions::getCurrentIndexProperty()
{
    return ui->propertiesList->currentRow();
}

void InformationScreenOptions::updatePropertyList(bool isChange, int index, QString propertyName)
{
    ui->propertiesList->takeItem(index);
    if(isChange)
    {
        ui->propertiesList->insertItem(index, propertyName);
    }
    showError(QString::null);

}

void InformationScreenOptions::on_editPropertyBtn_clicked()
{    
    ui->propertiesList->clearSelection();
    ui->propertiesList->clearFocus();
    ((InformationScreenController *)controller)->changeProperty();
    clearPropertyInput();
}

void InformationScreenOptions::on_propertiesList_itemClicked()
{
    ((InformationScreenController *)controller)->selectProperty();
}

void InformationScreenOptions::on_continueBtn_clicked()
{
    ui->imageBtn->setAccessibleName("");
    ((InformationScreenController *)controller)->nextCard();    
    ui->errorForm->hide();
    ui->successForm->hide();
    isFullFill();
}

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

QString InformationScreenOptions::getCardImage()
{
    return ui->imageBtn->accessibleName();
}

void InformationScreenOptions::disableAddBtn()
{
    ui->addPropertyBtn->setEnabled(false);
}

void InformationScreenOptions::clear()
{
    ui->titleInput->clear();
    ui->propertiesList->clear();
    ui->valueInput->clear();
    ui->propertyInput->clear();
    ui->imageBtn->setIcon(QIcon(":/resources/icon/image.png"));
}

void InformationScreenOptions::setCardTitle(QString title)
{   
    ui->titleInput->setText(title);
}

void InformationScreenOptions::setCardImage(QString path)
{
    setIconImage(path);
    ui->imageBtn->setAccessibleName(path);
    ((InformationScreenController *)controller)->cardIcon();
}

void InformationScreenOptions::on_browseBtn_clicked()
{    
    QString path = View::selectImage();
    if(path.isEmpty())
    {
        showError(tr("Debe seleccionar una imágen valida"));
        showSuccess("");
    }
    else
    {
        ui->imageBtn->setAccessibleName(path);
        ((InformationScreenController *)controller)->cardIcon();
        showError("");
        setIconImage(path);
        showSuccess(tr("Imagen ") + path + tr(" cargada correctamente"));
    }
    isFullFill();
}

void InformationScreenOptions::on_hpIconBtn_clicked()
{
    ((InformationScreenController *)controller)->goBack();
}
