import { Game } from 'phaser';

import Boot from './scenes/Boot';
import Preloader from './scenes/Preloader';
import StartPosition from './plugins/StartPosition';
import SimplePlatformerControls from './plugins/SimplePlatformerControls';
import MazePlugin from './plugins/MazePlugin';
import MazeLevel from './scenes/MazeLevel';

window.fadeColor = { r: 5, g: 4, b: 4 };

// this controls how much the pixels are scaled, try changing it and have fun
window.maxSize = 960;
// window.maxSize = 640;
// window.maxSize = 320;

const StartGame = (parent: string) => {
    const parentElem = document.getElementById(parent);
    const grandParent = parentElem?.parentElement;
    const grandParentWidth = grandParent?.clientWidth;
    const grandParentHeight = grandParent?.clientHeight;
    if (!grandParentWidth || !grandParentHeight) {
        return;
    }

    const longestSide = Math.max(grandParentWidth, grandParentHeight);
    const zoom = 2 * Math.max(1, Math.floor(longestSide / window.maxSize));

    const config: Phaser.Types.Core.GameConfig = {
        type: Phaser.AUTO,
        parent: 'phaser-game',
        width: grandParentWidth / zoom,
        height: grandParentHeight / zoom,
        pixelArt: true,
        transparent: true,
        zoom: zoom,
        physics: {
            default: 'arcade',
            arcade: {
                gravity: { x: 0, y: 0 },
                debug: false
            }
        },
        plugins: {
            scene: [
                { key: 'simplePlatformerControls', plugin: SimplePlatformerControls, mapping: 'controls' },
                { key: 'startPosition', plugin: StartPosition, mapping: 'startPosition', start: true },
                { key: 'MazePlugin', plugin: MazePlugin, mapping: 'maze', start: true }
            ],
            global: [
            ]
        },
        input: {
            gamepad: true
        },
        scene: [
            Boot,
            Preloader,
            MazeLevel
        ]
    };

    return new Game({ ...config, parent });
}

export default StartGame;
