package generator

import (
	"fmt"
	"galaxy/util"
	"math"
	"math/rand"
)

const (
	fullSectorWithFactor = int(360. / defaultFactor)
	deadZoneDWGrad       = 15.
)

func Generate(cfg ...func(*MapSetting)) (Map, error) {
	ms := DefaultMapSetting()
	for i := range cfg {
		cfg[i](&ms)
	}

	size := ms.ExpectedSize()

	m, err := NewMap(size, size, ms.Players)
	if err != nil {
		return Map{}, fmt.Errorf("%s: NewMap: %s", ms, err)
	}

	freePlanets := ms.NobodysPlanets()

	createPlanets := func(pc PlanetClass, ps PlanetSetting) error {
		return m.CreatePlanets(pc, ps.Number(freePlanets), float64(ps.MinDistanceHW), RandIFn(ps.MinSize, ps.MaxSize), RandIFn(ps.MinResource, ps.MaxResource))
	}

	// 1. Place Giant planets
	if err := createPlanets(PlanetClassGiant, ms.GiantPlanets); err != nil {
		return Map{}, fmt.Errorf("%s: create giant planets: %s", ms, err)
	}

	// 2. Place Big planets
	if err := createPlanets(PlanetClassBig, ms.BigPlanets); err != nil {
		return Map{}, fmt.Errorf("%s: create big planets: %s", ms, err)
	}

	// 3. Place players' Home Worlds
	for player := 0; player < int(ms.Players); player++ {
		hwCoord, err := m.NewCoordinate(float64(ms.HWMinDistance))
		if err != nil {
			return Map{}, fmt.Errorf("%s: hw new_coordinate: %s", ms, err)
		}
		hwPlanet := NewPlanet(PlanetClassHW, hwCoord, ms.HWSize, ms.HWResources)
		m.HomePlanets[player] = PlanetarySystem{HW: hwPlanet, DW: make([]Planet, ms.DWCount)}
		grads := make(map[uint16]bool, fullSectorWithFactor)
		for i := range fullSectorWithFactor {
			grads[uint16(i)] = true
		}
		for dw := 0; dw < int(ms.DWCount); dw++ {
			p := rand.Float64()*(float64(ms.DWMaxDistance)-float64(ms.DWMinDistance)) + float64(ms.DWMinDistance)
			free := make([]uint16, 0)
			for g := range grads {
				if v, ok := grads[g]; ok && v {
					free = append(free, g)
				}
			}
			randGrad := free[rand.Intn(len(free))]
			phi := float64(randGrad) * defaultFactor
			for i := range int(deadZoneDWGrad / defaultFactor) {
				lx := randGrad - uint16(i)
				if uint16(i) > randGrad {
					lx = uint16(fullSectorWithFactor) - (uint16(i) - randGrad)
				}
				grads[lx] = false
				rx := randGrad + uint16(i)
				if rx > uint16(fullSectorWithFactor) {
					rx = rx - uint16(fullSectorWithFactor)
				}
				grads[rx] = false
			}
			x := util.WrapF(hwCoord.X+p*math.Cos(phi), int(size))
			y := util.WrapF(hwCoord.Y+p*math.Sin(phi), int(size))
			dwPlanet := NewPlanet(PlanetClassDW, Coordinate{x, y}, ms.DWSize, ms.DWResources)
			m.HomePlanets[player].DW[dw] = dwPlanet
		}
	}

	// 4. Clear plotter and set dead zones around existing planets
	m.plotter.Clear()
	for i := range m.HomePlanets {
		m.plotter.MarkDeadZone(m.HomePlanets[i].HW.Position.X, m.HomePlanets[i].HW.Position.Y, ms.OthersMinDistance)
		for j := range m.HomePlanets[i].DW {
			m.plotter.MarkDeadZone(m.HomePlanets[i].DW[j].Position.X, m.HomePlanets[i].DW[j].Position.Y, ms.OthersMinDistance)
		}
	}
	for i := range m.FreePlanets {
		m.plotter.MarkDeadZone(m.FreePlanets[i].Position.X, m.FreePlanets[i].Position.Y, ms.OthersMinDistance)
	}

	// 5. Place Normal planets
	if err := createPlanets(PlanetClassNormal, ms.NormalPlanets); err != nil {
		return Map{}, fmt.Errorf("%s: create normal planets: %s", ms, err)
	}

	// 6. Place Rich planets
	if err := createPlanets(PlanetClassRich, ms.RichPlanets); err != nil {
		return Map{}, fmt.Errorf("%s: create rich planets: %s", ms, err)
	}

	// 7. Place Asteroids
	if err := createPlanets(PlanetClassAsterioid, ms.Asterioids); err != nil {
		return Map{}, fmt.Errorf("%s: create asteroids: %s", ms, err)
	}

	return *m, nil
}
