extends ColorRect # TextureRect

var ZLEVELS = preload("res://src/ZLevels.gd")
var Spell_Icon = preload("res://src/Spell_Icon.gd")
var Sheep_Counter = preload("res://src/Sheep_Counter.gd")
var tex_sheep_live = preload("res://images/sheep_icon_live.png")
var tex_sheep_dead = preload("res://images/sheep_icon_dead.png")

var Spell_Fireball = preload("res://src/Spell_Fireball.gd")
var Spell_Ice = preload("res://src/Spell_Ice.gd")
var Spell_Storm = preload("res://src/Spell_Storm.gd")
var tex_fire = preload("res://images/fireball_icon.png")
var tex_ice = preload("res://images/ice_icon.png")
var tex_storm = preload("res://images/storm_icon.png")
var tex_expended = preload("res://images/expended_icon.png")

var spellIcons = []
var sheepLive
var sheepDead

func _ready():
#  print(z_index) # = ZLEVELS.UI
  set("z", ZLEVELS.UI)
  var world = $"/root/World"
  set_size(Vector2(world.minDim, get_viewport().size.y - world.minDim))
  set_position(Vector2(0, world.minDim))
  color = Color(0.3, 0.1, 0.1)
  var iconOffset = 4 #px
  var iconHeight = 48
  var iconWidth = 2 * iconHeight
  var spacer = 20
  for idx in range(4):
    var spellIcon = Spell_Icon.new( \
      Vector2(iconOffset + idx * (iconOffset + iconWidth), iconOffset), \
      Vector2(iconWidth, iconHeight))
    spellIcons.append(spellIcon)
    add_child(spellIcon)
    
  sheepLive = Sheep_Counter.new(Vector2(iconOffset + 4 * (iconOffset + iconWidth) + spacer, iconOffset), \
      Vector2(iconWidth, iconHeight))
  sheepLive.setImage(tex_sheep_live)
  add_child(sheepLive)
  
  sheepDead = Sheep_Counter.new(Vector2(iconOffset + 4 * (iconOffset + iconWidth) + spacer + 120 + iconOffset, iconOffset), \
      Vector2(iconWidth, iconHeight))
  sheepDead.setImage(tex_sheep_dead)
  add_child(sheepDead)
  
  var menuBtn = Button.new()
  menuBtn.set_text("Menu")
  menuBtn.set("custom_fonts/font", load("res://fonts/unlock_font_small.tres"))
  menuBtn.set_size(Vector2(iconWidth, iconHeight))
  menuBtn.set_position(Vector2(iconOffset + 4 * (iconOffset + iconWidth) + spacer + 120 + iconOffset + 120 + 20, 4))
  menuBtn.connect("pressed", self, "_menuPressed")
  add_child(menuBtn)

func _menuPressed():
  global.mutton = 0
  global.lambs = 5
  global.deaths = 0
  get_tree().change_scene("res://Menu.tscn")

func updateProgress(spellIdx, progress):
  # set casting progress bars.
  if spellIdx < 4:
    var spellIcon = spellIcons[spellIdx]
    spellIcon.setProgress(progress)
  elif spellIdx == 4: # sacrifice
    sheepLive.setSacrificeProgress(progress)
  else:
    print("Unknown spell in progress.")  
  
func setExpended(spellIdx):
  if spellIdx < 4:
    spellIcons[spellIdx].set_texture(tex_expended)
  elif spellIdx == 4:
    pass # We can always sacrifice!
  else:
    print("Unknown spell expended.")
    
func updateSpellTexture(spellIdx, spell):
  if not spell.expended:
    if spell is Spell_Fireball:
      spellIcons[spellIdx].set_texture(tex_fire)
    if spell is Spell_Ice:
      spellIcons[spellIdx].set_texture(tex_ice)
    if spell is Spell_Storm:
      spellIcons[spellIdx].set_texture(tex_storm)