import {
  QFont,
  QFontDatabase,
  QMainWindow, WidgetEventTypes
} from '@nodegui/nodegui';
import { APPLICATION_NAME, FONT_FAMILY, FONT_LOCATION, FONT_SIZE } from '../Constants.js';
import { ProcessManager } from '../ProcessManager.js';
import { LoadingView } from './LoadingView.js';
import { RequestReloadPopup } from './RequestReloadPopup.js';
import { View } from './View.js';
export { GameView } from './GameView.js';
export { Popup } from './Popup.js';

export const win = new QMainWindow();
win.setFixedSize(800, 600);
win.setWindowTitle(APPLICATION_NAME);
win.show();
(global as any).win = win;
win.addEventListener(WidgetEventTypes.Paint, _ => _);
win.addEventListener('customContextMenuRequested', console.log);
win.addEventListener('objectNameChanged', console.log);
win.addEventListener('windowIconChanged', console.log);
win.addEventListener('windowTitleChanged', console.log);
win.addEventListener(
  WidgetEventTypes.Close,
  () => ProcessManager.quit()
);

// slightly broken idk
// const fontId = QFontDatabase.addApplicationFont('src/assets/font/bitmap/MxPlus_IBM_VGA_8x16.ttf')
// const fontId = QFontDatabase.addApplicationFont('src/assets/font/bitmap/MxPlus_ToshibaSat_8x16.ttf')
// const fontId = QFontDatabase.addApplicationFont(FONT_LOCATION);
// win.setStyleSheet(`
//   * {
//     font-family: '${FONT_FAMILY}';
//     font-size: ${FONT_SIZE}px;
//   }
// `);



setView(new LoadingView());

export function start() {

}

export function stop() {
  win.close();
}

export function setView(view: View) {
  view.addComponents();
}

export function setTitle(title: string) {

}

export function update() {

}

export function isStarted() {
  return true;
  return win.isVisible();
}

ProcessManager.on('reload', () => {
  RequestReloadPopup.show();
});


// HACK this is bullshit, :)
function f() {
  // win.repaint();
  win.update();
  setTimeout(f, 0);
}
f();

