import Phaser from 'phaser';

/**
 * Boutons directionnels gauche/droite pour mobile
 * Alternative au gyroscope si celui-ci n'est pas disponible
 */
export class DirectionalButtons {
    private scene: Phaser.Scene;
    private leftButton?: Phaser.GameObjects.Arc;
    private rightButton?: Phaser.GameObjects.Arc;
    private leftText?: Phaser.GameObjects.Text;
    private rightText?: Phaser.GameObjects.Text;

    private isLeftPressed: boolean = false;
    private isRightPressed: boolean = false;
    private isMobile: boolean = false;

    constructor(scene: Phaser.Scene) {
        this.scene = scene;
        this.detectDevice();

        // Ne créer les boutons QUE si on n'est PAS sur mobile
        if (!this.isMobile) {
            this.createButtons();
        } else {
            console.log('📱 Smartphone détecté → boutons directionnels désactivés (gyroscope utilisé)');
        }
    }

    /**
     * Détecte si on est sur mobile
     */
    private detectDevice(): void {
        const userAgent = navigator.userAgent.toLowerCase();
        this.isMobile = /android|webos|iphone|ipad|ipod|blackberry|iemobile|opera mini/i.test(userAgent);
        console.log('[DirectionalButtons] Mobile détecté?', this.isMobile);
    }

    /**
     * Crée les boutons gauche et droite
     */
    private createButtons(): void {
        const height = this.scene.cameras.main.height;
        const buttonSize = 80;
        const padding = 30;

        // Bouton GAUCHE
        this.leftButton = this.scene.add.circle(
            padding + buttonSize / 2,
            height - padding - buttonSize / 2,
            buttonSize / 2,
            0x4CAF50,
            0.6
        );
        this.leftButton.setStrokeStyle(4, 0xFFFFFF, 0.8);
        this.leftButton.setScrollFactor(0);
        this.leftButton.setDepth(1000);
        this.leftButton.setInteractive();

        this.leftText = this.scene.add.text(
            padding + buttonSize / 2,
            height - padding - buttonSize / 2,
            '←',
            {
                fontSize: '48px',
                color: '#FFFFFF',
                fontStyle: 'bold',
            }
        );
        this.leftText.setOrigin(0.5);
        this.leftText.setScrollFactor(0);
        this.leftText.setDepth(1001);

        // Bouton DROITE
        this.rightButton = this.scene.add.circle(
            padding * 2 + buttonSize * 1.5,
            height - padding - buttonSize / 2,
            buttonSize / 2,
            0x4CAF50,
            0.6
        );
        this.rightButton.setStrokeStyle(4, 0xFFFFFF, 0.8);
        this.rightButton.setScrollFactor(0);
        this.rightButton.setDepth(1000);
        this.rightButton.setInteractive();

        this.rightText = this.scene.add.text(
            padding * 2 + buttonSize * 1.5,
            height - padding - buttonSize / 2,
            '→',
            {
                fontSize: '48px',
                color: '#FFFFFF',
                fontStyle: 'bold',
            }
        );
        this.rightText.setOrigin(0.5);
        this.rightText.setScrollFactor(0);
        this.rightText.setDepth(1001);

        // Events pour le bouton GAUCHE
        this.leftButton.on('pointerdown', () => {
            this.isLeftPressed = true;
            this.leftButton?.setFillStyle(0x2E7D32, 0.9);
        });

        this.leftButton.on('pointerup', () => {
            this.isLeftPressed = false;
            this.leftButton?.setFillStyle(0x4CAF50, 0.6);
        });

        this.leftButton.on('pointerout', () => {
            this.isLeftPressed = false;
            this.leftButton?.setFillStyle(0x4CAF50, 0.6);
        });

        // Events pour le bouton DROITE
        this.rightButton.on('pointerdown', () => {
            this.isRightPressed = true;
            this.rightButton?.setFillStyle(0x2E7D32, 0.9);
        });

        this.rightButton.on('pointerup', () => {
            this.isRightPressed = false;
            this.rightButton?.setFillStyle(0x4CAF50, 0.6);
        });

        this.rightButton.on('pointerout', () => {
            this.isRightPressed = false;
            this.rightButton?.setFillStyle(0x4CAF50, 0.6);
        });

        console.log('✅ Boutons directionnels créés');
    }

    /**
     * Retourne la direction actuelle (-1, 0, 1)
     */
    public getDirection(): number {
        if (this.isLeftPressed && this.isRightPressed) {
            return 0; // Les deux = annuler
        }
        if (this.isLeftPressed) {
            return -1;
        }
        if (this.isRightPressed) {
            return 1;
        }
        return 0;
    }

    /**
     * Détruit les boutons
     */
    public destroy(): void {
        this.leftButton?.destroy();
        this.rightButton?.destroy();
        this.leftText?.destroy();
        this.rightText?.destroy();
    }
}
