package controller

import (
	"fmt"
	"slices"

	"galaxy/util"

	e "galaxy/error"

	"galaxy/game/internal/model/game"

	"github.com/google/uuid"
)

func (c *Cache) ScienceCreate(ri int, name string, drive, weapons, shileds, cargo float64) error {
	c.validateRaceIndex(ri)
	n, ok := util.ValidateTypeName(name)
	if !ok {
		return e.NewEntityTypeNameValidationError("%q", n)
	}
	if sc := slices.IndexFunc(c.g.Race[ri].Sciences, func(s game.Science) bool { return s.Name == n }); sc >= 0 {
		return e.NewEntityDuplicateIdentifierError("science %q", c.g.Race[ri].Sciences[sc].Name)
	}

	if drive < 0 {
		return e.NewDriveValueError(drive)
	}
	if weapons < 0 {
		return e.NewWeaponsValueError(weapons)
	}
	if shileds < 0 {
		return e.NewShieldsValueError(shileds)
	}
	if cargo < 0 {
		return e.NewCargoValueError(cargo)
	}
	sum := drive + weapons + shileds + cargo
	if sum != 1 {
		return e.NewScienceSumValuesError("D=%f W=%f S=%f C=%f sum=%f", drive, weapons, shileds, cargo, sum)
	}
	c.g.Race[ri].Sciences = append(c.g.Race[ri].Sciences, game.Science{
		ID:      uuid.New(),
		Name:    n,
		Drive:   game.Float(drive),
		Weapons: game.Float(weapons),
		Shields: game.Float(shileds),
		Cargo:   game.Float(cargo),
	})
	return nil
}

func (c *Cache) ScienceRemove(ri int, name string) error {
	c.validateRaceIndex(ri)
	sc := slices.IndexFunc(c.g.Race[ri].Sciences, func(s game.Science) bool { return s.Name == name })
	if sc < 0 {
		return e.NewEntityNotExistsError("science %q", name)
	}
	if pl := slices.IndexFunc(c.g.Map.Planet, func(p game.Planet) bool {
		return p.Production.Type == game.ResearchScience &&
			p.Production.SubjectID != nil &&
			*p.Production.SubjectID == c.g.Race[ri].Sciences[sc].ID
	}); pl >= 0 {
		return e.NewDeleteSciencePlanetProductionError(c.g.Map.Planet[pl].Name)
	}
	c.g.Race[ri].Sciences = append(c.g.Race[ri].Sciences[:sc], c.g.Race[ri].Sciences[sc+1:]...)
	return nil
}

func ResearchTech(r *game.Race, freeProduction float64, drive, weapons, shields, cargo float64) {
	increment := freeProduction / 5000.
	if drive > 0 {
		r.Tech = r.Tech.Set(game.TechDrive, r.Tech.Value(game.TechDrive)+increment*drive)
	}
	if weapons > 0 {
		r.Tech = r.Tech.Set(game.TechWeapons, r.Tech.Value(game.TechWeapons)+increment*weapons)
	}
	if shields > 0 {
		r.Tech = r.Tech.Set(game.TechShields, r.Tech.Value(game.TechShields)+increment*shields)
	}
	if cargo > 0 {
		r.Tech = r.Tech.Set(game.TechCargo, r.Tech.Value(game.TechCargo)+increment*cargo)
	}
}

// Internal func

func (c *Cache) raceScience(ri int) []game.Science {
	c.validateRaceIndex(ri)
	return c.g.Race[ri].Sciences
}

func (c *Cache) mustScience(ri int, id uuid.UUID) *game.Science {
	c.validateRaceIndex(ri)
	r := &c.g.Race[ri]
	i := slices.IndexFunc(r.Sciences, func(s game.Science) bool { return s.ID == id })
	if i < 0 {
		panic(fmt.Sprintf("science not found for race=%q id=%v", r.Name, id))
	}
	return &c.g.Race[ri].Sciences[i]
}
