#include "informationscreencontroller.h"
#include "View/informationscreenoptions.h"
#include "Model/information.h"
#include <QFileDialog>
#include <Services/parser.h>
#include <View/mainwindow.h>

InformationScreenController::InformationScreenController(InformationScreen *view)
{
    generalView = view;
    optionsView = nullptr;
    interfaz = new Information();
    isFromEdit = false;
}

void InformationScreenController::checkGeneralMenuSize()
{
    int rows = ((InformationScreen*)generalView)->getNumberOfRows();
    int cols = ((InformationScreen*)generalView)->getNumberOfCols();
    int icons = ((InformationScreen*)generalView)->getNumberOfIcons();
    QString error = checkMenuSize(icons, rows,cols);
    generalView->showError(error);
}

QString InformationScreenController::checkMenuSize(int icons, int rows, int cols)
{

    QString error;

    if(rows != 0 && cols != 0 && icons != 0)
    {
        int numberOfElements = rows * cols;
        if(numberOfElements < icons )
        {
            error = QString(tr("Para el número de iconos actual se necesitan mas filas/columnas"));
        }
        else if(numberOfElements - icons >= cols || numberOfElements - icons >= rows)
        {
            error = QString(tr("Para el número de iconos actual sobran filas/columnas"));
        }
        else{
            error = QString("");
            interfaz->setNumberOfRows(rows);
            interfaz->setNumberOfCols(cols);
        }
    }

    return error;

}

void InformationScreenController::heightView()
{
    int height = ((InformationScreen*)generalView)->getHeightView();
    interfaz->setHeigth(height);
}

void InformationScreenController::widthView()
{
    int width = ((InformationScreen*)generalView)->getWidthView();
    interfaz->setWidth(width);

}

void InformationScreenController::colorBackground()
{
    QColor colorBackground = ((InformationScreen*)generalView)->getBackgroundColor();
    interfaz->setHasImageBackground(false);
    interfaz->setColorBackground(colorBackground);
}

void InformationScreenController::imageBackground()
{
    QString path = ((InformationScreen *)generalView)->getBackgroundImage();
    QString qmlPath = QString(HEADER_FILE).append(path);
    interfaz->setHasImageBackground(true);
    interfaz->setImageBackground(qmlPath);
}

void InformationScreenController::cardSize()
{
    int rows = ((InformationScreen *)generalView)->getCardRows();
    int cols = ((InformationScreen *)generalView)->getCardCols();

    ((Information *) interfaz)->setCardRows(rows);
    ((Information *) interfaz)->setCardCols(cols);

}

void InformationScreenController::number_of_icons()
{
    int numberOfIcons = ((InformationScreen *)generalView)->getNumberOfIcons();
    interfaz->setNumberOfElements(numberOfIcons);
}

void InformationScreenController::on_icon_options()
{
    bool hasIcons = ((InformationScreen *)generalView)->getHasIcons();
    interfaz->setHasIcons(hasIcons);
}

void InformationScreenController::on_continueBtn_clicked()
{
    currentCard = 0;

    if(isFromEdit)
    {
        ((Information *)interfaz)->resizeNumberOfCards();
        ((Information *)interfaz)->resizeNumberOfProperties();
    }
    else
    {
        ((Information *)interfaz)->createCards();
    }

    optionsView = new InformationScreenOptions(interfaz->getHasIcons());
    ((InformationScreenOptions *)optionsView)->setController(this);
    switchToOptionsView();
    updateCardViewProgress();

    if(isFromEdit|| isBack)
    {
        setViewInfo();
    }
    isBack = false;
}

void InformationScreenController::cardTitle()
{
    QString title = ((InformationScreenOptions *) optionsView)->getCardTitle();
    ((Information *) interfaz)->setCardTitle(currentCard -1, title);
}

void InformationScreenController::cardIcon()
{
    QString path = ((InformationScreenOptions *)optionsView)->getCardImage();
    QString qmlPath = QString(HEADER_FILE).append(path);
    ((Information *) interfaz)->setCardImage(currentCard -1, qmlPath);
}

void InformationScreenController::addProperty()
{
    if(((Information *) interfaz)->hasMaximumOfProoperties(currentCard -1))
    {
        ((InformationScreenOptions*) optionsView)->disableAddBtn();
        optionsView->showInfo(tr("Ha alcanzado el número máximo de elementos"));
        return;
    }
    QString propertyName = ((InformationScreenOptions *) optionsView)->getPropertyName();
    QString propertyValue = ((InformationScreenOptions *) optionsView)->getPropertyValue();
    ((Information *) interfaz)->addProperty(currentCard -1, propertyName, propertyValue);
    ((InformationScreenOptions *) optionsView)->addProperty(propertyName);
    ((InformationScreenOptions *) optionsView)->clearPropertyInput();
    ((InformationScreenOptions *) optionsView)->isPropertyFill();
}

void InformationScreenController::removeProperty()
{
    int propertyIndex = ((InformationScreenOptions *) optionsView)->getCurrentIndexProperty();
    if(propertyIndex == -1)
    {
        optionsView->showError(tr("Para eliminar una propiedad debe seleccionarla previamente"));
        return;
    }
    ((Information *) interfaz)->removeProperty(currentCard -1, propertyIndex);
    ((InformationScreenOptions *) optionsView)->updatePropertyList(false, propertyIndex, QString::null);
    ((InformationScreenOptions *) optionsView)->clearPropertyInput();
    ((InformationScreenOptions *) optionsView)->isPropertyFill();
}

void InformationScreenController::changeProperty()
{
    int propertyIndex = ((InformationScreenOptions *) optionsView)->getCurrentIndexProperty();
    if(propertyIndex == -1)
    {
        optionsView->showError(tr("Para editar una propiedad debe seleccionarla previamente"));
        return;
    }
    QString propertyName = ((InformationScreenOptions *) optionsView)->getPropertyName();
    QString propertyValue = ((InformationScreenOptions *) optionsView)->getPropertyValue();
    ((Information *) interfaz)->addProperty(currentCard -1, propertyIndex, propertyName, propertyValue);
    ((InformationScreenOptions *) optionsView)->updatePropertyList(true, propertyIndex, propertyName);
}

void InformationScreenController::selectProperty()
{
    int propertyIndex = ((InformationScreenOptions *) optionsView)->getCurrentIndexProperty();
    QStringList property = ((Information *) interfaz)->getProperty(currentCard -1, propertyIndex);
    ((InformationScreenOptions *) optionsView)->showProperty(property);
}

void InformationScreenController::nextCard()
{
    int num = interfaz->getNumberOfElements();
    if(currentCard < interfaz->getNumberOfElements())
    {
        ((InformationScreenOptions*)optionsView)->clear();
        updateCardViewProgress();

        if(isFromEdit || isBack)
        {
            setViewInfo();
        }
    }
    else
    {
        finish();
    }
    isBack = false;
}

void InformationScreenController::updateCardViewProgress()
{
    currentCard ++;

    int totalIcons = interfaz->getNumberOfElements();
    int progress = (currentCard -1)*(100 / totalIcons);
    ((InformationScreenOptions *) optionsView)->setProgress(progress);
}

void InformationScreenController::setModel(Interfaz *information)
{
    interfaz = information;
    isFromEdit = true;
}

void InformationScreenController::goBack()
{
    isBack = true;
    if(currentCard > 1)
    {
        currentCard -=2;
        nextCard();        
    }
    else
    {
        generalView = new InformationScreen(this);
        ((InformationScreen *)generalView)->setEditModel(interfaz);
        generalView->show(optionsView->pos(), optionsView->size());
        optionsView->close();
        optionsView = nullptr;
    }
    isBack = true;
}

void InformationScreenController::setViewInfo()
{
    ((InformationScreenOptions *)optionsView)->setCardTitle(((Information *) interfaz)->getCardTitle(currentCard - 1));

    QString image;
    if(((Information *)interfaz)->getHasIcons())
    {
        image = ((Information *) interfaz)->getCardImage(currentCard - 1);
        image.replace("file:///", "");
    }

    if(image == nullptr)
    {
        image = ":/resources/icon/image.png";
    }

    ((InformationScreenOptions *)optionsView)->setCardImage(image);


    ((InformationScreenOptions *)optionsView)->setCardTitle(((Information *) interfaz)->getCardTitle(currentCard - 1));

    for(int i = 0; i < ((Information *)interfaz)->getNumberOfProperties(currentCard -1); i++)
    {
        QStringList property = ((Information *)interfaz)->getProperty(currentCard -1, i);
        ((InformationScreenOptions *)optionsView)->addProperty(property.at(0));
    }

}

void InformationScreenController::finish()
{
    QString path = getLongSavePath();

    if(path.isEmpty()) on_homeBtn_clicked();
    else{
        for(int i = 0; i < interfaz->getNumberOfElements(); i++)
        {
            ((Information *) interfaz)->addEmptyLeftProperties(i);
        }

        Parser *parser = new Parser();
        parser->parse((Information *)interfaz, path);
        optionsView->close();
        MainWindow *w = new MainWindow();
        w->show();
        showPreview(path);
    }
}
