import { Container, Assets, Sprite, Text } from 'pixi';

export default class PlayerTag extends Container
{
    constructor(playerId, anchorX = 0)
    {
        super();

        this.playerId = playerId;
        this.anchorX = anchorX;

        this.addButtonPadding = 10;
        this._wins = 0;
    }

    async init()
    {
        // Player label
        this.playerLabel = new Text({
            text: 'Player ' + this.playerId,
            style: {
                fill: '#ffffff',
                fontSize: 18,
                fontFamily: 'EraserDust',
            },
        });

        this.addChild(this.playerLabel);
        this.playerLabel.anchor.set(this.anchorX, 0);

        // Player name
        this.nameText = new Text({
            text: '',
            style: {
                fill: '#ffffff',
                fontSize: 24,
                fontFamily: 'EraserDust',
            },
        });

        this.addChild(this.nameText);
        this.nameText.y = this.playerLabel.y + this.playerLabel.height;
        this.nameText.anchor.set(this.anchorX, 0);

        // Player wins
        this.winsText = new Text({
            text: 'Wins: 0',
            style: {
                fill: '#ffffff',
                fontSize: 18,
                fontFamily: 'EraserDust',
            },
        });

        this.addChild(this.winsText);
        this.winsText.y = this.nameText.y + this.nameText.height;
        this.winsText.anchor.set(this.anchorX, 0);

        // Add Buddy button
        const texture = await Assets.load('assets/images/game/add-button.png');
        this.addButton = new Sprite(texture);

        this.addChild(this.addButton);
        this.addButton.y = this.winsText.y + this.winsText.height + this.addButtonPadding;
        this.addButton.anchor.set(this.anchorX, 0);
        this.addButton.scale = .5;
        this.addButton.interactive = true;
        this.addButton.buttonMode = true;
        this.addButton.cursor = 'pointer';

        // Add event listeners
        // Note that pointerdown event listener is added by parent container
        this.addButton.on('pointerover', () => {
            this.addButton.alpha = 0.8;
        });

        this.addButton.on('pointerout', () => {
            this.addButton.alpha = 1;
        });
    }

    get userName()
    {
        return this._userName;
    }

    set userName(value)
    {
        this._userName = value;

        // Show user name
        this.nameText.text = value;
    }

    get wins()
    {
        return this._wins;
    }

    set wins(value)
    {
        this._wins = value;

        // Show wins
        this.winsText.text = 'Wins: ' + value;
    }

    showAddButton(show)
    {
        this.addButton.visible = show;
    }

    getAddButton()
    {
        return this.addButton;
    }

    get maxHeight()
    {
        let maxHeight = 0;

        maxHeight += this.playerLabel.height;
        maxHeight += this.nameText.height;
        maxHeight += this.winsText.height;
        maxHeight += this.addButton.height + this.addButtonPadding;

        return maxHeight;
    }

    get currentHeight()
    {
        let currentHeight = 0;

        if (this.visible)
        {
            currentHeight += this.playerLabel.height;
            currentHeight += this.nameText.height;
            currentHeight += this.winsText.height;
            currentHeight += this.addButton.visible ? this.addButton.height + this.addButtonPadding : 0;
        }

        return currentHeight;
    }
}