const Phaser = require('phaser');
const { POSITIONS: { MENU_MAIN }, TEXT, ITEMS: { SKILLS, SPECS, COLOURS } } = require('./constants');

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

const UNEQUIP_Y = Y + Math.floor(HEIGHT * 0.7);
const UNEQUIP_WIDTH = Math.floor(WIDTH * 0.4);
const UNEQUIP_HEIGHT = Math.floor(HEIGHT * 0.15);

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

    create(props) {
        const { item, cryp } = props;
        if (!item) return false;

        // Default item text
        let title = item;
        let description = 'Description missing';
        let upgrades = '';
        let colours = '';

        // Replace item text with constants if it exists
        if (SKILLS[item]) {
            title = `Skill Item - ${item}`;
            ({ description } = SKILLS[item]);
            if (SKILLS[item].upgrades) ({ upgrades } = SKILLS[item]);
            if (SKILLS[item].colours) ({ colours } = SKILLS[item]);
        } else if (SPECS[item]) {
            title = `Spec Item - ${item}`;
            ({ description } = SPECS[item]);
            if (SPECS[item].upgrades) ({ upgrades } = SPECS[item]);
        } else if (COLOURS[item]) {
            title = `Colour Item - ${item}`;
            ({ description } = COLOURS[item]);
            if (COLOURS[item].upgrades) ({ upgrades } = COLOURS[item]);
        }

        // Add text objects
        this.add.text(X, Y, title, TEXT.HEADER);
        this.add
            .text(X, Y + HEIGHT * 0.1, description, TEXT.NORMAL)
            .setWordWrapWidth(WIDTH * 0.75);
        this.add
            .text(X, Y + HEIGHT * 0.25, upgrades, TEXT.NORMAL)
            .setWordWrapWidth(WIDTH * 0.75);
        if (colours !== '') {
            this.add
                .text(X, Y + HEIGHT * 0.35, `Adds ${colours} to cryp`, TEXT.NORMAL)
                .setWordWrapWidth(WIDTH * 0.75);
        }

        if (!cryp) return true;
        const ws = this.registry.get('ws');
        const { vbox } = this.registry.get('player');
        const unEquip = this.add.rectangle(X, UNEQUIP_Y, UNEQUIP_WIDTH, UNEQUIP_HEIGHT, 0x222222)
            .setOrigin(0, 0)
            .setInteractive()
            .on('pointerdown', () => {
                ws.sendVboxUnequip(vbox.instance, cryp.id, item);
                this.registry.set('crypStats', cryp);
            });
        this.add.text(unEquip.getCenter().x, unEquip.getCenter().y, 'unequip', TEXT.HEADER)
            .setOrigin(0.5, 0.5);
        return true;
    }

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

module.exports = ItemInfo;
