class_name ColorStorageClass
extends Node

var tree_collection = preload("res://Entities/Tree/Resources/TreeData.tres") as TreeDataCollection
var tree_materials: Dictionary = {}
var grass_materials: Dictionary = {
  "spring": {
	"bottom": Color(0.18, 0.35, 0.12), # #2e5920 - Dark forest base
	"top": Color(0.18, 0.35, 0.12), # Color(0.35, 0.65, 0.25), # #59a640 - Bright spring green
	"base": Color(0.12, 0.25, 0.08), # #1f4014 - Deep shadow green
	"flower": Color(0.776, 0.835, 0.855) # rgb(198, 213, 218) - Spring flower color
  },
  "summer": {
	"bottom": Color(0.22, 0.45, 0.18), # #38732e - Rich summer base
	"top": Color(0.22, 0.45, 0.18), # Color(0.4, 0.75, 0.3), # #66bf4d - Sun-kissed green
	"base": Color(0.15, 0.3, 0.12), # #264d1f - Shaded summer base
	"flower": Color(0.408, 0.537, 0.886) # rgb(104, 137, 226) - Summer flower color
  },
  "autumn": {
	"bottom": Color(0.35, 0.4, 0.15), # #596626 - Yellowing grass
	"top": Color(0.42, 0.45, 0.19), # #6b7330 - Muted autumn tips
	"base": Color(0.25, 0.25, 0.1), # #40401a - Dying grass base
	"flower": Color(0.820, 0.337, 0.318) # rgb(209, 86, 81) - Autumn flower color
  },
  "winter": {
	"bottom": Color(0.25, 0.3, 0.2), # #404d33 - Dormant green-brown
	"top": Color(0.35, 0.4, 0.3), # #59664d - Frost-touched tips
	"base": Color(0.2, 0.2, 0.15), # #333326 - Winter soil color
	"flower": Color(1.0, 0.8, 0.6) # #ffd9b3 - Winter flower color
  }
}
var bush_materials: Dictionary = {}
var flower_materials: Dictionary = {}

@export var tree_colours: Array

func _ready() -> void:
	populate_tree_colors()
	Log.pr("Tree colors populated: %d trees" % tree_materials.size())
	populate_bush_materials()
	Log.pr("Bush materials populated: %d seasons" % bush_materials.size())
	populate_flower_materials()
	Log.pr("Flower materials populated: %d seasons" % flower_materials.size())

# Create materials once at startup
func populate_tree_colors() -> void:
	for tree in tree_collection.trees:
		Log.pr("Loading tree: %s" % tree.tree_name)
		
		tree_materials[tree.tree_name] = {
			"spring": {
				"leaf": create_leaf_materials(tree.spring_leaf_color, "spring", tree.tree_name),
				"trunk": create_trunk_materials(tree.spring_trunk_color, "spring", tree.tree_name)
			},
			"summer": {
				"leaf": create_leaf_materials(tree.summer_leaf_color, "summer", tree.tree_name),
				"trunk": create_trunk_materials(tree.summer_trunk_color, "summer", tree.tree_name)
			},
			"autumn": {
				"leaf": create_leaf_materials(tree.autumn_leaf_color, "autumn", tree.tree_name),
				"trunk": create_trunk_materials(tree.autumn_trunk_color, "autumn", tree.tree_name)
			},
			"winter": {
				"leaf": create_leaf_materials(tree.winter_leaf_color, "winter", tree.tree_name),
				"trunk": create_trunk_materials(tree.winter_trunk_color, "winter", tree.tree_name)
			}
		}

func create_leaf_materials(base_color: Color, season: String, tree_name: String) -> Dictionary:
	var materials = {}
	materials = {
		"1": create_material(base_color, "%s_leaf_%s_primary" % [tree_name, season]),
		"2": create_material(base_color.darkened(0.1), "%s_leaf_%s_secondary" % [tree_name, season]),
		"3": create_material(base_color.lightened(0.1), "%s_leaf_%s_highlight" % [tree_name, season]),
		"4": create_material(base_color.darkened(0.3), "%s_leaf_%s_shadow" % [tree_name, season])
	}
	return materials

func create_trunk_materials(base_color: Color, season: String, tree_name: String) -> Dictionary:
	var materials = {}
	materials = {
		"1": create_material(base_color, "%s_trunk_%s_primary" % [tree_name, season]),
		"2": create_material(base_color.darkened(0.2), "%s_trunk_%s_bark" % [tree_name, season]),
		"3": create_material(Color(0.2, 0.4, 0.1), "%s_trunk_%s_moss" % [tree_name, season]),
		"4": create_material(base_color.lightened(0.1), "%s_trunk_%s_highlight" % [tree_name, season])
	}
	return materials

func create_material(color: Color, material_name: String) -> StandardMaterial3D:
	var material = StandardMaterial3D.new()
	material.albedo_color = color
	material.metallic = 0.0
	material.roughness = 0.8
	material.resource_name = material_name
	return material

func get_random_leaf_material(tree_name: String, season: String) -> StandardMaterial3D:
	if tree_materials.has(tree_name) and tree_materials[tree_name].has(season):
		var leaf_materials = tree_materials[tree_name][season]["leaf"]
		var material_keys = leaf_materials.keys()
		if material_keys.size() > 0:
			var random_key = material_keys[randi() % material_keys.size()]
			return leaf_materials[random_key]
	
	return create_material(Color.GREEN, "fallback_leaf")

func get_random_trunk_material(tree_name: String, season: String) -> StandardMaterial3D:
	if tree_materials.has(tree_name) and tree_materials[tree_name].has(season):
		var trunk_materials = tree_materials[tree_name][season]["trunk"]
		var material_keys = trunk_materials.keys()
		if material_keys.size() > 0:
			var random_key = material_keys[randi() % material_keys.size()]
			return trunk_materials[random_key]
	
	return create_material(Color(0.6, 0.4, 0.2), "fallback_trunk")

func populate_bush_materials() -> void:
	for season in Season.seasons:
		bush_materials[season] = {
			"1": create_bush_material(grass_materials[season]["base"], grass_materials[season]["flower"].lightened(0.1), "bush_base_%s" % season)
		}
	Log.pr("Bush materials populated for seasons: %s" % bush_materials.keys())

func create_bush_material(color: Color, variation: Color, material_name: String) -> ShaderMaterial:
	var material = ShaderMaterial.new()

	var shader = load("res://leaves.gdshader")

	material.shader = shader
	material.resource_name = material_name

	material.set_shader_parameter("base_color", color)
	material.set_shader_parameter("variation_color", variation)

	return material

func get_random_bush_material(season: String) -> ShaderMaterial:
	if bush_materials.has(season):
		var bush_materials_season = bush_materials[season]
		var material_keys = bush_materials_season.keys()
		if material_keys.size() > 0:
			var random_key = material_keys[randi() % material_keys.size()]
			return bush_materials_season[random_key]
	
	Log.pr("No bush materials found for season: %s, returning fallback." % season)
	return create_bush_material(Color.GREEN, Color.GREEN, "fallback_bush")

func populate_flower_materials() -> void:
	for season in Season.seasons:
		flower_materials[season] = {
			"flower": create_material(grass_materials[season]['flower'], "flower_base_%s" % season),
			"stem": create_material(grass_materials[season]["top"], "flower_stem_%s" % season),
		}
	Log.pr("Flower materials populated for seasons: %s" % flower_materials.keys())

func get_random_flower_material(season: String) -> StandardMaterial3D:
	if flower_materials.has(season):
		var flower_materials_season = flower_materials[season]
		var material_keys = flower_materials_season.keys()
		if material_keys.size() > 0:
			var random_key = material_keys[randi() % material_keys.size()]
			return flower_materials_season[random_key]
	
	Log.pr("No flower materials found for season: %s, returning fallback." % season)
	return create_material(Color(1.0, 1.0, 0.5), "fallback_flower")
