import { computed, ref } from 'vue'
import { defineStore } from 'pinia'
import type { MatchSessionState, GameState, Movement, PlayerDto } from '@/common/interfaces'

export const useGameStore = defineStore('game', () => {
  const sessionState = ref<MatchSessionState | undefined>(undefined)
  const gameState = ref<GameState | undefined>(undefined)
  const playerState = ref<PlayerDto | undefined>(undefined)
  const canMakeMove = ref(false)
  const canSelectTile = ref(false)
  const gameFinished = ref(false)
  const readyForStart = ref(false)
  const moveToMake = ref<Movement | undefined>(undefined)
  const incomingFreeEnds = ref<[number, number] | undefined>(undefined)
  const showReadyButton = ref(false)

  const isSessionStarted = computed(() => sessionState.value !== undefined)

  function updateSessionState(newState: MatchSessionState) {
    sessionState.value = newState
  }

  function updateGameState(newState: GameState) {
    gameState.value = newState
  }

  function updatePlayerState(newState: PlayerDto) {
    playerState.value = newState
  }

  function updateCanMakeMove(value: boolean) {
    canMakeMove.value = value
  }

  function setMoveToMake(move: Movement) {
    moveToMake.value = move
  }

  function updateCanSelectTile(value: boolean) {
    canSelectTile.value = value
  }

  function setIncomingFreeEnds(freeEnds: [number, number]) {
    incomingFreeEnds.value = freeEnds
  }

  function setShowReadyButton(value: boolean) {
    showReadyButton.value = value
  }

  function setReadyForStart(value: boolean) {
    readyForStart.value = value
  }

  function updateGameFinished(value: boolean) {
    gameFinished.value = value
  }

  return {
    sessionState,
    gameState,
    playerState,
    canMakeMove,
    moveToMake,
    incomingFreeEnds,
    canSelectTile,
    showReadyButton,
    readyForStart,
    gameFinished,
    updateSessionState,
    updateGameState,
    updatePlayerState,
    updateCanMakeMove,
    setMoveToMake,
    setIncomingFreeEnds,
    updateCanSelectTile,
    setShowReadyButton,
    setReadyForStart,
    updateGameFinished,
    isSessionStarted
  }
})
