const Phaser = require('phaser');

const Header = require('./header');
const Home = require('./home');
const Menu = require('./menu');
const Combat = require('./combat');

// const Background = require('./background');

function renderCryps() {
    const config = {
        type: Phaser.CANVAS,
        // backgroundColor: '#181818',
        resolution: window.devicePixelRatio,
        scale: {
            mode: Phaser.Scale.FIT,
            width: Math.floor(window.innerHeight * 1.6),
            height: Math.floor(window.innerHeight),
            max: {
                width: Math.floor(window.innerHeight * 1.6),
                height: Math.floor(window.innerHeight),
            },

        },
        antialias: true,
        physics: {
            default: 'arcade',
            arcade: {
                debug: false,
                gravity: { y: 0 },
            },
        },
        scene: [
            // Background,
            Header,
        ],
    };

    const game = new Phaser.Game(config);


    function changeData(parent, key, data) {
        // Don't load other scenes if you're not logged in
        if (!game.registry.get('account')) return false;

        if (key === 'home') {
            if (data) return game.scene.add('Home', Home, true);
        }

        if (key === 'menu') {
            if (!data || game.registry.get('inMenu')) return false;
            if (data) return game.scene.add('Menu', Menu, true);
        }

        if (key === 'game') {
            if (!data || game.registry.get('inGame')) return false;
            return game.scene.add('Combat', Combat, true);
        }

        return true;
    }

    game.registry.events.on('changedata', changeData);
    game.registry.events.on('setdata', changeData);

    window.addEventListener('mouseup', () => game.registry.set('pan', false));
    window.addEventListener('mousedown', () => game.registry.set('pan', true));
    window.addEventListener('resize', () => {
        game.scale.displaySize.maxWidth = window.innerHeight * 1.6;
        game.scale.displaySize.maxHeight = window.innerHeight;
    });


    return game;
}

module.exports = renderCryps;
