class_name UnlocksClass
extends Node

var unlocks: UnlockDataCollection = load("res://resources/UnlockData.tres")

var base_modifiers: Dictionary = {
	"sale_price_modifier": 1.0,
	"speed_modifier": 1.0,
	"efficiency_modifier": 1.0,
	"wood_respawn_modifier": 1.0,
	"wood_per_click_modifier": 1.0,
	"purchase_rate_modifier": 1.0,
	"autowood_modifier": 1.0,
	"premium_price_modifier": 1.0,
	"reputation_income": 0.0
}

var current_modifiers: Dictionary = base_modifiers.duplicate()

signal item_unlocked()

func reset_modifiers():
	current_modifiers = base_modifiers.duplicate()
	Log.pr("Modifiers reset to base values.")

func apply_modifiers():
	Log.pr("Applying modifiers for unlocked items...")
	reset_modifiers()
	for unlock in unlocks.unlocks:
		if unlock.is_unlocked:
			Log.pr("Applying modifier for unlocked item:", unlock.unlock_name)
			var apply_unlock_modifiers = unlock.get_current_modifiers()
			for key in apply_unlock_modifiers.keys():
				if current_modifiers.has(key):
					Log.pr(" - Current", key, "modifier before:", current_modifiers[key])
					# Reputation income is additive, not multiplicative
					if key == "reputation_income":
						current_modifiers[key] = apply_unlock_modifiers[key]
					else:
						current_modifiers[key] *= apply_unlock_modifiers[key]
					Log.pr(" - Applied", key, "modifier:", apply_unlock_modifiers[key], "New value:", current_modifiers[key])
				else:
					Log.pr(" - Warning: Unknown modifier key:", key)

func get_modifier_value(modifier_key: String) -> float:
	if current_modifiers.has(modifier_key):
		return current_modifiers[modifier_key]
	return 1.0

func get_unlock_by_id(unlock_id: int) -> UnlockDataResource:
	for unlock in unlocks.unlocks:
		if unlock.unlock_id == unlock_id:
			return unlock
	return null

func unlock_item(unlock_id: int) -> bool:
	var unlock_data = get_unlock_by_id(unlock_id)
	if not unlock_data:
		return false
	
	# Check if this unlock can be ranked up (handles both scaling and non-scaling)
	if not unlock_data.can_rank_up():
		Log.pr("Cannot rank up:", unlock_data.unlock_name, "- Already at max rank/unlocked")
		return false
	
	# Get the cost for the next rank/unlock
	var cost = unlock_data.get_next_cost()
	
	# Try to spend the currency
	if Inventory.spend_currency(cost):
		# Store previous rank for logging
		var previous_rank = unlock_data.current_rank
		
		# Unlock or rank up
		unlock_data.unlock()
		
		# Log appropriate message based on unlock type
		if unlock_data.is_scaling:
			Log.pr("Ranked up %s: Rank %d -> %d" % [unlock_data.unlock_name, previous_rank, unlock_data.current_rank])
		else:
			Log.pr("Unlocked:", unlock_data.unlock_name)
		
		# Apply modifiers again (now using updated rank)
		apply_modifiers()
		call_deferred("_refresh_ui")
		
		return true
	else:
		if unlock_data.is_scaling:
			Log.pr("Not enough currency to rank up %s to rank %d (Cost: %d)" % [unlock_data.unlock_name, unlock_data.get_next_rank(), cost])
		else:
			Log.pr("Not enough currency to unlock %s (Cost: %d)" % [unlock_data.unlock_name, cost])
		
		return false

func get_sale_price_per_item():
	return Global.base_sale_price * get_modifier_value("sale_price_modifier") * get_modifier_value("premium_price_modifier")

func get_wood_per_click():
	return Global.wood_per_click * get_modifier_value("wood_per_click_modifier")

func get_wood_respawn_time():
	return Global.base_wood_respawn * get_modifier_value("wood_respawn_modifier")

func get_items_produced_per_tick():
	return Global.cost_per_whittle * get_modifier_value("efficiency_modifier")

func get_sale_demand():
	return Global.base_purchase_rate * get_modifier_value("purchase_rate_modifier")

func get_reputation_income():
	return get_modifier_value("reputation_income")

func _refresh_ui():
	item_unlocked.emit()
