class_name GoalRequirementData
extends Resource

@export var currency: Currency
@export var amount_mantissa: float = 0.0
@export var amount_exponent: int = 0

func get_currency_id() -> StringName:
	if currency == null:
		return &""

	return GameState.get_currency_id(currency)

func get_amount() -> BigNumber:
	return BigNumber.new(amount_mantissa, amount_exponent)

func has_valid_currency() -> bool:
	var currency_id: StringName = get_currency_id()
	if currency_id == &"":
		return false

	return GameState.is_known_currency_id(currency_id)

func has_valid_amount() -> bool:
	return get_amount().mantissa >= 0.0

func is_valid() -> bool:
	return has_valid_currency() and has_valid_amount()

func is_met() -> bool:
	if not is_valid():
		return false

	var total_amount: BigNumber = GameState.get_total_currency_acquired_by_id(get_currency_id())
	return not total_amount.is_less_than(get_amount())

func get_summary_text() -> String:
	if not is_valid():
		return ""

	var currency_name: String = GameState.get_currency_name(get_currency_id())
	return "%s %s" % [get_amount().to_string_suffix(2), currency_name]

func get_progress() -> float:
	if not is_valid():
		return 0.0
	
	var current_amount: BigNumber = GameState.get_total_currency_acquired_by_id(get_currency_id())
	var required_amount: BigNumber = get_amount()
	
	if required_amount.mantissa <= 0.0:
		return 1.0
	
	if current_amount.is_greater_than(required_amount) or current_amount.is_equal_to(required_amount):
		return 1.0
	
	if current_amount.mantissa <= 0.0:
		return 0.0
	
	var current_log: float = log(maxf(current_amount.mantissa, 0.0001)) / log(10) + float(current_amount.exponent)
	var required_log: float = log(maxf(required_amount.mantissa, 0.0001)) / log(10) + float(required_amount.exponent)
	
	if required_log <= 0.0:
		return 1.0
	
	var progress: float = current_log / required_log
	return clampf(progress, 0.0, 1.0)
