class_name UnlocksType
extends Base

# Signals

# Constants
const LOG_CATEGORY = "LOCK"

var unlocks = {}

func unlock_item(key: String, category: String):
	if not unlocks.has(category):
		if _logger: _logger.error("Attempted to unlock item %s in invalid category %s" % [key, category], LOG_CATEGORY)
		return
	
	if not unlocks[category].has(key):
		if _logger: _logger.error("Attempted to unlock invalid item %s in category %s" % [key, category], LOG_CATEGORY)
		return
	
	if unlocks[category][key]:
		if _logger: _logger.warn("Unlocked already unlocked item %s in category %s" % [key, category], LOG_CATEGORY)
	
	unlocks[category][key].status = true
	if _logger: _logger.info("Unlocked item %s in category %s" % [key, category], LOG_CATEGORY)


func lock_item(key: String, category: String):
	if not unlocks.has(category):
		if _logger: _logger.error("Attempted to lock item %s in invalid category %s" % [key, category], LOG_CATEGORY)
		return
	
	if not unlocks[category].has(key):
		if _logger: _logger.error("Attempted to lock invalid item %s in category %s" % [key, category], LOG_CATEGORY)
		return
	
	if not unlocks[category][key]:
		if _logger: _logger.warn("Locked already locked item %s in category %s" % [key, category], LOG_CATEGORY)
	
	unlocks[category][key].status = false
	if _logger: _logger.info("Locked item %s in category %s" % [key, category], LOG_CATEGORY)


func reset_unlocks():
	if (_data):
		unlocks = {}
		
		for category in _data.data:
			var category_data = _data.data[category]
			
			if category_data.has("!_config"):
				if category_data["!_config"].has("unlock"):
					if category_data["!_config"]["unlock"] == "true":
						unlocks[category] = {}
						
						for key in category_data:
							if key == "!_config":
								continue
							
							var element = category_data[key]
							
							if element.has("locked"):
								if element["locked"] == "true":
									if not element.has("unlock_trigger"):
										if _logger: _logger.warn("Locked element %s in category %s has no unlock trigger" % [key, category], LOG_CATEGORY)
										unlocks[category][key] = {
											"status": false
										}
										continue
									
									if not element.has("unlock_amount"):
										if _logger: _logger.warn("Locked element %s in category %s has no unlock amount" % [key, category], LOG_CATEGORY)
										unlocks[category][key] = {
											"status": false
										}
										continue
									
									if is_nan(int(element["unlock_amount"])):
										if _logger: _logger.warn("Locked element %s in category %s unlock amount %s is not a number" % [key, category, element["unlock_amount"]], LOG_CATEGORY)
										unlocks[category][key] = {
											"status": false
										}
										continue
									
									unlocks[category][key] = {
										"status": false,
										"trigger": element["unlock_trigger"],
										"amount": int(element["unlock_amount"])
									}
								else:
									unlocks[category][key] = {
										"status": true
									}
							else:
								unlocks[category][key] = {
									"status": true
								}
	if _logger: _logger.info("Reset Unlocks", LOG_CATEGORY)

func _ready():
	super()
	
	reset_unlocks()


#func _on_number_persisted(key: String, value, category: PersisterType.DataCategory):
	#_check_trigger(key, value)


func _check_trigger(key: String, value: int):
	for category in unlocks:
		var category_data = unlocks[category]
		
		for element in category_data:
			var element_data = category_data[element]
			
			if not element_data.has("trigger"):
				continue
			
			if element_data["trigger"] != key:
				continue
			
			if element_data["status"]:
				continue
			
			if _logger: _logger.debug("Checking trigger %s for item %s in category %s: %d (required) vs %d (incoming)" % [key, element, category, element_data["amount"], value], LOG_CATEGORY)
			if value > element_data["amount"]:
				unlock_item(element, category)