import { ExtendedCharacter, TSocket } from '../utilities/types'
import { Zone } from '@prisma/client'
import prisma from '../utilities/prisma'

class CharacterManager {
  private characters!: ExtendedCharacter[]

  public async boot() {
    this.characters = []
  }

  public initCharacter(character: ExtendedCharacter) {
    this.characters = [...this.characters, character]
  }

  public async removeCharacter(character: ExtendedCharacter) {
    await prisma.character.update({
      where: { id: character.id },
      data: {
        positionX: character.positionX,
        positionY: character.positionY,
        rotation: character.rotation,
        zoneId: character.zoneId
      }
    })
    this.characters = this.characters.filter((x) => x.id !== character.id)
  }

  public getCharacterFromSocket(socket: TSocket) {
    return this.characters.find((x) => x.id === socket?.characterId)
  }

  public hasResetMovement(character: ExtendedCharacter) {
    return this.characters.find((x) => x.id === character.id)?.resetMovement
  }

  public getCharactersInZone(zone: Zone) {
    return this.characters.filter((x) => x.zoneId === zone.id)
  }
}

export default new CharacterManager()