const Phaser = require('phaser');

// Scenes constantly showing
const MenuCrypList = require('./menu.cryps.list');
const MenuNavigation = require('./menu.navigation');
const ItemList = require('./item.list');
// Scenes which change depending on menu context
const Zones = require('./zones');
const GameList = require('./game.list');
const StatSheet = require('./statsheet');
const ItemInfo = require('./item.info');

const FIXED_MENU_SCENES = [
    'MenuCrypList',
    'MenuNavigation',
    'ItemList',
];

const MAIN_MENU_SCENES = [
    'Zones',
    'GameList',
    'StatSheet',
    'ItemInfo',
];

class Menu extends Phaser.Scene {
    constructor() {
        super({ key: 'Menu', active: true });
    }

    create() {
        this.registry.events.on('changedata', this.updateData, this);
        this.registry.events.on('setdata', this.updateData, this);

        // Request the latest player state when we load the scene
        const player = this.registry.get('player');
        this.registry.get('ws').sendPlayerState(player.instance);
        // When we load the menu request the latest items
        // Item list will restart when the data comes in
        this.scene.manager.add('MenuCrypList', MenuCrypList, true);
        this.scene.manager.add('MenuNavigation', MenuNavigation, true);
        this.scene.manager.add('ItemList', ItemList, true);
        this.registry.set('inMenu', true);

        return true;
    }

    updateData(parent, key, data) {
        if (!data) return false;
        // Controls which scene shows in the main top right section
        switch (key) {
            case 'game': return this.cleanUp();
            case 'home': return this.cleanUp();
            case 'zone': return this.newMainScene('Zones', Zones, data);
            case 'gameList': return this.newMainScene('GameList', GameList, data);
            case 'crypStats': return this.newMainScene('StatSheet', StatSheet, data);
            case 'itemInfo': return this.newMainScene('ItemInfo', ItemInfo, data);
            default: return false;
        }
    }

    newMainScene(key, scene, data) {
        let addScene = true;
        // List of scenes which could be occupying the main section of the menu
        MAIN_MENU_SCENES.forEach((sKey) => {
            if (this.scene.manager.keys[sKey]) {
                if (key === sKey) {
                    // If there is new data for the current scene restart
                    this.scene.manager.keys[sKey].scene.restart(data);
                    addScene = false;
                } else {
                    // Delete the old scene
                    this.scene.manager.keys[sKey].cleanUp();
                }
            }
        });
        if (addScene) this.scene.manager.add(key, scene, true, data);
    }

    cleanUp() {
        this.registry.events.off('changedata', this.updateData, this);
        this.registry.events.off('setdata', this.updateData, this);
        // Delete scenes which could be showing before switching to battle scene
        const removeScenes = (sKey) => {
            if (this.scene.get(sKey)) this.scene.get(sKey).cleanUp();
        };
        FIXED_MENU_SCENES.forEach(removeScenes);
        MAIN_MENU_SCENES.forEach(removeScenes);
        this.registry.set('inMenu', false);
        this.combinerItems = this.registry.set('combinerItems', null);


        this.scene.remove();
    }
}

module.exports = Menu;
