const Phaser = require('phaser');
const { POSITIONS: { HOME_MAIN }, TEXT } = require('./constants');

const X = HOME_MAIN.x();
const Y = HOME_MAIN.y();
const WIDTH = HOME_MAIN.width();
const HEIGHT = HOME_MAIN.height();

const NULL_UUID = '00000000-0000-0000-0000-000000000000';

class HomeRankings extends Phaser.Scene {
    constructor() {
        super({ key: 'HomeInstances' });
    }

    create() {
        this.add.text(X, Y, 'Instances Scene', TEXT.HEADER);
        const playerList = this.registry.get('playerList');
        const addInstance = (player, i) => {
            const joinNormal = this.add
                .rectangle(X, Y + HEIGHT * 0.15 * (i + 1), WIDTH * 0.5, HEIGHT * 0.1, 0x888888)
                .setInteractive()
                .setOrigin(0)
                .on('pointerdown', () => {
                    if (player.ready) {
                        this.registry.set('player', player);
                        this.registry.get('ws').sendInstanceReady(player.instance);
                    } else {
                        this.game.events.emit('SET_PLAYER', player);
                        this.registry.get('ws').sendInstanceScores(player.instance);
                    }
                });
            const name = player.instance === NULL_UUID ? 'Normal Mode' : `${player.instance.substring(0, 5)}`;
            const disp = player.ready ? name.concat(' - in progress') : name;
            this.add
                .text(joinNormal.getCenter().x, joinNormal.getCenter().y, disp, TEXT.NORMAL)
                .setOrigin(0.5, 0.5);
        };

        playerList.forEach(addInstance);
    }

    cleanUp() {
        this.scene.remove();
    }
}

module.exports = HomeRankings;
