extends PanelContainer

@export var buff_data: GeneratorBuffData

@onready var _label: Label = $HBoxContainer/Label
@onready var _button: Button = $HBoxContainer/Button

var _current_level: int = 0


func _ready() -> void:
	if buff_data != null:
		_label.text = buff_data.text if not buff_data.text.is_empty() else str(buff_data.id)
		_button.text = "Buy"
		_button.disabled = true
	else:
		_label.text = "No Buff"
		_button.text = "Disabled"
		_button.disabled = true
	
	_button.pressed.connect(_on_button_pressed)
	GameState.buff_level_changed.connect(_on_buff_level_changed)
	GameState.buff_unlocked_changed.connect(_on_buff_unlocked_changed)
	GameState.currency_changed.connect(_on_currency_changed)
	_update_ui()


func _on_button_pressed() -> void:
	if buff_data == null:
		return
	
	var cost_currency_id: StringName = _resolve_buff_cost_currency_id(buff_data)
	if cost_currency_id == &"":
		push_warning("test_buff: Invalid cost currency")
		return
	
	var cost: BigNumber = buff_data.get_cost_for_level(_current_level)
	if GameState.spend_currency_by_id(cost_currency_id, cost):
		var next_level: int = _current_level + 1
		GameState.set_buff_level(buff_data.id, next_level)
		_apply_buff_effect(buff_data, next_level)
		_current_level = next_level
		_update_ui()
	else:
		push_warning("test_buff: Not enough currency to buy buff")


func _apply_buff_effect(buff: GeneratorBuffData, level: int) -> void:
	if buff.kind != GeneratorBuffData.BuffKind.RESOURCE_PURCHASE:
		return
	
	var target_currency_id: StringName = _resolve_buff_target_currency_id(buff)
	if target_currency_id == &"":
		return
	
	var purchased_amount: BigNumber = buff.get_resource_purchase_amount_for_level(level)
	if purchased_amount.mantissa > 0.0:
		GameState.add_currency_by_id(target_currency_id, purchased_amount)


func _resolve_buff_target_currency_id(buff: GeneratorBuffData) -> StringName:
	if buff.resource_target_currency == null:
		return &""
	
	return CurrencyDatabase.get_currency_id(buff.resource_target_currency)


func _on_buff_level_changed(buff_id: StringName, new_level: int) -> void:
	if buff_data != null and buff_id == buff_data.id:
		_current_level = new_level
		_update_ui()


func _on_buff_unlocked_changed(buff_id: StringName, unlocked: bool) -> void:
	if buff_data != null and buff_id == buff_data.id:
		_update_ui()

func _on_currency_changed(_changed_currency_id: StringName, _new_value: BigNumber) -> void:
	_update_ui()

func _update_ui() -> void:
	if buff_data == null:
		return
	
	if not GameState.is_buff_unlocked(buff_data.id):
		_button.disabled = true
		_button.text = "Locked"
		return
	
	var is_maxed: bool = _current_level >= buff_data.max_level if buff_data.max_level >= 0 else false
	var can_buy: bool = not is_maxed
	
	var cost_currency_id: StringName = _resolve_buff_cost_currency_id(buff_data)
	if cost_currency_id != &"":
		var cost: BigNumber = buff_data.get_cost_for_level(_current_level)
		var current_amount: BigNumber = GameState.get_currency_amount_by_id(cost_currency_id)
		can_buy = can_buy and (current_amount.is_greater_than(cost) or current_amount.is_equal_to(cost))
	
	_button.disabled = not can_buy
	
	if is_maxed:
		_button.text = "Maxed"
	else:
		var cost: BigNumber = buff_data.get_cost_for_level(_current_level)
		_button.text = "Buy (%s)" % cost.to_string_suffix(2)


func _resolve_buff_cost_currency_id(buff: GeneratorBuffData) -> StringName:
	if buff.cost_currency == null:
		return &""
	
	return CurrencyDatabase.get_currency_id(buff.cost_currency)
