extends Control

@export_multiline var description = ""
@export var icon: Texture
@export var plant: PackedScene
@export var hotkey: int
var t_cost: int = -1
var shown: bool = false

func _input(ev):
    var b = $MarginContainer/Button
    if ev is InputEventKey and ev.keycode == hotkey and !ev.pressed and !b.disabled:
        b._on_pressed()

func _ready():
    var p = plant.instantiate()
    description += "\n[color=#88FFFF]Root strength: " + str(p.max_hp) + "[/color]"
    description += "\n[color=#FF8888]Base damage: " + str(p.attack_damage) + "[/color]"
    description += "\n[color=#88FF88]Fire rate: " + str(p.attack_rate) + "s [/color]"
    description += "\n[color=#FFFF88]Cost: " + str(p.cost) + "[/color]"
    description += "[/font]"
    t_cost = p.cost
    find_child("Label").set_text("[center][font_size=18]???[/font_size][/center]")
    find_child("Button").set_button_icon(icon)
    updoot(19)

func updoot(money: int):
    if t_cost >= 0 and money >= t_cost:
        set_modulate(Color(1.0, 1.0, 1.0, 1.0))
        $MarginContainer/Button.set_disabled(false)
    else:
        set_modulate(Color(0.4, 0.4, 0.4, 1.0))
        $MarginContainer/Button.set_disabled(true)
        
    if t_cost >= 0 and shown == false and money >= t_cost - 10:
        find_child("Label").set_text(description)
        shown = true
