package engine

import (
	"math"
	"testing"
)

func TestNewTransform(t *testing.T) {
	transform := NewTransform(10, 20, 100, 1)
	if transform.x != 10 || transform.y != 20 || transform.size != 100 || transform.rotation != 1 {
		t.Errorf("NewTransform(10, 20, 100, 1) = %v, want {x:10, y:20, size:100, rotation:1}", transform)
	}
}

func TestTransformIconPoint(t *testing.T) {
	tests := []struct {
		transform  Transform
		x, y, w, h float64
		expected   Point
	}{
		// No rotation (0 degrees)
		{NewTransform(0, 0, 100, 0), 10, 20, 5, 5, Point{X: 10, Y: 20}},
		{NewTransform(10, 20, 100, 0), 5, 10, 0, 0, Point{X: 15, Y: 30}},

		// 90 degrees rotation
		{NewTransform(0, 0, 100, 1), 10, 20, 5, 5, Point{X: 75, Y: 10}},

		// 180 degrees rotation
		{NewTransform(0, 0, 100, 2), 10, 20, 5, 5, Point{X: 85, Y: 75}},

		// 270 degrees rotation
		{NewTransform(0, 0, 100, 3), 10, 20, 5, 5, Point{X: 20, Y: 85}},

		// Test rotation normalization (rotation > 3)
		{NewTransform(0, 0, 100, 4), 10, 20, 0, 0, Point{X: 10, Y: 20}}, // Same as rotation 0
		{NewTransform(0, 0, 100, 5), 10, 20, 5, 5, Point{X: 75, Y: 10}}, // Same as rotation 1
	}

	for _, tt := range tests {
		result := tt.transform.TransformIconPoint(tt.x, tt.y, tt.w, tt.h)
		if !approximatelyEqual(result.X, tt.expected.X) || !approximatelyEqual(result.Y, tt.expected.Y) {
			t.Errorf("Transform(%v).TransformIconPoint(%v, %v, %v, %v) = %v, want %v",
				tt.transform, tt.x, tt.y, tt.w, tt.h, result, tt.expected)
		}
	}
}

func TestNoTransform(t *testing.T) {
	if NoTransform.x != 0 || NoTransform.y != 0 || NoTransform.size != 0 || NoTransform.rotation != 0 {
		t.Errorf("NoTransform should be {x:0, y:0, size:0, rotation:0}, got %v", NoTransform)
	}

	// Test that NoTransform doesn't change points
	point := Point{X: 10, Y: 20}
	result := NoTransform.TransformIconPoint(point.X, point.Y, 0, 0)
	if result != point {
		t.Errorf("NoTransform should not change point %v, got %v", point, result)
	}
}

// approximatelyEqual checks if two float64 values are approximately equal
func approximatelyEqual(a, b float64) bool {
	const epsilon = 1e-9
	return math.Abs(a-b) < epsilon
}
