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


struct TBoneStruct
{
    float v[3];
    float q[4];
    float len;

    QString parentBoneName;
};


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

    QObject::connect(ui->ConvertAn1, SIGNAL(clicked()), this, SLOT(CallConvertAn1()));
    QObject::connect(ui->ConvertBn1, SIGNAL(clicked()), this, SLOT(CallConvertBn1()));
}

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


void MainWindow::CallConvertAn1()
{
    QFileDialog dialog(this);
    dialog.setFileMode(QFileDialog::AnyFile);
    dialog.setAcceptMode(QFileDialog::AcceptOpen);

    if (dialog.exec())
    {
        QMessageBox msgBox1;
        msgBox1.setText("Begin");
        msgBox1.exec();

        QFile file(dialog.selectedFiles()[0]);
        if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
        {
            QMessageBox msgBox;
            msgBox.setText("Fail");
            msgBox.exec();
            return;
        }




        QFileDialog dialogSave(this);
        dialogSave.setFileMode(QFileDialog::AnyFile);
        dialogSave.setAcceptMode(QFileDialog::AcceptSave);

        if (!dialogSave.exec())
        {
            return;
        }

        QByteArray saveArr;


        QFile saveFile(dialogSave.selectedFiles()[0]);
        if (!saveFile.open(QIODevice::WriteOnly | QIODevice::Truncate))
        {
            QMessageBox msgBox;
            msgBox.setText("Fail");
            msgBox.exec();
            return;
        }


        QTextStream in(&file);
        QString line;

        //======= Go file parsing

        saveArr.push_back('A');
        saveArr.push_back('N');
        saveArr.push_back((char)0);
        saveArr.push_back((char)1);

        line = in.readLine();

        int frameCount = line.remove("Frames ").toInt();

        line = in.readLine();

        int boneCount = line.remove("Bones ").toInt();

        saveArr.insert(saveArr.size(), (char*)&boneCount, 4);
        saveArr.insert(saveArr.size(), (char*)&frameCount, 4);

        QStringList splited;

        float v[3];
        float q[4];

        float len;

        for (int i=0; i<frameCount; i++)
        {

        line = in.readLine();

        for (int j=0; j<boneCount; j++)
        {

        line = in.readLine();
        splited = line.split(" ", QString::SkipEmptyParts);

        v[0] = splited.at(0).toFloat()*0.1;
        v[1] = splited.at(2).toFloat()*0.1;
        v[2] = -splited.at(1).toFloat()*0.1;

        line = in.readLine();
        splited = line.split(" ", QString::SkipEmptyParts);

        q[0] = splited.at(0).toFloat();
        q[1] = splited.at(2).toFloat();
        q[2] = -splited.at(1).toFloat();
        q[3] = splited.at(3).toFloat();

        line = in.readLine();
        len = line.toFloat()*0.1;

        saveArr.insert(saveArr.size(), (char*)v, 12);
        saveArr.insert(saveArr.size(), (char*)q, 16);
        saveArr.insert(saveArr.size(), (char*)&len, 4);
        }

        }


        saveFile.write(saveArr);

        //======= End file parsing

        QMessageBox msgBox;
        msgBox.setText("End");
        msgBox.exec();


    }

}

void MainWindow::CallConvertBn1()
{
    QFileDialog dialog(this);
    dialog.setFileMode(QFileDialog::AnyFile);
    dialog.setAcceptMode(QFileDialog::AcceptOpen);

    if (dialog.exec())
    {
        QMessageBox msgBox1;
        msgBox1.setText("Begin");
        msgBox1.exec();

        QFile file(dialog.selectedFiles()[0]);
        if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
        {
            QMessageBox msgBox;
            msgBox.setText("Fail");
            msgBox.exec();
            return;
        }


        QFileDialog dialogSave(this);
        dialogSave.setFileMode(QFileDialog::AnyFile);
        dialogSave.setAcceptMode(QFileDialog::AcceptSave);

        if (!dialogSave.exec())
        {
            return;
        }

        QByteArray saveArr;


        QFile saveFile(dialogSave.selectedFiles()[0]);
        if (!saveFile.open(QIODevice::WriteOnly | QIODevice::Truncate))
        {
            QMessageBox msgBox;
            msgBox.setText("Fail");
            msgBox.exec();
            return;
        }


        saveArr.push_back('B');
        saveArr.push_back('N');
        saveArr.push_back((char)0);
        saveArr.push_back((char)2);


        QTextStream in(&file);
        QString line;

        int boneCount;

        QString boneName;
        QString parentBoneName;

        float v[3];
        float q[4];

        float len;

        QStringList splited;

        std::map<QString, int> boneNameMap;

        //======= Go file parsing

        line = in.readLine();

        boneCount = line.remove("Bones ").toInt();

        std::vector<TBoneStruct> boneStructMap;

        for (int i=0; i<boneCount; i++)
        {

            TBoneStruct b;

        line = in.readLine();
        boneName = line;

        boneNameMap[boneName] = i;

        line = in.readLine();
        splited = line.split(" ", QString::SkipEmptyParts);

        b.v[0] = splited.at(0).toFloat()*0.1;
        b.v[1] = splited.at(2).toFloat()*0.1;
        b.v[2] = -splited.at(1).toFloat()*0.1;

        line = in.readLine();
        splited = line.split(" ", QString::SkipEmptyParts);

        b.q[0] = splited.at(0).toFloat();
        b.q[1] = splited.at(2).toFloat();
        b.q[2] = -splited.at(1).toFloat();
        b.q[3] = splited.at(3).toFloat();

        line = in.readLine();
        b.len = line.toFloat()*0.1;

        line = in.readLine();
        b.parentBoneName = line;

        boneStructMap.push_back(b);

        /*
        saveArr.insert(saveArr.size(), (char*)v, 12);
        saveArr.insert(saveArr.size(), (char*)q, 16);

        saveArr.insert(saveArr.size(), (char*)&len, 4);

        saveArr.insert(saveArr.size(), (char*)&len, 4);*/

        }



        line = in.readLine();

        int vertexCount = line.remove("Vertices ").toInt();

        saveArr.insert(saveArr.size(), (char*)&boneCount, 4);
        saveArr.insert(saveArr.size(), (char*)&vertexCount, 4);

        for (int i=0; i<boneCount; i++)
        {

            saveArr.insert(saveArr.size(), (char*)boneStructMap[i].v, 12);
            saveArr.insert(saveArr.size(), (char*)boneStructMap[i].q, 16);

            saveArr.insert(saveArr.size(), (char*)&boneStructMap[i].len, 4);

            int parentBoneIndex;
            if (boneNameMap.count(boneStructMap[i].parentBoneName) == 0)
            {
                parentBoneIndex = -1;
            }
            else
            {
               parentBoneIndex = boneNameMap[boneStructMap[i].parentBoneName];
            }
            saveArr.insert(saveArr.size(), (char*)&parentBoneIndex, 4);
        }

        int wcount;
        float weight;

        for (int i=0; i<vertexCount; i++)
        {
            line = in.readLine();
            wcount = line.remove("WCount ").toInt();

            saveArr.insert(saveArr.size(), (char*)&wcount, 4);


            for (int j=0; j<wcount; j++)
            {
                line = in.readLine();
                int lastSpacebarPos = line.lastIndexOf(" ");
                line[lastSpacebarPos] = '|';
                splited = line.split("|", QString::SkipEmptyParts);
                boneName = splited.at(0);
                weight = splited.at(1).toFloat();

                int boneIndex = boneNameMap[boneName];

                saveArr.insert(saveArr.size(), (char*)&boneIndex, 4);
                saveArr.insert(saveArr.size(), (char*)&weight, 4);

            }

        }


        /*
        line = in.readLine();

        int frameCount = line.remove("Frames ").toInt();

        line = in.readLine();

        int boneCount = line.remove("Bones ").toInt();

        QStringList splited;

        float v[3];
        float q[4];

        float len;

        for (int i=0; i<frameCount; i++)
        {

        line = in.readLine();

        for (int j=0; j<boneCount; j++)
        {

        line = in.readLine();
        splited = line.split(" ", QString::SkipEmptyParts);

        v[0] = splited.at(0).toFloat();
        v[1] = splited.at(1).toFloat();
        v[2] = splited.at(2).toFloat();

        line = in.readLine();
        splited = line.split(" ", QString::SkipEmptyParts);

        q[0] = splited.at(0).toFloat();
        q[1] = splited.at(1).toFloat();
        q[2] = splited.at(2).toFloat();
        q[3] = splited.at(3).toFloat();

        line = in.readLine();
        len = line.toFloat();
        }

        }*/

        saveFile.write(saveArr);

        //======= End file parsing

        QMessageBox msgBox;
        msgBox.setText("End");
        msgBox.exec();


    }
}
