<template>
  <Container ref="characterContainer" :x="currentPositionX" :y="currentPositionY" :depth="isometricDepth">
    <ChatBubble :mapCharacter="props.mapCharacter" />
    <HealthBar :mapCharacter="props.mapCharacter" />
    <CharacterHair :mapCharacter="props.mapCharacter" />
    <Sprite ref="characterSprite" :origin-y="1" :flipX="isFlippedX" />
  </Container>
</template>

<script lang="ts" setup>
import { Direction } from '@/application/enums'
import { type MapCharacter } from '@/application/types'
import CharacterHair from '@/components/game/character/partials/CharacterHair.vue'
import ChatBubble from '@/components/game/character/partials/ChatBubble.vue'
import HealthBar from '@/components/game/character/partials/HealthBar.vue'
import { useCharacterSprite } from '@/composables/useCharacterSpriteComposable'
import { useGameStore } from '@/stores/gameStore'
import { useMapStore } from '@/stores/mapStore'
import { Container, Sprite, useScene } from 'phavuer'
import { onMounted, onUnmounted, watch } from 'vue'

const props = defineProps<{
  tilemap: Phaser.Tilemaps.Tilemap
  mapCharacter: MapCharacter
}>()

const gameStore = useGameStore()
const mapStore = useMapStore()
const scene = useScene()

const { characterContainer, characterSprite, currentPositionX, currentPositionY, isometricDepth, isFlippedX, updatePosition, calcDirection, updateSprite, initializeSprite, cleanup } = useCharacterSprite(scene, props.tilemap, props.mapCharacter)

const handlePositionUpdate = (newValues: any, oldValues: any) => {
  if (!newValues) return

  if (!oldValues || newValues.positionX !== oldValues.positionX || newValues.positionY !== oldValues.positionY) {
    const direction = !oldValues ? Direction.POSITIVE : calcDirection(oldValues.positionX, oldValues.positionY, newValues.positionX, newValues.positionY)
    updatePosition(newValues.positionX, newValues.positionY, direction)
  }

  if (newValues.isMoving !== oldValues?.isMoving || newValues.rotation !== oldValues?.rotation) {
    updateSprite()
  }
}

watch(
  () => ({
    positionX: props.mapCharacter.character.positionX,
    positionY: props.mapCharacter.character.positionY,
    isMoving: props.mapCharacter.isMoving,
    rotation: props.mapCharacter.character.rotation,
    isAttacking: props.mapCharacter.isAttacking
  }),
  handlePositionUpdate
)

onMounted(async () => {
  await initializeSprite()

  if (props.mapCharacter.character.id === gameStore.character!.id) {
    mapStore.setCharacterLoaded(true)
    scene.cameras.main.startFollow(characterContainer.value as Phaser.GameObjects.Container)
  }
})

onUnmounted(() => {
  cleanup()
})
</script>
