package de.nowchess.backcore.game

import de.nowchess.api.player.{PlayerId, PlayerInfo}
import de.nowchess.chess.engine.GameEngine

import java.util.concurrent.atomic.AtomicReference

/** Singleton holder that bridges an externally-created GameEngine into the CDI context.
  *
  * All fields are `val` wrapping an AtomicReference. Set engine / white / black before starting Quarkus. In
  * standalone-server or test mode the defaults are used; GameService is created lazily (on first request), so the
  * values are already set by the time CDI constructs it.
  */
object GameEngineHolder:
  private val engineRef = new AtomicReference[GameEngine](new GameEngine())
  private val whiteRef  = new AtomicReference[PlayerInfo](PlayerInfo(PlayerId("white"), "White"))
  private val blackRef  = new AtomicReference[PlayerInfo](PlayerInfo(PlayerId("black"), "Black"))

  val gameId: String = GameId.generate()

  def engine: GameEngine            = engineRef.get()
  def engine_=(e: GameEngine): Unit = engineRef.set(e)

  def white: PlayerInfo            = whiteRef.get()
  def white_=(p: PlayerInfo): Unit = whiteRef.set(p)

  def black: PlayerInfo            = blackRef.get()
  def black_=(p: PlayerInfo): Unit = blackRef.set(p)
