#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTextStream.h>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

}

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

Ui::MainWindow* MainWindow::GetUi()
{
    return ui;
}

void MainWindow::RegisterSignals()
{
    QObject::connect(ui->AddTexturesButton, SIGNAL(pressed()), this, SLOT(OnAddTexturesPressed()));
    QObject::connect(ui->GenerateButton, SIGNAL(pressed()), this, SLOT(OnGeneratePressed()));
    QObject::connect(ui->ClearButton, SIGNAL(pressed()), this, SLOT(OnClearPressed()));
    QObject::connect(ui->SaveTextureButton, SIGNAL(pressed()), this, SLOT(OnSaveTexturePressed()));
    QObject::connect(ui->SaveParamsButton, SIGNAL(pressed()), this, SLOT(OnSaveSheetParamsPressed()));
}



void MainWindow::OnAddTexturesPressed()
{
    QFileDialog dialog(this);

    dialog.setFileMode(QFileDialog::ExistingFiles);

    dialog.setNameFilter("PNG images (*.png)");

    if (dialog.exec())
    {

        QStringList fileNames = dialog.selectedFiles();

        for (int i = 0; i < fileNames.count(); i++)
        {
            boost::shared_ptr<QPixmap> pixmap(new QPixmap(fileNames[i], "PNG"));

            QFileInfo q;
            q.setFile(fileNames[i]);

            QString fn = q.fileName();

            //std::string fileName = fn.toUtf8().constData();

            ui->textureList->addItem(fn);

            textureSheet.pixmapArray.push_back(std::pair<QString, boost::shared_ptr<QPixmap>>(fn, pixmap));
            textureSheet.imageNamesArray.push_back(fn);

        }
    }
}

void MainWindow::OnGeneratePressed()
{
    int width = ui->widthSpinBox->value();
    int height = ui->heightSpinBox->value();

    try
    {
    textureSheet.Calculate(width, height);
    QString xml = textureSheet.GetCalculatedXml();
    ui->textureParamsEdit->clear();

    ui->textureParamsEdit->appendPlainText(xml);

    }
    catch (std::runtime_error& error)
    {
        QMessageBox msgBox;
        msgBox.setText("Not enough space");
        msgBox.exec();
    }


    ui->PixmapSheet->setPixmap(*(textureSheet.resultPixmap));
}

void MainWindow::OnClearPressed()
{
    textureSheet.Clear();
    ui->PixmapSheet->setPixmap(*(textureSheet.resultPixmap));

    ui->textureList->clear();
}

void MainWindow::OnSaveTexturePressed()
{
    QFileDialog dialog(this);

    dialog.setAcceptMode(QFileDialog::AcceptSave);

    dialog.setFileMode(QFileDialog::AnyFile);

    dialog.setNameFilter("PNG images (*.png)");

    dialog.setDefaultSuffix("png");

    if (dialog.exec())
    {
        QString fileName = *(dialog.selectedFiles().begin());

        textureSheet.resultPixmap->save(fileName, "PNG");
    }
}

void MainWindow::OnSaveSheetParamsPressed()
{
    QFileDialog dialog(this);

    dialog.setAcceptMode(QFileDialog::AcceptSave);

    dialog.setFileMode(QFileDialog::AnyFile);

    dialog.setNameFilter("XML files (*.xml)");

    dialog.setDefaultSuffix("xml");

    if (dialog.exec())
    {
        QString fileName = *(dialog.selectedFiles().begin());

        QFile file(fileName);
        if (file.open(QIODevice::WriteOnly | QIODevice::Text))
        {
             QTextStream outStream(&file);
             outStream << ui->textureParamsEdit->toPlainText();
        }

    }
}
