#include <QProcess>
#include <QThread>
#include <unistd.h>

#include "about.h"
#include "macros.h"
#include "options.h"
#include "emuopt.h"
#include "qmc2main.h"
#include "gamelist.h"

#if QMC2_JOYSTICK == 1
#include <SDL/SDL.h>
#endif

#if defined(_SC_PHYS_PAGES) && defined(_SC_PAGESIZE) && defined(_SC_AVPHYS_PAGES)
#define QMC2_SHOWMEMINFO
#endif

// external global variables
extern MainWindow *qmc2MainWindow;
extern Gamelist *qmc2Gamelist;
extern EmulatorOptions *qmc2GlobalEmulatorOptions;

About::About(QWidget *parent)
  : QDialog(parent)
{
#ifdef QMC2_DEBUG
  qmc2MainWindow->log(QMC2_LOG_FRONTEND, "DEBUG: About::About(QWidget *parent = 0x" + QString::number((ulong)parent, 16) + ")");
#endif

  setupUi(this);

  widgetSize = QSize(-1, -1);
  widgetPosValid = FALSE;
  ignoreResizeAndMove = TRUE;

  adjustSize();
}

About::~About()
{
#ifdef QMC2_DEBUG
  qmc2MainWindow->log(QMC2_LOG_FRONTEND, "DEBUG: About::~About()");
#endif

}

void About::showEvent(QShowEvent *e)
{
#ifdef QMC2_DEBUG
  qmc2MainWindow->log(QMC2_LOG_FRONTEND, "DEBUG: About::showEvent(QShowEvent *e = 0x" + QString::number((ulong)e, 16) + ")");
#endif

  ignoreResizeAndMove = TRUE;

#if defined(QMC2_SHOWMEMINFO)
  // get memory information
  quint64 numPages, pageSize, freePages, totalSize, totalUsed, totalFree;
  numPages = sysconf(_SC_PHYS_PAGES) / 1024;
  pageSize = sysconf(_SC_PAGESIZE) / 1024;
  freePages = sysconf( _SC_AVPHYS_PAGES)/1024;
  totalSize = numPages * pageSize;
  totalFree = pageSize * freePages;
  totalUsed = totalSize - totalFree;
#endif

#if QMC2_JOYSTICK == 1
  const SDL_version *sdlVersionLinked = SDL_Linked_Version();
  SDL_version sdlVersionCompile;
  SDL_VERSION(&sdlVersionCompile);
#endif

  int numLogicalCores = QThread::idealThreadCount();

  QString titleString =
#if defined(QMC2_SDLMAME)
          "<p><font size=\"+1\"><b>QMC2 - M.A.M.E. Catalog / Launcher II</b></font><br>" +
#elif defined(QMC2_SDLMESS)
          "<p><font size=\"+1\"><b>QMC2 - M.E.S.S. Catalog / Launcher II</b></font><br>" +
#endif
          tr("Qt 4 based UNIX multi-emulator frontend") + "<br>" +
          tr("Version ") + QString::number(MAJOR) + "." + QString::number(MINOR) +
#ifdef BETA
          ".b" + QString::number(BETA) +
#endif
          ", " + tr("built for") + 
#if defined(QMC2_SDLMAME)
          " SDLMAME" +
#elif defined(QMC2_SDLMESS)
          " SDLMESS" +
#else
          " ???" +
#endif
          "<br>" + tr("Copyright") + " &copy; 2006 - 2009 R. Reucher, " + tr("Germany") + "</p>";
  labelTitle->setText(titleString);

  QString projectInfoString =
          "<p><b>" + tr("Project homepage:") + "</b><br>http://qmc2.arcadehits.net/</p>" +
          "<p><b>" + tr("Development site:") + "</b><br>http://sourceforge.net/projects/qmc2</p>" +
          "<p><b>" + tr("QMC2 development mailing list:") + "</b><br>qmc2-devel@lists.sourceforge.net (" + tr("subscribers only") +")</p>" +
          "<p><b>" + tr("List subscription:") + "</b><br>https://lists.sourceforge.net/lists/listinfo/qmc2-devel</p>";
  labelProjectInfo->setText(projectInfoString);

  QString sysInfoString =
          "<p><b>" + tr("Build OS:") + "</b><br>" + XSTR(TARGET_OS_NAME) + " " + XSTR(TARGET_OS_RELEASE) + " " + XSTR(TARGET_MACHINE) + "</p>" +
          "<p><b>" + tr("Emulator version:") + "</b><br>" + qmc2Gamelist->emulatorVersion + "</p>" +
          "<p><b>" + tr("Template information:") + "</b><br>" + tr("Emulator:") + " " + qmc2GlobalEmulatorOptions->templateEmulator + "<br>" + tr("Version:") + " " + qmc2GlobalEmulatorOptions->templateVersion + "<br>" + tr("Format:") + " " + qmc2GlobalEmulatorOptions->templateFormat + "</p>" +
          "<p><b>" + tr("Qt version:") + "</b><br>" + tr("Compile-time:") + " " + QT_VERSION_STR + "<br>" + tr("Run-time:") + " " + qVersion() + "</p>" +
#if QMC2_JOYSTICK == 1
          "<p><b>" + tr("SDL version:") + "</b><br>" + tr("Compile-time:") + " " + QString("%1.%2.%3").arg(sdlVersionCompile.major).arg(sdlVersionCompile.minor).arg(sdlVersionCompile.patch) + "<br>" + tr("Run-time:") + " " + QString("%1.%2.%3").arg(sdlVersionLinked->major).arg(sdlVersionLinked->minor).arg(sdlVersionLinked->patch) + "</p>" +
#endif
#if defined(QMC2_SHOWMEMINFO)
          "<p><b>" + tr("Physical memory:") + "</b><br>" + tr("Total: %1 MB").arg(totalSize) + "<br>" + tr("Free: %1 MB").arg(totalFree) + "<br>" + tr("Used: %1 MB").arg(totalUsed) + "</p>"
#endif
          "<p><b>" + tr("Number of CPUs:") + "</b><br>" + (numLogicalCores != -1 ? QString("%1").arg(numLogicalCores) : tr("unknown")) + "</p>"
          "<p><b>" + tr("Environment variables:") + "</b>";
  QStringList envVars = QProcess::systemEnvironment();
  envVars.sort();
  foreach (QString ev, envVars)
    sysInfoString += "<br>" + ev;
  sysInfoString += "</p>";
  textBrowserSystemInformation->setText(sysInfoString);

  // ensure a black background for the logo image
  labelLogoPixmap->setPalette(QPalette(QColor(0, 0, 0)));

  if ( widgetSize.isValid() )
    resize(widgetSize);
  if ( widgetPosValid )
    move(widgetPos);

  ignoreResizeAndMove = FALSE;

  e->accept();
}

void About::resizeEvent(QResizeEvent *e)
{
#ifdef QMC2_DEBUG
  qmc2MainWindow->log(QMC2_LOG_FRONTEND, "DEBUG: About::resizeEvent(QResizeEvent *e = 0x" + QString::number((ulong)e, 16) + ")");
#endif

  if ( !ignoreResizeAndMove )
    widgetSize = size();

  e->accept();
}

void About::moveEvent(QMoveEvent *e)
{
#ifdef QMC2_DEBUG
  qmc2MainWindow->log(QMC2_LOG_FRONTEND, "DEBUG: About::moveEvent(QMoveEvent *e = 0x" + QString::number((ulong)e, 16) + ")");
#endif

  if ( !ignoreResizeAndMove ) {
    widgetPos = pos();
    widgetPosValid = TRUE;
  }

  e->accept();
}
