package generator_test

import (
	"fmt"
	"testing"

	"galaxy/game/internal/generator"

	"github.com/stretchr/testify/assert"
)

func TestGenerator(t *testing.T) {
	maxPlayers := 30
	for players := 10; players <= maxPlayers; players++ {
		t.Run(fmt.Sprintf("%d_players", players), func(t *testing.T) {
			var s generator.MapSetting
			m, err := generator.Generate(func(ms *generator.MapSetting) {
				ms.Players = uint32(players)
				s = *ms
			})
			if err != nil {
				t.Fatalf("generate: %s", err)
				return
			}
			assert.Equal(t, players, len(m.HomePlanets), "hw-s count")
			for hw := range m.HomePlanets {
				testPlanetPositionOnMap(t, generator.PlanetClassHW, m.Height, m.Width, m.HomePlanets[hw].HW.Position.X, m.HomePlanets[hw].HW.Position.Y)
				assert.Equal(t, s.HWSize, m.HomePlanets[hw].HW.Size, "hw #%d: size", hw)
				assert.Equal(t, s.HWResources, m.HomePlanets[hw].HW.Resources, "hw #%d: resources", hw)
				assert.Equal(t, int(s.DWCount), len(m.HomePlanets[hw].DW), "hw #%d: dw-s count", hw)
				for dw := range m.HomePlanets[hw].DW {
					testPlanetPositionOnMap(t, generator.PlanetClassDW, m.Height, m.Width, m.HomePlanets[hw].DW[dw].Position.X, m.HomePlanets[hw].DW[dw].Position.Y)
					assert.Equal(t, s.DWSize, m.HomePlanets[hw].DW[dw].Size, "hw #%d dw #%d: size", hw, dw)
					assert.Equal(t, s.DWResources, m.HomePlanets[hw].DW[dw].Resources, "hw #%d dw #%d: resources", hw, dw)
					d := m.ShortDistance(m.HomePlanets[hw].HW.Position, m.HomePlanets[hw].DW[dw].Position)
					assert.LessOrEqualf(t, float64(s.DWMinDistance), d, "distance: HW[%.04f,%04f] <-> DW[%.04f,%04f]",
						m.HomePlanets[hw].HW.Position.X, m.HomePlanets[hw].HW.Position.Y,
						m.HomePlanets[hw].DW[dw].Position.X, m.HomePlanets[hw].DW[dw].Position.Y)
					assert.GreaterOrEqualf(t, float64(s.DWMaxDistance), d, "distance: HW[%.04f,%04f] <-> DW[%.04f,%04f]",
						m.HomePlanets[hw].HW.Position.X, m.HomePlanets[hw].HW.Position.Y,
						m.HomePlanets[hw].DW[dw].Position.X, m.HomePlanets[hw].DW[dw].Position.Y)
				}
			}
			assert.LessOrEqualf(t, int(s.NobodysPlanets()), len(m.FreePlanets), "free planets clount")
			freePlanetCount := make(map[generator.PlanetClass]int)
			for fp := range m.FreePlanets {
				ps := planetSettings(t, m.FreePlanets[fp].PlanetClass, s)
				testPlanetParameters(t, ps, m.Height, m.Width, m.FreePlanets[fp])
				if v, ok := freePlanetCount[m.FreePlanets[fp].PlanetClass]; !ok {
					freePlanetCount[m.FreePlanets[fp].PlanetClass] = 1
				} else {
					freePlanetCount[m.FreePlanets[fp].PlanetClass] = v + 1
				}
				if ps.MinDistanceHW > 0 {
					for hw := range m.HomePlanets {
						d := m.ShortDistance(m.HomePlanets[hw].HW.Position, m.FreePlanets[fp].Position)
						assert.LessOrEqualf(t, float64(ps.MinDistanceHW), d, "distance: HW[%.04f,%04f] <-> planet_class=%v[%.04f,%04f]",
							m.HomePlanets[hw].HW.Position.X, m.HomePlanets[hw].HW.Position.Y,
							m.FreePlanets[fp].PlanetClass,
							m.FreePlanets[fp].Position.X, m.FreePlanets[fp].Position.Y)
					}
				}
			}
			for pc, num := range freePlanetCount {
				ps := planetSettings(t, pc, s)
				maxNum := ps.Number(s.NobodysPlanets())
				assert.Equalf(t, num, maxNum, "planet_class=%v ratio=%f of total %d", pc, ps.Ratio, s.NobodysPlanets())
			}
		})
	}
}

func testPlanetParameters(t *testing.T, s generator.PlanetSetting, mapW, mapH uint32, p generator.Planet) {
	testPlanetPositionOnMap(t, p.PlanetClass, mapW, mapH, p.Position.X, p.Position.Y)
	assert.LessOrEqualf(t, s.MinResource, p.Resources, "planet class=%s min resources", p.PlanetClass)
	assert.GreaterOrEqualf(t, s.MaxResource, p.Resources, "planet class=%s max resources", p.PlanetClass)
	assert.LessOrEqualf(t, s.MinSize, p.Size, "planet class=%s min size", p.PlanetClass)
	assert.GreaterOrEqualf(t, s.MaxSize, p.Size, "planet class=%s max size", p.PlanetClass)
}

func testPlanetPositionOnMap(t *testing.T, cls generator.PlanetClass, mapW, mapH uint32, x, y float64) {
	assert.GreaterOrEqual(t, x, 0.0, "planet class=%s x=%f < 0", cls, x)
	assert.Less(t, x, float64(mapW), "planet class=%s x=%f >= %d", cls, x, mapW)
	assert.GreaterOrEqual(t, y, 0.0, "planet class=%s y=%f < 0", cls, y)
	assert.Less(t, y, float64(mapH), "planet class=%s y=%f >= %d", cls, y, mapH)
}

func planetSettings(t *testing.T, pc generator.PlanetClass, s generator.MapSetting) generator.PlanetSetting {
	switch pc {
	case generator.PlanetClassGiant:
		return s.GiantPlanets
	case generator.PlanetClassBig:
		return s.BigPlanets
	case generator.PlanetClassNormal:
		return s.NormalPlanets
	case generator.PlanetClassRich:
		return s.RichPlanets
	case generator.PlanetClassAsterioid:
		return s.Asterioids
	default:
		assert.FailNow(t, "unexpected planet class: %s", pc)
		return generator.PlanetSetting{}
	}
}

func BenchmarkGenerator(b *testing.B) {
	i := 0
	for b.Loop() {
		i++
		b.Run(fmt.Sprintf("instance #%02d", i), func(b *testing.B) {
			_, err := generator.Generate()
			if err != nil {
				b.Error(err)
			}
		})
	}
}
