package controller

import (
	"fmt"
	"iter"
	"slices"

	"galaxy/calc"
	"galaxy/util"

	e "galaxy/error"

	"galaxy/game/internal/model/game"

	"github.com/google/uuid"
)

func (c *Cache) ShipClassCreate(ri int, typeName string, drive float64, ammo int, weapons, shileds, cargo float64) error {
	c.validateRaceIndex(ri)
	if err := calc.ValidateShipTypeValues(drive, ammo, weapons, shileds, cargo); err != nil {
		return err
	}
	n, ok := util.ValidateTypeName(typeName)
	if !ok {
		return e.NewEntityTypeNameValidationError("%q", n)
	}
	if st := slices.IndexFunc(c.g.Race[ri].ShipTypes, func(st game.ShipType) bool { return st.Name == typeName }); st >= 0 {
		return e.NewEntityDuplicateIdentifierError("ship class %q", c.g.Race[ri].ShipTypes[st].Name)
	}
	c.g.Race[ri].ShipTypes = append(c.g.Race[ri].ShipTypes, game.ShipType{
		ID:       uuid.New(),
		Name:     n,
		Drive:    game.Float(drive),
		Armament: uint(ammo),
		Weapons:  game.Float(weapons),
		Shields:  game.Float(shileds),
		Cargo:    game.Float(cargo),
	})
	c.invalidateShipGroupCache()
	c.invalidateFleetCache()
	return nil
}

func (c *Cache) shipClassMerge(ri int, sourceName, targetName string) error {
	c.validateRaceIndex(ri)
	sourceClass, sti, ok := c.ShipClass(ri, sourceName)

	if !ok {
		return e.NewEntityNotExistsError("source ship type %q", sourceName)
	}
	targetClass, _, ok := c.ShipClass(ri, targetName)

	if !ok {
		return e.NewEntityNotExistsError("target ship type %q", sourceName)
	}

	if sourceClass.Name == targetClass.Name {
		return e.NewEntityTypeNameEqualityError("ship type %q", targetName)
	}

	if !sourceClass.Equal(*targetClass) {
		return e.NewMergeShipTypeNotEqualError()
	}

	// switch planet productions to the new type
	for pl := range c.g.Map.Planet {
		if c.g.Map.Planet[pl].OwnedBy(c.g.Race[ri].ID) &&
			c.g.Map.Planet[pl].Production.Type == game.ProductionShip &&
			c.g.Map.Planet[pl].Production.SubjectID != nil &&
			*c.g.Map.Planet[pl].Production.SubjectID == sourceClass.ID {

			c.g.Map.Planet[pl].Production.SubjectID = &targetClass.ID
		}
	}

	// switch ship groups to the new type
	for sg := range c.g.ShipGroups {
		if c.g.ShipGroups[sg].OwnerID == c.g.Race[ri].ID && c.g.ShipGroups[sg].TypeID == sourceClass.ID {
			c.g.ShipGroups[sg].TypeID = targetClass.ID
		}
	}

	// remove the source type
	c.g.Race[ri].ShipTypes = append(c.g.Race[ri].ShipTypes[:sti], c.g.Race[ri].ShipTypes[sti+1:]...)

	c.invalidateShipGroupCache()
	c.invalidateFleetCache()

	return nil
}

func (c *Cache) shipClassRemove(ri int, name string) error {
	c.validateRaceIndex(ri)
	st, i, ok := c.ShipClass(ri, name)
	if !ok {
		return e.NewEntityNotExistsError("ship type %q", name)
	}
	if pl := slices.IndexFunc(c.g.Map.Planet, func(p game.Planet) bool {
		return p.Production.Type == game.ProductionShip &&
			p.Production.SubjectID != nil &&
			st.ID == *p.Production.SubjectID
	}); pl >= 0 {
		return e.NewDeleteShipTypePlanetProductionError(c.g.Map.Planet[pl].Name)
	}

	for sg := range c.listShipGroups(ri) {
		if sg.TypeID == st.ID {
			return e.NewDeleteShipTypeExistingGroupError("group: %s", sg.ID)
		}
	}
	c.g.Race[ri].ShipTypes = append(c.g.Race[ri].ShipTypes[:i], c.g.Race[ri].ShipTypes[i+1:]...)

	c.invalidateShipGroupCache()
	c.invalidateFleetCache()

	return nil
}

// ShipTypes used for tests only
func (c *Cache) ShipTypes(ri int) []*game.ShipType {
	c.validateRaceIndex(ri)
	result := make([]*game.ShipType, len(c.g.Race[ri].ShipTypes))
	for i := range c.g.Race[ri].ShipTypes {
		result[i] = &c.g.Race[ri].ShipTypes[i]
	}
	return result
}

func (c *Cache) ListShipTypes(ri int) iter.Seq[*game.ShipType] {
	return func(yield func(*game.ShipType) bool) {
		for i := range c.g.Race[ri].ShipTypes {
			if !yield(&c.g.Race[ri].ShipTypes[i]) {
				return
			}
		}
	}
}

func (c *Cache) ShipClass(ri int, name string) (*game.ShipType, int, bool) {
	i := slices.IndexFunc(c.g.Race[ri].ShipTypes, func(st game.ShipType) bool { return st.Name == name })
	if i < 0 {
		return nil, -1, false
	}
	return &c.g.Race[ri].ShipTypes[i], i, true
}

func (c *Cache) ShipType(ri int, ID uuid.UUID) (*game.ShipType, bool) {
	c.validateRaceIndex(ri)
	for i := range c.g.Race[ri].ShipTypes {
		if c.g.Race[ri].ShipTypes[i].ID == ID {
			return &c.g.Race[ri].ShipTypes[i], true
		}
	}
	return nil, false
}

func (c *Cache) MustShipType(ri int, ID uuid.UUID) *game.ShipType {
	if v, ok := c.ShipType(ri, ID); ok {
		return v
	}
	panic(fmt.Sprintf("ship class not found: race_idx=%d id=%v", ri, ID))
}
