extends Node3D

enum CanopyVariant {
	GREEN,
	DARK_GREEN,
	YELLOW_GREEN,
}

@export var canopy_variant: CanopyVariant = CanopyVariant.GREEN
@export var canopy_radius: float = 1.35

const COLORS := {
	CanopyVariant.GREEN: Color(0.19, 0.38, 0.17),
	CanopyVariant.DARK_GREEN: Color(0.12, 0.30, 0.12),
	CanopyVariant.YELLOW_GREEN: Color(0.30, 0.42, 0.15),
}

const WIND_SHADER := preload("res://world/jajce/materials/wind.gdshader")


func _ready() -> void:
	for child in get_children():
		child.queue_free()

	var hash_seed := hash(global_position)
	var rng := RandomNumberGenerator.new()
	rng.seed = hash_seed
	canopy_variant = rng.randi() % COLORS.size() as CanopyVariant
	canopy_radius = 1.0 + rng.randf() * 0.8

	var trunk_mesh := CylinderMesh.new()
	trunk_mesh.top_radius = 0.22
	trunk_mesh.bottom_radius = 0.32
	trunk_mesh.height = 2.8

	var trunk_mat := StandardMaterial3D.new()
	trunk_mat.albedo_color = Color(0.27, 0.16, 0.09)
	trunk_mat.roughness = 0.95
	trunk_mesh.material = trunk_mat

	var trunk := MeshInstance3D.new()
	trunk.mesh = trunk_mesh
	trunk.position = Vector3(0, 1.4, 0)
	add_child(trunk)

	var canopy_mesh := SphereMesh.new()
	canopy_mesh.radius = canopy_radius
	canopy_mesh.height = 2.4

	var canopy_mat := ShaderMaterial.new()
	canopy_mat.shader = WIND_SHADER
	canopy_mat.set_shader_parameter("albedo", COLORS[canopy_variant])
	canopy_mat.set_shader_parameter("roughness", 0.9)
	canopy_mesh.material = canopy_mat

	var canopy := MeshInstance3D.new()
	canopy.mesh = canopy_mesh
	canopy.position = Vector3(0, 3.2 + (canopy_radius - 1.35) * 0.5, 0)
	add_child(canopy)
