#include "Animation.h"
#include "mainwindow.h"

TAnimation::TAnimation(QObject *parent)
    : QObject(parent)
{
}

void TAnimation::SetMainWindow(MainWindow* window)
{
    Window = window;
}


void TAnimation::AddFrame(const TFrame& frame)
{
    FrameList.push_back(frame);
}


TFrame& TAnimation::GetFrame(int i)
{
    return FrameList[i];
}

size_t TAnimation::GetFrameCount()
{
    return FrameList.size();
}

int TAnimation::GetMaxWidth()
{
    int r = FrameList[0].Pixmap->width() + FrameList[0].ShiftX;

    for (size_t i = 1; i < FrameList.size(); i++)
    {
        if (r < FrameList[i].Pixmap->width() + FrameList[i].ShiftX)
        {
            r = FrameList[i].Pixmap->width() + FrameList[i].ShiftX;
        }
    }

    return r;
}

int TAnimation::GetMaxHeight()
{
    int r = FrameList[0].Pixmap->height() + FrameList[0].ShiftY;

    for (size_t i = 1; i < FrameList.size(); i++)
    {
        if (r < FrameList[i].Pixmap->height() + FrameList[i].ShiftY)
        {
            r = FrameList[i].Pixmap->height() + FrameList[i].ShiftY;
        }
    }

    return r;
}


void TAnimation::FillSheet(boost::shared_ptr<QPixmap> sheetPixmap, std::vector<TFrameData>& frameDataArr)
{
    int maxFrameWidth = GetMaxWidth();
    int maxFrameHeight = GetMaxHeight();

    int cursorX = 0;
    int cursorY = 0;



    QPainter p;


    p.begin(&(*sheetPixmap));


    for (size_t i=0; i<FrameList.size(); i++)
    {
        int shiftX = FrameList[i].ShiftX;
        int shiftY = FrameList[i].ShiftY;


        TFrameData frameData;

        frameData.TexCoordFromX = static_cast<float>(cursorX) / sheetPixmap->width();
        frameData.TexCoordFromY = 1.f - static_cast<float>(cursorY + maxFrameHeight) / sheetPixmap->height();

        frameData.TexCoordToX = static_cast<float>(cursorX + maxFrameWidth) / sheetPixmap->width();
        frameData.TexCoordToY = 1.f - static_cast<float>(cursorY) / sheetPixmap->height();

        frameData.TimeToPlay = FrameList[i].TimeToPlay;

        frameDataArr.push_back(frameData);




        QPixmap& pixmapToDraw = *(FrameList[i].Pixmap);

        p.drawPixmap(cursorX + shiftX, cursorY + shiftY, pixmapToDraw);

        cursorX += maxFrameWidth;

        if (cursorX + maxFrameWidth >= sheetPixmap->width())
        {
            cursorX = 0;
            cursorY += maxFrameHeight;
        }


    }

    p.end();







}


void TAnimation::LoadFrame()
{
    QFileDialog dialog(Window);

    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();

            AddFrame(TFrame(pixmap, 0, fileName));

            Window->LoadFrame(fileName);
        }

        //FontFileName = (dialog.selectedFiles())[0];
        //emit SetFontNameTextSignal(FontFileName);

        //LoadFontFile();
    }
}


void TAnimation::Clear()
{
    FrameList.clear();
}



