#include "inkscreencontroller.h"
#include <Model/ink.h>
#include <QFileDialog>
#include <Services/parser.h>
#include <View/mainwindow.h>

InkScreenController::InkScreenController(InkScreen *view)
{   
    generalView = view;
    optionsView = nullptr;
    interfaz = new Ink();
}

void InkScreenController::heightView()
{
    int height = ((InkScreen*)generalView)->getHeight();
    interfaz->setHeigth(height);
}

void InkScreenController::widthView()
{
    int width = ((InkScreen*)generalView)->getWidth();
    interfaz->setWidth(width);
}

void InkScreenController::checkMenuSize()
{
    int rows = ((InkScreen*)generalView)->getNumberOfRows();
    int cols = ((InkScreen*)generalView)->getNumberOfCols();
    int elements = ((InkScreen*)generalView)->getNumberOfElements();
    ((InkScreen *) generalView)->clearElements();
    int userSize = rows * cols;
    QString error;

    if(rows != 0 && cols != 0 && elements != 0)
    {

        if(userSize < elements )
        {
            generalView->showError(tr("Para el número de iconos actual se necesitan mas filas/columnas"));
        }
        else if(userSize - elements >= cols || userSize - elements >= rows)
        {
            generalView->showError(tr("Para el número de iconos actual sobran filas/columnas"));
        }
        else{
            interfaz->setNumberOfRows(rows);
            interfaz->setNumberOfCols(cols);                 
            ((Ink *) interfaz)->setNumberOfElements(elements);
            ((InkScreen *)generalView)->addInkButtons(elements, rows, cols, ((Ink *) interfaz)->getInkColors());
        }
    }
    else
    {
        ((InkScreen *) generalView)->showInfo(tr("Podrá seleccionar colores cuando cumplimente los datos anteriores"));
    }



}

void InkScreenController::textProperty()
{
    InkTextOption textOption = ((InkScreen *) generalView)->getTextOption();
    ((Ink *) interfaz)->setTextOption(textOption);
}

void InkScreenController::inkColor(int btnPosition)
{
    QColor color = ((InkScreen *) generalView)->getInkColor(btnPosition);
    ((Ink *) interfaz)->addColor(btnPosition, color);
}

void InkScreenController::setModel(Interfaz *ink)
{
    interfaz = ink;
}

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

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

void InkScreenController::selectedIcon()
{
    QObject *btn = QObject::sender();
    QPushButton* buttonSender = qobject_cast<QPushButton*>(btn);
}
