extends Node

var castingTime
var timeCasted
var casting
var overlay
var progress
var spellSlot
var expended

func _init():
  expended = false
  progress = 0.0
  timeCasted = 0.0
  casting = false

func _ready():
  overlay = $"/root/World/UI/overlay"

func setSlot(idx):
  spellSlot = idx

func _process(delta):
  if casting:
    timeCasted += delta
    if timeCasted > castingTime or spellSlot == 4 and timeCasted > 0.5 * castingTime:
      evaluateCasting()
    if castingTime == 0.0:
      progress = 0.0
    else:
      progress = timeCasted / castingTime
    overlay.updateProgress(spellSlot, progress)

func startCasting():
  if not expended:
    casting = true
    return true # return that we're busy now
  return false
  
func endCasting():
  if casting:
    evaluateCasting()
    progress = 0.0
    overlay.updateProgress(spellSlot, progress)

func evaluateCasting():
    if progress < 0.5:
      if spellSlot == 4 and timeCasted > 0.5 * castingTime:
        effect2()
      else:
        effect1()
    else:
      effect2()
    timeCasted = 0.0
    if spellSlot != 4:
      expended = true
      overlay.setExpended(spellSlot)
    casting = false
    $"/root/World/ys/player".busy = false
    
func effect1():
  print("stub effect 1")
  
func effect2():
  print("stub effect 2")