import { Scene } from 'phaser';

export class BootScene extends Scene {
  constructor() {
    super({ key: 'BootScene' });
  }

  preload() {
    // New individual sprites
    this.load.image('player_idle', 'assets/sprites/player_idle.png');
    this.load.image('player_dead', 'assets/sprites/player_dead.png');
    this.load.image('player_propeller', 'assets/sprites/player_propeller.png');
    this.load.image('propeller_hat', 'assets/sprites/propeller_hat.png');
    this.load.image('rocket', 'assets/sprites/rocket.png');
    this.load.image('player_rocket', 'assets/sprites/player_rocket.png');
    this.load.image('enemy_bug', 'assets/sprites/enemy_bug.png');
  }

  create() {
    this.createBackgrounds();
    this.scene.start('MenuScene');
  }

  createBackgrounds() {
    const gridW = 512;
    const gridH = 512;
    const gridGraphics = this.make.graphics({ x: 0, y: 0, add: false });

    // Deep purple background
    gridGraphics.fillStyle(0x0f001f, 1);
    gridGraphics.fillRect(0, 0, gridW, gridH);

    // Grid lines
    gridGraphics.lineStyle(1, 0x2e0059, 0.35);
    for (let i = 0; i <= gridW; i += 32) {
      gridGraphics.moveTo(i, 0);
      gridGraphics.lineTo(i, gridH);
    }
    for (let i = 0; i <= gridH; i += 32) {
      gridGraphics.moveTo(0, i);
      gridGraphics.lineTo(gridW, i);
    }
    gridGraphics.strokePath();

    // Some accent hexagons / blockchain nodes
    gridGraphics.lineStyle(1, 0x581c87, 0.2);
    for (let i = 0; i < 8; i++) {
      const hx = Phaser.Math.Between(20, gridW - 20);
      const hy = Phaser.Math.Between(20, gridH - 20);
      const size = Phaser.Math.Between(6, 14);
      gridGraphics.strokeCircle(hx, hy, size);
    }

    gridGraphics.generateTexture('gridBg', gridW, gridH);

    // Star / particle texture
    const sGfx = this.make.graphics({ x: 0, y: 0, add: false });
    sGfx.fillStyle(0xffffff, 0.9);
    sGfx.fillCircle(2, 2, 2);
    sGfx.generateTexture('star', 4, 4);

    // Gwei particle
    const gGfx = this.make.graphics({ x: 0, y: 0, add: false });
    gGfx.fillStyle(0xa855f7, 1);
    gGfx.fillRect(0, 0, 5, 5);
    gGfx.generateTexture('gwei', 5, 5);

    // Platform texture — cartoon 3D block inspired by concept art
    const pw = 110;
    const ph = 32;
    const pGfx = this.make.graphics({ x: 0, y: 0, add: false });

    // Drop shadow
    pGfx.fillStyle(0x000000, 0.25);
    pGfx.fillRoundedRect(3, 4, pw, ph, 10);

    // Bottom / side face (darker purple for 3D look)
    pGfx.fillStyle(0x5b21b6, 1);
    pGfx.fillRoundedRect(0, 4, pw, ph - 4, 10);

    // Top face (main purple)
    pGfx.fillStyle(0x8b5cf6, 1);
    pGfx.fillRoundedRect(0, 0, pw, ph - 6, 10);

    // Thick cartoon outline
    pGfx.lineStyle(3, 0x1e1b4b, 1);
    pGfx.strokeRoundedRect(0, 0, pw, ph - 6, 10);

    // Highlight on top edge
    pGfx.fillStyle(0xffffff, 0.35);
    pGfx.fillRoundedRect(5, 2, pw - 10, 6, 4);

    // Subtle edge line separating top and side faces
    pGfx.lineStyle(2, 0x6d28d9, 0.8);
    pGfx.beginPath();
    pGfx.moveTo(2, ph - 6);
    pGfx.lineTo(pw - 2, ph - 6);
    pGfx.strokePath();

    pGfx.generateTexture('platform', pw, ph);

    // Spring texture (procedural)
    const springGfx = this.make.graphics({ x: 0, y: 0, add: false });
    springGfx.lineStyle(3, 0xffd700, 1);
    springGfx.beginPath();
    for (let i = 0; i < 5; i++) {
      springGfx.moveTo(5, i * 6);
      springGfx.lineTo(15, i * 6 + 3);
      springGfx.moveTo(15, i * 6 + 3);
      springGfx.lineTo(5, i * 6 + 6);
    }
    springGfx.strokePath();
    springGfx.generateTexture('spring', 20, 32);
  }
}
