import * as Phaser from "phaser";
import Card from "../objects/card";
import GameButton from "../objects/button";

class MainMenuScene extends Phaser.Scene {
  constructor() {
    super('MainMenu')
  }

  preload() {
  }

  create() {
    const gameState = {
      cards: [],
      tablePositions: []
    }

    const { centerX, centerY, width, height } = this.cameras.main;
    const bg = this.add.image(centerX, centerY, 'background01');
    const cardsPerRow = 4;
    const numCards = cardsPerRow * cardsPerRow;

    const characters = ['akakabuto', 'ben', 'cross', 'gin', 'hyena', 'john', 'madara', 'mosa', 'riki', 'smith', 'sniper']

    for (let x = 0; x < cardsPerRow; x++) {
      for (let y = 0; y < cardsPerRow; y++) {
        const char = Math.floor(Math.random() * 100) % characters.length;
        const card = new Card(this, 80, 80, characters[char]);
        card.scaleTo(.4, .4)
        card.flip();
        card.onEvent('clicked', () => {
          card.flip()
        })
        gameState.cards.push(card);
        gameState.tablePositions.push({ x: x * 130 + 80, y: y * 130 + 160 })
      }
    }

    gameState.cards.forEach((card, idx) => {
      const pos = gameState.tablePositions[idx]
      card.moveTo(pos.x, pos.y)
    })

    this.input.on('gameobjectup', function (pointer, gameObject) {
      gameObject.emit('clicked', gameObject);
    }, this);

    //card.scaleTo(0.5, 0.5)
    //card.moveTo(400, 150);
    //card.flip()
  }

}

export default MainMenuScene;