extends Reference
class_name SummerDayMatrix4


var values: Array


func _init(initial_values: Array = [
	1.0, 0.0, 0.0, 0.0,
	0.0, 1.0, 0.0, 0.0,
	0.0, 0.0, 1.0, 0.0,
	0.0, 0.0, 0.0, 1.0,
]):
	if initial_values.size() != 16:
		push_error(
			"Instance: " + str(self) + " \"initial_values.size() != 16\""
			)
	
	var misplaced_type_found = false
	for i in initial_values:
		if !(i is float):
			misplaced_type_found = true
	if misplaced_type_found:
		push_error(
			"Instance: " + str(self)
			+ " \"initial_values\" contains non-float values!"
			)
		
	values = initial_values


static func from_Basis(from: Basis) -> SummerDayMatrix4:
	var Self = load("res://addons/summer_day/utilities/matrix_4.gd")
	return Self.new([
		from.x.x, from.y.x, 0.0, from.z.x,
		from.x.y, from.y.y, 0.0, from.z.y,
		0.0, 0.0, 0.0, 0.0, # <-- Change to  0.0, 0.0, 1.0, 0.0,
		from.x.z, from.y.z, 0.0, from.z.z,
	])


static func from_Transform(from: Transform) -> SummerDayMatrix4:
	var Self = load("res://addons/summer_day/utilities/matrix_4.gd")
	return Self.new([
		from.basis.x.x, from.basis.x.y, from.basis.x.z, from.origin.x,
		from.basis.y.x, from.basis.y.y, from.basis.y.z, from.origin.y,
		from.basis.z.x, from.basis.z.y, from.basis.z.z, from.origin.z,
		0.0, 0.0, 0.0, 1.0
	])


static func mult(
	left: SummerDayMatrix4, right: SummerDayMatrix4
) -> SummerDayMatrix4:
	var Self = load("res://addons/summer_day/utilities/matrix_4.gd")
	var result = []
	for y in range(4):
		for x in range(4):
			var sum = 0.0
			for i in range(4):
				sum += left.get_value(x, i) * right.get_value(i, y)
			result.append(sum)
	return Self.new(result)


# Get value per coordinates.
func get_value(x: int, y: int) -> float:
	return values[y * 4 + x]


func get_values_as_colors() -> Array:
	var list = []
	for y in range(4):
		list.append(
			Color(
				get_value(0, y),
				get_value(1, y),
				get_value(2, y),
				get_value(3, y)
			)
		)
	return list


func print():
	print(values)
