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

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

    Pixmap = new QPixmap(960, 640);
    levelData = new LevelData;
    levelData->mainWindow = this;

    CurrentMode = CM_ADD_POLYS;

}

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

void MainWindow::InitSlots()
{

    QObject::connect(ui->LoadBackgroundButton, SIGNAL(pressed()), this, SLOT(LoadBackgroundSlot()));
    QObject::connect(ui->radioButtonAddPolys, SIGNAL(pressed()), this, SLOT(RadioButtonAddPolysSelected()));
    QObject::connect(ui->radioButtonModifyPolys, SIGNAL(pressed()), this, SLOT(RadioButtonModifyPolysSelected()));
    QObject::connect(ui->Image, SIGNAL(buttonPressed(int, int)), levelData, SLOT(OnMouseDown(int, int)));

    QObject::connect(ui->SaveLevelButton, SIGNAL(pressed()), this, SLOT(OnSaveButtonPressed()));
    QObject::connect(ui->LoadLevelButton, SIGNAL(pressed()), this, SLOT(OnLoadButtonPressed()));

    InitSlotsForAddPolys();
}

void MainWindow::InitSlotsForAddPolys()
{
    QObject::disconnect(ui->Image, SIGNAL(leftButtonReleased(int, int)), levelData, SLOT(OnSelectPoint(int, int)));
    QObject::disconnect(ui->Image, SIGNAL(mouseMoveWithRightButtonPressed(int, int)), levelData, SLOT(OnMovePoint(int, int)));
    QObject::disconnect(this, SIGNAL(deleteButtonPressed()), levelData, SLOT(DeleteSelectedPoint()));


    QObject::connect(ui->Image, SIGNAL(leftButtonReleased(int, int)), levelData, SLOT(OnAddPoint(int, int)));
    QObject::connect(ui->Image, SIGNAL(rightButtonReleased(int, int)), levelData, SLOT(OnApplyPointList(int, int)));
    QObject::connect(this, SIGNAL(deleteButtonPressed()), levelData, SLOT(DeleteLastTempPoint()));

}

void MainWindow::InitSlotsForModifyPolys()
{
    QObject::disconnect(ui->Image, SIGNAL(leftButtonReleased(int, int)), levelData, SLOT(OnAddPoint(int, int)));
    QObject::disconnect(ui->Image, SIGNAL(rightButtonReleased(int, int)), levelData, SLOT(OnApplyPointList(int, int)));
    QObject::disconnect(this, SIGNAL(deleteButtonPressed()), levelData, SLOT(DeleteLastTempPoint()));

    QObject::connect(ui->Image, SIGNAL(leftButtonReleased(int, int)), levelData, SLOT(OnSelectPoint(int, int)));
    QObject::connect(ui->Image, SIGNAL(mouseMoveWithRightButtonPressed(int, int)), levelData, SLOT(OnMovePoint(int, int)));
    QObject::connect(this, SIGNAL(deleteButtonPressed()), levelData, SLOT(DeleteSelectedPoint()));

}

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

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

    dialog.setFileMode(QFileDialog::ExistingFile);

    if (dialog.exec())
    {
        QString filename = (dialog.selectedFiles())[0];
        bkgImage = QImage(filename);

        levelData->RefreshPixmap();
        //ui->Image->setPixmap(*Pixmap);
    }
}

void MainWindow::RadioButtonAddPolysSelected()
{
    CurrentMode = CM_ADD_POLYS;
    InitSlotsForAddPolys();
    levelData->ResetSelected();
    levelData->RefreshPixmap();
}

void MainWindow::RadioButtonModifyPolysSelected()
{
    CurrentMode = CM_MODIFY_POLYS;
    InitSlotsForModifyPolys();
    levelData->ResetTempPoints();
    levelData->RefreshPixmap();
}

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

    dialog.setNameFilter(tr("xml (*.xml)"));

    dialog.setFileMode(QFileDialog::AnyFile);

    if (dialog.exec())
    {
        QString filename = (dialog.selectedFiles())[0];

        QString xmlOut;

        QXmlStreamWriter stream(&xmlOut);
        stream.setAutoFormatting(true);
        stream.writeStartDocument();

        stream.writeStartElement("LevelPolyArr");

        for (size_t i = 0; i < levelData->PolyArr.size(); i++)
        {
            stream.writeStartElement("LevelPoly");

            for (size_t j = 0; j < levelData->PolyArr[i].pointArr.size(); j++)
            {
                stream.writeStartElement("Point");
                stream.writeAttribute("x", QString::number(levelData->PolyArr[i].pointArr[j].x()));
                stream.writeAttribute("y", QString::number(levelData->PolyArr[i].pointArr[j].y()));
                stream.writeEndElement();
            }

            stream.writeEndElement();
        }

        stream.writeEndElement();

        stream.writeEndDocument();

        QFile file(filename);
        if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
            return;

        QTextStream out(&file);
        out << xmlOut;

        file.close();
    }

}

void MainWindow::OnLoadButtonPressed()
{

}

void MainWindow::SetImagePixmap(QPixmap* p)
{
    ui->Image->setPixmap(*p);
}

void MainWindow::keyReleaseEvent(QKeyEvent * qev)
{
    if (qev->key() == Qt::Key_Delete)
    {
        emit deleteButtonPressed();
    }
}
