class_name EntityInteractableArea extends Area3D
const ItemAction = preload("res://cutscene/item/ItemAction.gd")

@export var entity:Entity

func isInteractable() -> bool:
  if !entity:
    return false

  match entity.interactType:
    Entity.InteractType.NONE:
      return false
    Entity.InteractType.CONVERSATION:
      return entity.dialogueBasePath != ""
    Entity.InteractType.CUTSCENE:
      return entity.cutscene != null and entity.cutscene.canRun()
    Entity.InteractType.ONE_TIME_ITEM:
      return (
        entity.oneTimeItem != null
        and entity.oneTimeItem.quantity > 0
        and entity.oneTimeItem.item != Item.Id.NULL
      )
    Entity.InteractType.BATTLE_TEST:
      return true

  return false

func _onConversationInteract(_other:Entity) -> void:
  assert(entity.dialogueBasePath != "")
  var cutscene:Cutscene = Cutscene.new()
  cutscene.addCallable(DialogueAction.getDialogueCallable(
    entity.dialogueBasePath,
    entity.dialogueTitle,
    [entity],
    DialogueAction.DialogueMode.CONVERSATION
  ))
  cutscene.start()

func _onItemInteract(_other:Entity) -> void:
  assert(entity.oneTimeItem != null)
  var cutscene:Cutscene = Cutscene.new()
  cutscene.addCallable(ItemAction.getItemCallable(entity.oneTimeItem.toItemStack()))
  await cutscene.start()
  entity.queue_free()

func onInteract(other:Entity) -> void:
  match entity.interactType:
    Entity.InteractType.NONE:
      return
    Entity.InteractType.CONVERSATION:
      _onConversationInteract(other)
    Entity.InteractType.ONE_TIME_ITEM:
      _onItemInteract(other)
    Entity.InteractType.CUTSCENE:
      var cutscene:Cutscene = Cutscene.new()
      entity.cutscene.queue(cutscene)
      cutscene.start()
    Entity.InteractType.BATTLE_TEST:
      var testEnemy = BattleFighter.new({
        'controller': BattleFighter.FighterController.AI
      })
      var cutscene:Cutscene = Cutscene.new()
      cutscene.addCallable(BattleStartAction.getStartBattleCallable({
        BATTLE.BattlePosition.RIGHT_MIDDLE_FRONT: PartySingleton.PARTY_JOHN,
        BATTLE.BattlePosition.LEFT_MIDDLE_FRONT: testEnemy
      }))
      cutscene.start()
