extends SubViewport

@export var camera: Camera3D
@export var output_folder := "res://CameraOutputs/"
@export var step_size := 100.0
@export var frame_count := 11
enum RenderingMode {STATISTICS_EXCHANGE, PERIODIC_TILING, TILING_AND_BLENDING}
@export var renderingMode := RenderingMode.STATISTICS_EXCHANGE

var current_frame := 0
var is_rendering := false
var results_step := 0
var camera_x_pos := 0.0

func _ready():
	
	#default: Mean exchange
	if renderingMode == RenderingMode.STATISTICS_EXCHANGE:
		RenderingServer.global_shader_parameter_set("texturingMode", 0)
		print("🔧 Texturing set to mean exchange.")
	elif renderingMode == RenderingMode.PERIODIC_TILING:
		RenderingServer.global_shader_parameter_set("texturingMode", 3)
		print("🔧 Texturing set to periodic tiling.")
	elif renderingMode == RenderingMode.TILING_AND_BLENDING:
		RenderingServer.global_shader_parameter_set("texturingMode", 4)
		print("🔧 Texturing set to tiling and blending.")
		
	camera_x_pos = camera.global_position.x
	render_target_update_mode = SubViewport.UPDATE_ONCE
	render_target_clear_mode = SubViewport.CLEAR_MODE_ALWAYS
	_start_next_frame()

func _process(_delta):
	renderFrame()

func renderFrame():
	if is_rendering:
		await RenderingServer.frame_post_draw
		var tex := get_texture()
		if tex and tex.get_width() > 0:
			var image := tex.get_image()
			if image:
				var filename := output_folder + "result_step" + str(results_step) + "_frame" + str(current_frame) + ".png"
				var error := image.save_png(filename)
				if error == OK:
					print("✅ Saved: ", filename)
				else:
					print("❌ Failed to save: ", filename)
			current_frame += 1
			if current_frame < frame_count:
				_start_next_frame()
			else:
				print("✅ All frames rendered.")
				render_target_update_mode = SubViewport.UPDATE_DISABLED
				is_rendering = false
				if renderingMode == RenderingMode.STATISTICS_EXCHANGE:
					changeSettingsAndReset()

func _start_next_frame():
	if current_frame != 0:
		camera.global_position.x += step_size
	render_target_update_mode = SubViewport.UPDATE_ONCE
	is_rendering = true

func changeSettingsAndReset():
	results_step += 1
	if(results_step == 1): #PCA covariance exchange
		RenderingServer.global_shader_parameter_set("texturingMode", 1)
		RenderingServer.global_shader_parameter_set("recoloringMode", 0)
		print("🔧 Texturing set to PCA covariance exchange.")
	if(results_step == 2): #ZCA covariance exchange
		RenderingServer.global_shader_parameter_set("recoloringMode", 1)
		print("🔧 Texturing set to ZCA covariance exchange.")
	if(results_step == 3): #PCA histogram exchange
		RenderingServer.global_shader_parameter_set("texturingMode", 1)
		RenderingServer.global_shader_parameter_set("recoloringMode", 0)
		print("🔧 Texturing set to PCA histogram exchange.")
	if(results_step == 4): #ZCA histogram exchange
		RenderingServer.global_shader_parameter_set("recoloringMode", 1)
		print("🔧 Texturing set to ZCA histogram exchange.")
	if(results_step < 5):
		current_frame = 0
		camera.global_position.x = camera_x_pos
		_start_next_frame()
