const Phaser = require('phaser');
const { POSITIONS: { MENU_MAIN }, TEXT, SKILLS } = require('./constants');
const Item = require('./elements/item');

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

const SKILL_WIDTH = Math.floor(WIDTH / 10);


class DeleteHitBox extends Phaser.GameObjects.Rectangle {
    constructor(scene, x, y) {
        super(scene, x, y, SKILL_WIDTH, SKILL_WIDTH, 0x222222);
        this.setOrigin(0);
        this.itemSelect = () => this.setFillStyle(0xff0000);
        this.itemDeselect = () => this.setFillStyle(0x222222);
    }
}

const itemCheckHitbox = (scene, pointer) => {
    const { list } = scene.scene.get('MenuCrypList').children;
    const hitboxes = list.filter(c => c.cryp)
        .concat(scene.children.list.filter(c => c instanceof DeleteHitBox));

    let found;
    for (let i = 0; i < hitboxes.length; i += 1) {
        if (Phaser.Geom.Rectangle.ContainsPoint(hitboxes[i].getBounds(), pointer.position)) {
            found = hitboxes[i];
        } else {
            hitboxes[i].itemDeselect();
        }
    }

    return found;
};

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


    updateData(parent, key, data) {
        if (key === 'player') {
            console.log('grep');
            console.log(data);
            const cryp = data.cryps.find(c => c.id === this.cryp.id);
            this.scene.restart(cryp);
        }
    }

    create(cryp) {
        this.registry.events.on('changedata', this.updateData, this);
        // const ws = this.registry.get('ws');

        const player = this.registry.get('player');
        if (!player) return false;
        // const { vbox } = player;

        this.cryp = cryp;

        /*
        const del = this.add.existing(new DeleteHitBox(this, X + WIDTH * 0.7, Y + HEIGHT * 0.6));
        this.add.text(del.getCenter().x, del.getCenter().y, 'unequip', TEXT.HEADER)
            .setOrigin(0.5, 0.5);
        */

        this.add.text(X, Y, cryp.name, TEXT.HEADER);

        const crypStat = (stat, i) => {
            const STAT_X = X;
            const STAT_Y = Y + (i + 2) * TEXT_MARGIN;
            this.add.text(STAT_X, STAT_Y, `${stat.stat}: ${stat.base} -> ${stat.value}`, TEXT.NORMAL);
        };

        const CRYP_STATS = [
            cryp.hp,
            cryp.red_shield,
            cryp.blue_shield,
            cryp.evasion,
            cryp.red_damage,
            cryp.blue_damage,
            cryp.speed,
        ];

        CRYP_STATS.forEach(crypStat);
        /*
        const knownSkill = (skill, i) => {
            const SKILL_X = X + WIDTH * 0.4 + WIDTH * 0.125 * i;
            const SKILL_Y = Y + HEIGHT * 0.15;
            const itemObj = new Item(this, skill.skill, i, SKILL_X, SKILL_Y, SKILL_WIDTH, Math.floor(SKILL_WIDTH / 2));
            // itemObj.on('pointerdown', () => );
            this.input.setDraggable(itemObj);
            this.add.existing(itemObj);
        };
        cryp.skills.forEach(knownSkill);
        this.add.text(X + WIDTH * 0.35, Y, 'Skills', TEXT.HEADER);

        this.add.text(X + WIDTH * 0.35, Y + HEIGHT * 0.25, 'Specs', TEXT.HEADER);
        const knownSpec = (spec, i) => {
            const SKILL_X = X + WIDTH * 0.4 + WIDTH * 0.125 * i;
            const SKILL_Y = Y + HEIGHT * 0.4;
            const itemObj = new Item(this, spec, i, SKILL_X, SKILL_Y, SKILL_WIDTH, Math.floor(SKILL_WIDTH / 2));
            // itemObj.on('pointerdown', () => );
            this.input.setDraggable(itemObj);
            this.add.existing(itemObj);
        };
        cryp.specs.forEach(knownSpec);


        this.input.on('drag', (pointer, item, dragX, dragY) => {
            if (!(item instanceof Item)) return false;
            item.setPosition(dragX, dragY);
            const hitBox = itemCheckHitbox(this, pointer);
            if (hitBox) hitBox.itemSelect();
            return true;
        });

        this.input.on('dragend', (pointer, item) => {
            if (!(item instanceof Item)) return false;
            item.setPosition(item.origX, item.origY);
            const hitBox = itemCheckHitbox(this, pointer);
            if (hitBox) {
                hitBox.itemDeselect();
                // add socket function for unlearn here
                ws.sendVboxUnequip(vbox.instance, cryp.id, item.item);
            }
            return true;
        });
        */
        return this;
    }

    cleanUp() {
        this.registry.events.off('changedata', this.updateData, this);
        this.scene.remove();
    }
}

module.exports = StatSheet;
