package engine

import (
	"fmt"
	"testing"
)

var benchmarkCases = []struct {
	h, s, l float64
}{
	{0.0, 0.5, 0.5},  // Red
	{0.33, 0.5, 0.5}, // Green
	{0.66, 0.5, 0.5}, // Blue
	{0.5, 1.0, 0.3},  // Cyan dark
	{0.8, 0.8, 0.7},  // Purple light
}

func BenchmarkCorrectedHSLToRGB(b *testing.B) {
	b.ResetTimer()
	b.ReportAllocs()

	for i := 0; i < b.N; i++ {
		tc := benchmarkCases[i%len(benchmarkCases)]
		CorrectedHSLToRGB(tc.h, tc.s, tc.l)
	}
}

func BenchmarkNewColorCorrectedHSL(b *testing.B) {
	b.ResetTimer()
	b.ReportAllocs()

	for i := 0; i < b.N; i++ {
		tc := benchmarkCases[i%len(benchmarkCases)]
		NewColorCorrectedHSL(tc.h, tc.s, tc.l)
	}
}

// Benchmark hex color formatting optimization
func BenchmarkRGBToHex(b *testing.B) {
	colors := []struct {
		r, g, b uint8
	}{
		{255, 0, 0},
		{0, 255, 0},
		{0, 0, 255},
		{128, 128, 128},
		{255, 255, 255},
		{0, 0, 0},
	}

	b.ResetTimer()
	b.ReportAllocs()

	for i := 0; i < b.N; i++ {
		for _, c := range colors {
			_ = RGBToHex(c.r, c.g, c.b)
		}
	}
}

// Benchmark against the old fmt.Sprintf approach
func BenchmarkRGBToHex_OldSprintf(b *testing.B) {
	colors := []struct {
		r, g, b uint8
	}{
		{255, 0, 0},
		{0, 255, 0},
		{0, 0, 255},
		{128, 128, 128},
		{255, 255, 255},
		{0, 0, 0},
	}

	b.ResetTimer()
	b.ReportAllocs()

	for i := 0; i < b.N; i++ {
		for _, c := range colors {
			_ = fmt.Sprintf("#%02x%02x%02x", c.r, c.g, c.b)
		}
	}
}

// Benchmark Color.String() method
func BenchmarkColorString(b *testing.B) {
	colors := []Color{
		NewColorRGB(255, 0, 0),           // Red, no alpha
		NewColorRGBA(0, 255, 0, 128),     // Green with alpha
		NewColorRGB(0, 0, 255),           // Blue, no alpha
		NewColorRGBA(128, 128, 128, 200), // Gray with alpha
	}

	b.ResetTimer()
	b.ReportAllocs()

	for i := 0; i < b.N; i++ {
		for _, c := range colors {
			_ = c.String()
		}
	}
}
