# CORE FRAMEWORK SOURCE FILE
# Copyright (c) 2024 The StarOpenSource Project & Contributors
# Licensed under the GNU Affero General Public License v3
# 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

## Allows for data validation.
extends CoreBaseModule

var schemas: Array[CoreValidationSchema]
var singles: Array[CoreValidationSingle]

# +++ module +++
func _cleanup() -> void:
	# Schemas
	var schemas_remove_enty: Array[CoreValidationSchema] = []
	for schema in schemas:
		schemas_remove_enty.append(schema)
		if !is_instance_valid(schema): continue
		if !is_instance_valid(schema.parent):
			logger.diag("Removing schema '" + schema.name + "'")
			schema.queue_free()
	for schema in schemas_remove_enty:
		schemas.remove_at(schemas.find(schema))
	
	# Singles
	var singles_remove_enty: Array[CoreValidationSingle] = []
	for single in singles:
		singles_remove_enty.append(single)
		if !is_instance_valid(single): continue
		if !is_instance_valid(single.parent):
			logger.diag("Removing single '" + single.name + "'")
			single.queue_free()
	for single in singles_remove_enty:
		singles.remove_at(singles.find(single))

func _schedule() -> void:
	# Singles
	for single in singles:
		if is_instance_valid(single):
			logger.diag("Removing single '" + single.name + "'")
			single.queue_free()
	
	# Schemas
	for schema in schemas:
		if is_instance_valid(schema):
			logger.diag("Removing schema '" + schema.name + "'")
			schema.queue_free()

# +++ data validation +++
## Returns a new [CoreValidationSingle].
func get_single(data, parent: Node) -> CoreValidationSingle:
	var single: CoreValidationSingle = CoreValidationSingle.new(core, data, parent)
	single.name = core.stringify_variables("CoreValidationSingle -> %parent% (type=%type% bytes=%bytes%)", { "parent": parent, "type": type_string(typeof(data)), "bytes": var_to_bytes_with_objects(data).size() })
	singles.append(single)
	return single

## Returns a new [CoreValidationSingle] for use in [CoreValidationSchema]s.
func get_single_schema(parent: Node) -> CoreValidationSingle:
	var single: CoreValidationSingle = CoreValidationSingle.new(core, null, parent)
	single.name = core.stringify_variables("CoreValidationSingle -> %parent% (no data, for schema)", { "parent": parent })
	singles.append(single)
	return single

## Returns a new [CoreValidationSchema].
func get_schema(schema_new: Dictionary, data_new: Dictionary, parent: Node) -> CoreValidationSchema:
	var schema: CoreValidationSchema = CoreValidationSchema.new(core, schema_new, data_new, parent)
	schema.name = core.stringify_variables("CoreValidationSchema -> %parent% (keys_schema=%keys_schema% keys_data=%keys_data% bytes_schema=%bytes_schema% bytes_data=%bytes_data%)", { "parent": parent, "keys_schema": schema_new.size(), "keys_data": data_new.size(), "bytes_schema": var_to_bytes_with_objects(schema_new).size(), "bytes_data": var_to_bytes_with_objects(data_new).size() })
	schemas.append(schema)
	return schema