
#include "Import.h"
#include "ImportAnsys.h"
#include "ImportMarc.h"
#include "ImportNastran.h"
#include "ImportRTMS.h"
#include "ImportGmesh.h"
#include "RTMSimulator.h"

#include <QApplication>
#include <QDesktopWidget>
#include <QString>
#include <QWidget>
#include <QHBoxLayout>
#include <QLabel>
#include <QFile>
#include <QTextStream>
#include <QMessageBox>
#include <QString>
#include <QDataStream>


/**
 * Creates a standardized pop up info box to display a message to the user.
 * @param message Message to be displayed
 */
InfoBox::InfoBox(QString message)
{
    setGeometry(0,0,200,50);
    QWidget* desktop = QApplication::desktop();
    int desktopWidth = desktop->width();
    int desktopHeight = desktop->height();
    move((desktopWidth-width())/2, (desktopHeight-height())/2);
    setWindowModality ( Qt::ApplicationModal );

    QHBoxLayout layout;
    setLayout(&layout);
    m_pLabel = new QLabel;
    layout.addWidget(m_pLabel);
    setText(message);
    setVisible(this);

}

/**
 * Changes the message displayed in this message box.
 * @param message New message to be displayed
 */
void InfoBox::setText(QString message)
{
    m_pLabel->setText("<html><b>" + message + "</b></html>");
}

/**
 * Opens the file specified in strFile and determines the type of file it is.
 * Then forwards a pointer to the file to the appropriate import handler.
 * @param strFile Fully qualified path to the file to be imported
 * @param pSimulator Pointer to the RTMSimulator object where the file should be imported to.
 */
Import::Import(QString strFile, RTMSimulator* pSimulator):
        m_pSimulator(pSimulator),
        m_numberOfNodes(0),
        m_numberOfElements(0)
{
    pSimulator->invalidatePermeabilityGlobals();
    QFile* pFile = new QFile(strFile);

    if(pFile->fileName().endsWith(".igs",Qt::CaseInsensitive) || pFile->fileName().endsWith(".iges",Qt::CaseInsensitive))
    {
        QString tempFileName = Gmsh::mesh(pFile);
        QFile* pTempFile = new QFile(tempFileName);
        pTempFile->open(QIODevice::ReadOnly);
        ImportGmesh gmesh(pTempFile, pSimulator);
        pTempFile->close();
        pTempFile->remove();
    }
    else if (pFile->open(QIODevice::ReadOnly))
    {
        while (!pFile->atEnd())
        {
            QString line = pFile->readLine();
            if(line.contains("$MeshFormat",Qt::CaseInsensitive))
            {
                pFile->reset();
                ImportGmesh gmesh(pFile, pSimulator);
                break;
            }
            else if(line.contains("MARC",Qt::CaseInsensitive))
            {
                pFile->reset();
                ImportNastran nastran(pFile, pSimulator);
                break;
            }
            else if(line.contains("NASTRAN",Qt::CaseInsensitive))
            {
                pFile->reset();
                ImportNastran nastran(pFile, pSimulator);
                break;
            }
            else if(line.contains("ANSYS",Qt::CaseInsensitive))
            {
                pFile->reset();
                ImportAnsys ansys(pFile, pSimulator);
                break;
            }
            else if(line.contains("RTMSimulatorScene",Qt::CaseInsensitive))
            {
                ImportRTMS::import(pFile,pSimulator);
                break;
            }
        }
        pFile->reset();
    }

    pFile->close();
}



