#include <QMessageBox>
#include <QFileDialog>
#include <QQuickView>
#include "menuscreencontroller.h"
#include "View/mainwindow.h"
#include "View/settings.h"
#include "Model/menu.h"
#include "View/menuiconoptions.h"
#include "Services/parser.h"

MenuScreenController::MenuScreenController(MenuScreen *view)
{
    generalView = view;
    optionsView = nullptr;
    isFromEdit = false;
    isBack = false;
    interfaz = new Menu();
}

void MenuScreenController::updateIconViewProgress(bool isBack){

    currentIcon ++;

    int totalIcons = interfaz->getNumberOfElements();
    int progress = (currentIcon -1)*(100 / totalIcons);

    if(!isBack)
    {
        calculateCurrentIcon();
    }

    ((MenuIconOptions*)optionsView)->setProgress(progress);
}

void MenuScreenController::calculateCurrentIcon()
{
    int numberOfCols = interfaz->getNumberOfCols();
    int currentCol;
    int currentRow = ((int)(currentIcon/numberOfCols)) +1;
    if( currentIcon%numberOfCols == 0) currentRow --;

    if(currentIcon <= numberOfCols)
    {
        currentCol = currentIcon;
    }
    else
    {
        currentCol = currentIcon - ((currentRow -1) * numberOfCols);
    }

    ((MenuIconOptions*)optionsView)->setCurrentIcon(currentRow, currentCol );

}

void MenuScreenController::setIconInfo()
{
    QString iconPath = ((Menu *)interfaz)->getIconPath(currentIcon - 1);
    iconPath = iconPath.replace("file:///", "");
    QString iconTitle = ((Menu *)interfaz)->getIconTitle(currentIcon - 1);
    int top = 0;
    int bottom = 0;
    int right = 0;
    int left = 0;

    if(((Menu *)interfaz)->getCustomPositionModified() && !((Menu *)interfaz)->isAutomaticPosition())
    {
        top = ((Menu *)interfaz)->getIconTopMargin(currentIcon - 1);
        bottom = ((Menu *)interfaz)->getIconBottomMargin(currentIcon - 1);
        left = ((Menu *)interfaz)->getIconLeftMargin(currentIcon - 1);
        right = ((Menu *)interfaz)->getIconRightMargin(currentIcon - 1);
    }

    ((MenuIconOptions*)optionsView)->setIconInfo(iconPath, iconTitle, top, bottom, left, right);

}

void MenuScreenController::on_title_options( ){
    bool hasTitle = ((MenuScreen*)generalView)->isTitleOption();
    ((Menu *)interfaz)->setHasTitle(hasTitle);
}

void MenuScreenController::on_continueBtn_clicked(){        

    currentIcon = 0;

    bool hasText = ((Menu *)interfaz)->isTitleEnabled();
    bool isAtomatic = ((Menu *)interfaz)->isAutomaticPosition();
    optionsView = new MenuIconOptions();
    ((MenuIconOptions*)optionsView)->setController(this);
    ((MenuIconOptions*)optionsView)->setEnabledOptions(hasText, isAtomatic);

    switchToOptionsView();

    updateIconViewProgress();

    if(isFromEdit)
    {
        ((Menu *)interfaz)->resizeNumberOfIcons();
        setIconInfo();
    }
    else
    {
        ((Menu *)interfaz)->createIcons();
    }
}

void MenuScreenController::automatic_icon_position(){
    bool isAutmatic = ((MenuScreen *)generalView)->isAutomaticPosition();
    ((Menu *)interfaz)->setAutomaticPosition(isAutmatic);
}

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

void MenuScreenController::iconTitle()
{
    QString iconTitle = ((MenuIconOptions*)optionsView)->getIconTitle();
    ((Menu *)interfaz)->setIconTitle(currentIcon-1, iconTitle);
}

void MenuScreenController::nextIcon()
{    
    if(currentIcon < interfaz->getNumberOfElements())
    {
        ((MenuIconOptions*)optionsView)->clear();
        updateIconViewProgress();

        if(isFromEdit || isBack)
        {
            setIconInfo();
        }
    }
    else
    {
        finish();
    }
}

void MenuScreenController::topMargin()
{
    int topMargin = ((MenuIconOptions*)optionsView)->getTopMargin();    
    ((Menu *)interfaz)->setIconTopMargin(currentIcon - 1, topMargin);
}

void MenuScreenController::bottomMargin()
{
    int bottomMargin= ((MenuIconOptions*)optionsView)->getBottomMargin();    
    ((Menu *)interfaz)->setIconBottomMargin(currentIcon -1, bottomMargin);
}

void MenuScreenController::leftMargin()
{
    int leftMargin = ((MenuIconOptions*)optionsView)->getLeftMargin();    
    ((Menu *)interfaz)->setIconLeftMargin(currentIcon -1, leftMargin);
}

void MenuScreenController::rigthMargin()
{
    int rigthMargin = ((MenuIconOptions*)optionsView)->getRigthMargin();    
    ((Menu *)interfaz)->setIconRightMargin(currentIcon -1, rigthMargin);
}

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

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

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

    if(path.isEmpty()) on_homeBtn_clicked();
    else{
        Parser *parser = new Parser();
        parser->parse((Menu *)interfaz, path);
        optionsView->close();
        MainWindow *w = new MainWindow();
        w->show();
        showPreview(path);
    }
}

void MenuScreenController::backExit()
{
    calculateCurrentIcon();
}

void MenuScreenController::checkMenuSize()
{
    int rows = ((MenuScreen*)generalView)->getNumberOfRows();
    int cols = ((MenuScreen*)generalView)->getNumberOfCols();
    int icons = ((MenuScreen*)generalView)->getNumberOfIcons();
    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);
        }
    }

    generalView->showError(error);


}

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

void MenuScreenController::widthView()
{
    int width = ((MenuScreen*)generalView)->getWidthView();
    interfaz->setWidth(width);
}

void MenuScreenController::iconImage()
{
    QString path = ((MenuIconOptions *)optionsView)->getIconImage();
    QString qmlPath = QString(HEADER_FILE).append(path);
    Icon *icon =  ((Menu *)interfaz)->getIcons()->at(0);
    ((Menu *)interfaz)->setIconPath(currentIcon - 1, qmlPath);
}

void MenuScreenController::setModel(Interfaz *menu)
{
    interfaz = menu;
    isFromEdit = true;
}

void MenuScreenController::goBack()
{

    if(currentIcon > 1)
    {
        currentIcon -= 2;
        ((MenuIconOptions*)optionsView)->clear();
        updateIconViewProgress(true);
        setIconInfo();
        isBack = true;
    }
    else
    {
        generalView = new MenuScreen(this);
        ((MenuScreen *)generalView)->setEditModel(interfaz);
        generalView->show(optionsView->pos(), optionsView->size());
        optionsView->close();
        optionsView = nullptr;
    }
}
