package controller_test

import (
	"fmt"
	"slices"
	"strings"
	"testing"

	"galaxy/util"

	e "galaxy/error"

	"galaxy/game/internal/model/game"

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

func TestCreateShips(t *testing.T) {
	c, _ := newCache()

	assert.ErrorContains(t,
		c.CreateShips(Race_0_idx, "Unknown_Ship_Type", R0_Planet_0_num, 2),
		e.GenericErrorText(e.ErrInputEntityNotExists))
	assert.ErrorContains(t,
		c.CreateShips(Race_0_idx, Race_0_Gunship, R1_Planet_1_num, 2),
		e.GenericErrorText(e.ErrInputEntityNotOwned))

	assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Freighter, R0_Planet_0_num, 1))
	assert.Len(t, slices.Collect(c.RaceShipGroups(Race_0_idx)), 1)

	assert.NoError(t, c.CreateShips(Race_1_idx, Race_1_Freighter, R1_Planet_1_num, 1))
	assert.Len(t, slices.Collect(c.RaceShipGroups(1)), 1)

	assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Freighter, R0_Planet_0_num, 6))
	assert.Len(t, slices.Collect(c.RaceShipGroups(Race_0_idx)), 2)

	assert.NoError(t, c.CreateShips(Race_1_idx, Race_1_Gunship, R1_Planet_1_num, 1))
	assert.Len(t, slices.Collect(c.RaceShipGroups(1)), 2)
}

func TestUnsafeCreateShips(t *testing.T) {
	c, _ := newCache()
	r := c.Race(Race_0_idx)
	r.Tech = r.Tech.Set(game.TechDrive, 1.001)
	r.Tech = r.Tech.Set(game.TechWeapons, 2.999)
	r.Tech = r.Tech.Set(game.TechShields, 3.1003)
	r.Tech = r.Tech.Set(game.TechCargo, 4.0005001)

	sgi := c.CreateShipsUnsafe_T(Race_0_idx, c.MustShipClass(Race_0_idx, Race_0_Freighter).ID, R0_Planet_0_num, 1)
	sg := c.ShipGroup(sgi)
	assert.Equal(t, 1.001, sg.Tech.Value(game.TechDrive))
	assert.Equal(t, 0., sg.Tech.Value(game.TechWeapons))
	assert.Equal(t, 3.100, sg.Tech.Value(game.TechShields))
	assert.Equal(t, 4.001, sg.Tech.Value(game.TechCargo))

	sgi = c.CreateShipsUnsafe_T(Race_0_idx, c.MustShipClass(Race_0_idx, Race_0_Gunship).ID, R0_Planet_0_num, 1)
	sg = c.ShipGroup(sgi)
	assert.Equal(t, 1.001, sg.Tech.Value(game.TechDrive))
	assert.Equal(t, 2.999, sg.Tech.Value(game.TechWeapons))
	assert.Equal(t, 3.100, sg.Tech.Value(game.TechShields))
	assert.Equal(t, 0., sg.Tech.Value(game.TechCargo))
}

func TestShipGroupMerge(t *testing.T) {
	c, g := newCache()

	assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Freighter, R0_Planet_0_num, 1)) // 1 -> 2
	assert.NoError(t, c.CreateShips(Race_1_idx, Race_1_Freighter, R1_Planet_1_num, 1))
	assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Freighter, R0_Planet_0_num, 6)) // (2)
	assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Gunship, R0_Planet_0_num, 2))   // (3)
	assert.NoError(t, c.CreateShips(Race_1_idx, Race_1_Gunship, R1_Planet_1_num, 1))

	c.RaceTechLevel(Race_0_idx, game.TechDrive, 1.5)
	assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Gunship, R0_Planet_0_num, 9))   // 4 -> 6
	assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Freighter, R0_Planet_0_num, 7)) // 5 -> 7
	assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Gunship, R0_Planet_0_num, 4))   // (6)
	assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Freighter, R0_Planet_0_num, 4)) // (7)

	assert.Len(t, slices.Collect(c.RaceShipGroups(Race_0_idx)), 7)

	c.RaceTechLevel(Race_1_idx, game.TechShields, 2.0)
	assert.Equal(t, 2.0, c.Race(Race_1_idx).Tech[game.TechShields].F())
	assert.NoError(t, c.CreateShips(1, Race_1_Freighter, R1_Planet_1_num, 1))
	assert.Len(t, slices.Collect(c.RaceShipGroups(Race_1_idx)), 3)

	assert.ErrorContains(t,
		g.ShipGroupMerge(UnknownRace),
		e.GenericErrorText(e.ErrInputUnknownRace))
	assert.ErrorContains(t,
		g.ShipGroupMerge(Race_Extinct.Name),
		e.GenericErrorText(e.ErrRaceExinct))

	assert.NoError(t, g.ShipGroupMerge(Race_0.Name))

	assert.Len(t, slices.Collect(c.RaceShipGroups(Race_1_idx)), 3)
	assert.Len(t, slices.Collect(c.RaceShipGroups(Race_0_idx)), 4)

	shipTypeID := func(ri int, name string) uuid.UUID {
		class, _, ok := c.ShipClass(ri, name)
		if !ok {
			t.Fatalf("ship_class not found: %s", name)
			return uuid.Nil
		}
		return class.ID
	}

	for sg := range c.RaceShipGroups(Race_0_idx) {
		switch {
		case sg.TypeID == shipTypeID(Race_0_idx, Race_0_Freighter) && sg.TechLevel(game.TechDrive) == 1.1:
			assert.Equal(t, uint(7), sg.Number)
			// assert.Equal(t, uint(1), sg.Index)
		case sg.TypeID == shipTypeID(Race_0_idx, Race_0_Freighter) && sg.TechLevel(game.TechDrive) == 1.5:
			assert.Equal(t, uint(11), sg.Number)
			// assert.Equal(t, uint(4), sg.Index)
		case sg.TypeID == shipTypeID(Race_0_idx, Race_0_Gunship) && sg.TechLevel(game.TechDrive) == 1.1:
			assert.Equal(t, uint(2), sg.Number)
			// assert.Equal(t, uint(2), sg.Index)
		case sg.TypeID == shipTypeID(Race_0_idx, Race_0_Gunship) && sg.TechLevel(game.TechDrive) == 1.5:
			assert.Equal(t, uint(13), sg.Number)
			// assert.Equal(t, uint(3), sg.Index)
		default:
			t.Error("not all ship groups covered")
		}
	}
}

func TestShipGroupBreak(t *testing.T) {
	c, g := newCache()
	assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Freighter, R0_Planet_0_num, 13)) // group #1 (0)
	assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Gunship, R0_Planet_0_num, 7))    // group #2 (1) - In_Space
	c.ShipGroup(1).StateInSpace = &InSpace

	fleet := "R0_Fleet"
	assert.NoError(t, g.ShipGroupJoinFleet(Race_0.Name, fleet, c.ShipGroup(0).ID))

	assert.ErrorContains(t,
		g.ShipGroupBreak(UnknownRace, c.ShipGroup(0).ID, uuid.New(), 1),
		e.GenericErrorText(e.ErrInputUnknownRace))
	assert.ErrorContains(t,
		g.ShipGroupBreak(Race_Extinct.Name, c.ShipGroup(0).ID, uuid.New(), 1),
		e.GenericErrorText(e.ErrRaceExinct))
	assert.ErrorContains(t,
		g.ShipGroupBreak(Race_0.Name, uuid.New(), uuid.New(), 1),
		e.GenericErrorText(e.ErrInputEntityNotExists))
	assert.ErrorContains(t,
		g.ShipGroupBreak(Race_0.Name, c.ShipGroup(0).ID, c.ShipGroup(0).ID, 1),
		e.GenericErrorText(e.ErrInputNewEntityDuplicateIdentifier))
	assert.ErrorContains(t,
		g.ShipGroupBreak(Race_0.Name, c.ShipGroup(0).ID, uuid.New(), 17),
		e.GenericErrorText(e.ErrBeakGroupNumberNotEnough))
	assert.ErrorContains(t,
		g.ShipGroupBreak(Race_0.Name, c.ShipGroup(1).ID, uuid.New(), 1),
		e.GenericErrorText(e.ErrShipsBusy))

	assert.Len(t, slices.Collect(c.RaceShipGroups(Race_0_idx)), 2)
	assert.Len(t, slices.Collect(c.ListFleets(Race_0_idx)), 1)

	// group #1 -> group #3 (5 new, 8 left)
	assert.NoError(t, c.ShipGroupBreak(Race_0_idx, c.ShipGroup(0).ID, uuid.New(), 5)) // group #3 (2)
	assert.Len(t, slices.Collect(c.RaceShipGroups(Race_0_idx)), 3)
	assert.Equal(t, uint(8), c.ShipGroup(0).Number)
	assert.NotNil(t, c.ShipGroup(0).FleetID)
	assert.Equal(t, uint(5), c.ShipGroup(2).Number)
	// assert.Equal(t, uint(3), c.ShipGroup(2).Index)
	assert.Nil(t, c.ShipGroup(2).FleetID)
	assert.Nil(t, c.ShipGroup(2).CargoType)

	// group #1 -> group #4 (2 new, 6 left)
	c.ShipGroup(0).CargoType = game.CargoColonist.Ref()
	c.ShipGroup(0).Load = 32.8                                                        // 8 ships
	assert.NoError(t, c.ShipGroupBreak(Race_0_idx, c.ShipGroup(0).ID, uuid.New(), 2)) // group #4 (3)
	assert.Len(t, slices.Collect(c.RaceShipGroups(Race_0_idx)), 4)
	assert.Equal(t, uint(6), c.ShipGroup(0).Number)
	assert.NotNil(t, c.ShipGroup(0).FleetID)
	assert.Equal(t, uint(2), c.ShipGroup(3).Number)
	// assert.Equal(t, uint(4), c.ShipGroup(3).Index)
	assert.Nil(t, c.ShipGroup(3).FleetID)
	assert.NoError(t, c.ShipGroupJoinFleet(Race_0_idx, fleet, c.ShipGroup(3).ID))
	assert.NotNil(t, c.ShipGroup(3).FleetID)

	assert.Equal(t, game.CargoColonist.Ref(), c.ShipGroup(0).CargoType)
	assert.Equal(t, 24.6, util.Fixed3(c.ShipGroup(0).Load.F()))
	assert.Equal(t, game.CargoColonist.Ref(), c.ShipGroup(3).CargoType)
	assert.Equal(t, 8.2, util.Fixed3(c.ShipGroup(3).Load.F()))

	// group #1 -> MAX 6 off the fleet
	assert.NoError(t, g.ShipGroupBreak(Race_0.Name, c.ShipGroup(0).ID, uuid.New(), 6)) // group #1 (0)
	assert.Len(t, slices.Collect(c.RaceShipGroups(Race_0_idx)), 4)
	assert.Equal(t, uint(6), c.ShipGroup(0).Number)
	assert.Nil(t, c.ShipGroup(0).FleetID)

	// group #4 -> ALL off the fleet
	assert.NoError(t, g.ShipGroupBreak(Race_0.Name, c.ShipGroup(3).ID, uuid.New(), 0)) // group #1 (0)
	assert.Len(t, slices.Collect(c.RaceShipGroups(Race_0_idx)), 4)
	assert.Equal(t, uint(2), c.ShipGroup(3).Number)
	assert.Nil(t, c.ShipGroup(3).FleetID)
}

func TestShipGroupTransfer(t *testing.T) {
	c, g := newCache()
	assert.NoError(t, c.CreateShips(Race_0_idx, ShipType_Cruiser, R0_Planet_0_num, 11)) // group #1 (0)
	assert.NoError(t, c.CreateShips(Race_1_idx, ShipType_Cruiser, R1_Planet_1_num, 23)) // group #2 (1)

	assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Gunship, R0_Planet_0_num, 17)) // group #3 (2) - In_Space
	assert.NoError(t, g.ShipGroupJoinFleet(Race_0.Name, "R0_Fleet", c.ShipGroup(2).ID))
	assert.NotNil(t, c.ShipGroup(2).FleetID)
	c.ShipGroup(2).StateInSpace = &InSpace
	c.ShipGroup(2).CargoType = game.CargoMaterial.Ref()
	c.ShipGroup(2).Load = 1.234

	assert.Len(t, slices.Collect(c.RaceShipGroups(Race_0_idx)), 2)
	assert.Len(t, slices.Collect(c.RaceShipGroups(Race_1_idx)), 1)

	assert.ErrorContains(t,
		g.ShipGroupTransfer(UnknownRace, Race_1.Name, c.ShipGroup(1).ID),
		e.GenericErrorText(e.ErrInputUnknownRace))
	assert.ErrorContains(t,
		g.ShipGroupTransfer(Race_0.Name, UnknownRace, c.ShipGroup(1).ID),
		e.GenericErrorText(e.ErrInputUnknownRace))
	assert.ErrorContains(t,
		g.ShipGroupTransfer(Race_0.Name, Race_Extinct.Name, c.ShipGroup(1).ID),
		e.GenericErrorText(e.ErrRaceExinct))
	assert.ErrorContains(t,
		g.ShipGroupTransfer(Race_Extinct.Name, Race_1.Name, c.ShipGroup(1).ID),
		e.GenericErrorText(e.ErrRaceExinct))
	assert.ErrorContains(t,
		g.ShipGroupTransfer(Race_0.Name, Race_0.Name, c.ShipGroup(1).ID),
		e.GenericErrorText(e.ErrInputSameRace))
	assert.ErrorContains(t,
		g.ShipGroupTransfer(Race_0.Name, Race_1.Name, uuid.New()),
		e.GenericErrorText(e.ErrInputEntityNotExists))

	orig := *c.ShipGroup(2)
	assert.NoError(t, g.ShipGroupTransfer(Race_0.Name, Race_1.Name, c.ShipGroup(2).ID)) // group #2 (3)
	assert.Len(t, slices.Collect(c.RaceShipGroups(Race_0_idx)), 1)
	assert.Len(t, slices.Collect(c.RaceShipGroups(Race_1_idx)), 2)

	newSg := c.ShipGroup(2)
	assert.Equal(t, c.MustShipClass(Race_1_idx, Race_0_Gunship).Name, c.MustShipClass(Race_0_idx, Race_0_Gunship).Name)
	assert.Equal(t, c.MustShipClass(Race_1_idx, Race_0_Gunship).Drive, c.MustShipClass(Race_0_idx, Race_0_Gunship).Drive)
	assert.Equal(t, c.MustShipClass(Race_1_idx, Race_0_Gunship).Weapons, c.MustShipClass(Race_0_idx, Race_0_Gunship).Weapons)
	assert.Equal(t, c.MustShipClass(Race_1_idx, Race_0_Gunship).Shields, c.MustShipClass(Race_0_idx, Race_0_Gunship).Shields)
	assert.Equal(t, c.MustShipClass(Race_1_idx, Race_0_Gunship).Cargo, c.MustShipClass(Race_0_idx, Race_0_Gunship).Cargo)
	assert.Equal(t, c.MustShipClass(Race_1_idx, Race_0_Gunship).Armament, c.MustShipClass(Race_0_idx, Race_0_Gunship).Armament)
	assert.Equal(t, orig.State(), newSg.State())
	assert.Equal(t, orig.CargoType, newSg.CargoType)
	assert.Equal(t, orig.Load, newSg.Load)
	assert.Equal(t, orig.TechLevel(game.TechDrive), newSg.TechLevel(game.TechDrive))
	assert.Equal(t, orig.TechLevel(game.TechWeapons), newSg.TechLevel(game.TechWeapons))
	assert.Equal(t, orig.TechLevel(game.TechShields), newSg.TechLevel(game.TechShields))
	assert.Equal(t, orig.TechLevel(game.TechCargo), newSg.TechLevel(game.TechCargo))
	assert.Equal(t, orig.Destination, newSg.Destination)
	assert.Equal(t, orig.StateInSpace, newSg.StateInSpace)
	assert.Equal(t, orig.StateUpgrade, newSg.StateUpgrade)
	assert.Equal(t, orig.StateTransfer, newSg.StateTransfer)
	assert.Equal(t, newSg.TypeID, c.MustShipClass(Race_1_idx, Race_0_Gunship).ID)
	assert.Equal(t, orig.Number, newSg.Number)
	assert.Equal(t, Race_1_ID, newSg.OwnerID)
	assert.Nil(t, newSg.FleetID)

	assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Gunship, R0_Planet_0_num, 1))
	assert.Len(t, slices.Collect(c.RaceShipGroups(Race_0_idx)), 2)
	sg := c.ShipGroup(3)
	assert.Equal(t, game.StateInOrbit, sg.State())
	assert.NoError(t, g.ShipGroupSend(Race_0.Name, sg.ID, R0_Planet_2_num))
	assert.Equal(t, game.StateLaunched, sg.State())
	assert.Equal(t, sg.OwnerID, Race_0_ID)

	assert.NoError(t, g.ShipGroupTransfer(Race_0.Name, Race_1.Name, sg.ID))
	sg = c.ShipGroup(3)
	assert.Len(t, slices.Collect(c.RaceShipGroups(Race_0_idx)), 1)
	assert.Len(t, slices.Collect(c.RaceShipGroups(Race_1_idx)), 3)
	assert.Equal(t, game.StateTransfer, sg.State())
	assert.Equal(t, sg.OwnerID, Race_1_ID)
	assert.ErrorContains(t,
		g.ShipGroupTransfer(Race_1.Name, Race_0.Name, sg.ID),
		e.GenericErrorText(e.ErrShipsBusy))

	// transfer ship class with existing name
	originalName := c.MustShipClass(Race_0_idx, ShipType_Cruiser).Name
	assert.NoError(t, g.ShipGroupTransfer(Race_0.Name, Race_1.Name, c.ShipGroup(0).ID))
	var s *game.ShipType
	for st := range c.ListShipTypes(Race_1_idx) {
		if strings.HasPrefix(st.Name, originalName) && st.Name != originalName {
			s = st
		}
	}
	assert.NotNil(t, s)
	assert.Greater(t, len(s.Name), len(originalName))
}

func TestShipGroupLoad(t *testing.T) {
	c, g := newCache()

	// 1: idx = 0 / Ready to load
	assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Freighter, R0_Planet_0_num, 11))

	// 2: idx = 1 / Has no cargo bay
	assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Gunship, R0_Planet_0_num, 1))

	// 3: idx = 2 / In_Space
	assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Freighter, R0_Planet_0_num, 7))
	c.ShipGroup(2).StateInSpace = &InSpace

	// 4: idx = 3 / loaded with COL
	assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Freighter, R0_Planet_0_num, 11))
	c.ShipGroup(3).CargoType = game.CargoColonist.Ref()
	c.ShipGroup(3).Load = 1.234

	// 5: idx = 4 / on foreign planet
	assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Freighter, R0_Planet_0_num, 11))
	c.ShipGroup(4).Destination = R1_Planet_1_num

	assert.Len(t, slices.Collect(c.RaceShipGroups(Race_0_idx)), 5)

	// tests
	assert.ErrorContains(t,
		g.ShipGroupLoad(UnknownRace, c.ShipGroup(0).ID, game.CargoMaterial.String(), 0),
		e.GenericErrorText(e.ErrInputUnknownRace))
	assert.ErrorContains(t,
		g.ShipGroupLoad(Race_Extinct.Name, c.ShipGroup(0).ID, game.CargoMaterial.String(), 0),
		e.GenericErrorText(e.ErrRaceExinct))
	assert.ErrorContains(t,
		g.ShipGroupLoad(Race_0.Name, c.ShipGroup(0).ID, "GOLD", 0),
		e.GenericErrorText(e.ErrInputCargoTypeInvalid))
	assert.ErrorContains(t,
		g.ShipGroupLoad(Race_0.Name, uuid.New(), game.CargoMaterial.String(), 0),
		e.GenericErrorText(e.ErrInputEntityNotExists))
	assert.ErrorContains(t,
		g.ShipGroupLoad(Race_0.Name, c.ShipGroup(2).ID, game.CargoMaterial.String(), 0),
		e.GenericErrorText(e.ErrShipsBusy))
	assert.ErrorContains(t,
		g.ShipGroupLoad(Race_0.Name, c.ShipGroup(4).ID, game.CargoMaterial.String(), 0),
		e.GenericErrorText(e.ErrInputEntityNotOwned))
	assert.ErrorContains(t,
		g.ShipGroupLoad(Race_0.Name, c.ShipGroup(1).ID, game.CargoMaterial.String(), 0),
		e.GenericErrorText(e.ErrInputNoCargoBay))
	assert.ErrorContains(t,
		g.ShipGroupLoad(Race_0.Name, c.ShipGroup(3).ID, game.CargoMaterial.String(), 0),
		e.GenericErrorText(e.ErrInputCargoLoadNotEqual))

	// initial planet is empty
	assert.ErrorContains(t,
		g.ShipGroupLoad(Race_0.Name, c.ShipGroup(0).ID, game.CargoMaterial.String(), 0),
		e.GenericErrorText(e.ErrInputCargoLoadNotEnough))
	// add cargo to planet
	c.PutMaterial(R0_Planet_0_num, 100)
	// not enough on the planet
	assert.ErrorContains(t,
		g.ShipGroupLoad(Race_0.Name, c.ShipGroup(0).ID, game.CargoMaterial.String(), 101),
		e.GenericErrorText(e.ErrInputCargoLoadNotEnough))

	assert.Len(t, slices.Collect(c.RaceShipGroups(Race_0_idx)), 5)

	// load maximum
	assert.NoError(t, g.ShipGroupLoad(Race_0.Name, c.ShipGroup(0).ID, game.CargoMaterial.String(), 0))
	assert.Equal(t, 0.0, c.MustPlanet(R0_Planet_0_num).Material.F())
	assert.Equal(t, game.CargoMaterial.Ref(), c.ShipGroup(0).CargoType)
	assert.Equal(t, 100.0, c.ShipGroup(0).Load.F())

	assert.NoError(t, g.ShipGroupUnload(Race_0.Name, c.ShipGroup(0).ID, 0))

	// load limited
	assert.NoError(t, g.ShipGroupLoad(Race_0.Name, c.ShipGroup(0).ID, game.CargoMaterial.String(), 18))
	assert.Equal(t, 82.0, c.MustPlanet(R0_Planet_0_num).Material.F())
	assert.Equal(t, game.CargoMaterial.Ref(), c.ShipGroup(0).CargoType)
	assert.Equal(t, 18.0, c.ShipGroup(0).Load.F())

	assert.NoError(t, g.ShipGroupUnload(Race_0.Name, c.ShipGroup(0).ID, 0))

	// add cargo to planet
	c.PutMaterial(R0_Planet_0_num, 100)
	// loading all available cargo
	assert.NoError(t, g.ShipGroupLoad(Race_0.Name, c.ShipGroup(0).ID, game.CargoMaterial.String(), 0))
	assert.Equal(t, 0.0, c.MustPlanet(R0_Planet_0_num).Material.F())
	assert.Equal(t, 100.0, c.ShipGroup(0).Load.F()) // free: 131.0
	assert.Equal(t, game.CargoMaterial.Ref(), c.ShipGroup(0).CargoType)

	// add cargo to planet
	c.PutMaterial(R0_Planet_0_num, 200)
	assert.NoError(t, g.ShipGroupLoad(Race_0.Name, c.ShipGroup(0).ID, game.CargoMaterial.String(), 31))
	assert.Equal(t, 169.0, c.MustPlanet(R0_Planet_0_num).Material.F())
	assert.Equal(t, 131.0, c.ShipGroup(0).Load.F()) // free: 100.0
	assert.Equal(t, game.CargoMaterial.Ref(), c.ShipGroup(0).CargoType)

	// load to maximum cargo space left
	assert.NoError(t, g.ShipGroupLoad(Race_0.Name, c.ShipGroup(0).ID, game.CargoMaterial.String(), 0))
	assert.Equal(t, 69.0, c.MustPlanet(R0_Planet_0_num).Material.F())
	assert.Equal(t, 231.0, c.ShipGroup(0).Load.F()) // free: 0.0
	assert.Equal(t, game.CargoMaterial.Ref(), c.ShipGroup(0).CargoType)

	// ship group is full
	assert.ErrorContains(t,
		g.ShipGroupLoad(Race_0.Name, c.ShipGroup(0).ID, game.CargoMaterial.String(), 0),
		e.GenericErrorText(e.ErrInputCargoLoadNoSpaceLeft))
}

func TestShipGroupUnload(t *testing.T) {
	c, g := newCache()

	// 1: idx = 0 / empty
	assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Freighter, R0_Planet_0_num, 10))

	// 2: idx = 1 / Has no cargo bay
	assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Gunship, R0_Planet_0_num, 1))

	// 3: idx = 2 / In_Space
	assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Freighter, R0_Planet_0_num, 7))
	c.ShipGroup(2).StateInSpace = &InSpace

	// 4: idx = 3 / loaded with COL
	assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Freighter, R0_Planet_0_num, 11))
	c.ShipGroup(3).CargoType = game.CargoColonist.Ref()
	c.ShipGroup(3).Load = 1.234

	// 5: idx = 4 / on foreign planet / loaded with COL
	assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Freighter, R0_Planet_0_num, 11))
	c.ShipGroup(4).Destination = R1_Planet_1_num
	c.ShipGroup(4).CargoType = game.CargoColonist.Ref()
	c.ShipGroup(4).Load = 1.234

	// 6: idx = 5 / on foreign planet / loaded with MAT
	assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Freighter, R0_Planet_0_num, 11))
	c.ShipGroup(5).Destination = R1_Planet_1_num
	c.ShipGroup(5).CargoType = game.CargoMaterial.Ref()
	c.ShipGroup(5).Load = 100.0

	assert.Len(t, slices.Collect(c.RaceShipGroups(Race_0_idx)), 6)

	// tests
	assert.ErrorContains(t,
		g.ShipGroupUnload(UnknownRace, c.ShipGroup(0).ID, 0),
		e.GenericErrorText(e.ErrInputUnknownRace))
	assert.ErrorContains(t,
		g.ShipGroupUnload(Race_Extinct.Name, c.ShipGroup(0).ID, 0),
		e.GenericErrorText(e.ErrRaceExinct))
	assert.ErrorContains(t,
		g.ShipGroupUnload(Race_0.Name, uuid.New(), 0),
		e.GenericErrorText(e.ErrInputEntityNotExists))
	assert.ErrorContains(t,
		g.ShipGroupUnload(Race_0.Name, c.ShipGroup(2).ID, 0),
		e.GenericErrorText(e.ErrShipsBusy))
	assert.ErrorContains(t,
		g.ShipGroupUnload(Race_0.Name, c.ShipGroup(1).ID, 0),
		e.GenericErrorText(e.ErrInputNoCargoBay))
	assert.ErrorContains(t,
		g.ShipGroupUnload(Race_0.Name, c.ShipGroup(0).ID, 0),
		e.GenericErrorText(e.ErrInputCargoUnloadEmpty))
	assert.ErrorContains(t,
		g.ShipGroupUnload(Race_0.Name, c.ShipGroup(4).ID, 0),
		e.GenericErrorText(e.ErrInputEntityNotOwned))

	assert.Len(t, slices.Collect(c.RaceShipGroups(Race_0_idx)), 6)

	// unload MAT on foreign planet / limited
	assert.NoError(t, g.ShipGroupUnload(Race_0.Name, c.ShipGroup(5).ID, 20.1))
	assert.Equal(t, 20.1, util.Fixed3(c.MustPlanet(R1_Planet_1_num).Material.F()))
	assert.Equal(t, game.CargoMaterial.Ref(), c.ShipGroup(5).CargoType)
	assert.Equal(t, 79.9, util.Fixed3(c.ShipGroup(5).Load.F()))

	// unload MAT on foreign planet / ALL
	assert.NoError(t, g.ShipGroupUnload(Race_0.Name, c.ShipGroup(5).ID, 0))
	assert.Equal(t, 100.0, util.Fixed3(c.MustPlanet(R1_Planet_1_num).Material.F()))
	assert.Equal(t, 0.0, util.Fixed3(c.ShipGroup(5).Load.F()))
	assert.Nil(t, c.ShipGroup(5).CargoType)

	// unload ALL
	c.ShipGroup(0).CargoType = game.CargoColonist.Ref()
	c.ShipGroup(0).Load = 100
	assert.NoError(t, g.ShipGroupUnload(Race_0.Name, c.ShipGroup(0).ID, 101))
	assert.Equal(t, 100.0, util.Fixed3(c.MustPlanet(R0_Planet_0_num).Colonists.F()))
	assert.Equal(t, 0.0, util.Fixed3(c.ShipGroup(0).Load.F()))
	assert.Nil(t, c.ShipGroup(0).CargoType)
}

func TestShipGroupDismantle(t *testing.T) {
	c, g := newCache()

	// 1: idx = 0 / empty
	assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Freighter, R0_Planet_0_num, 10))

	// 2: idx = 1 / In_Space
	assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Freighter, R0_Planet_0_num, 7))
	c.ShipGroup(1).StateInSpace = &InSpace

	// 3: idx = 2 / loaded with COL
	assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Freighter, R0_Planet_0_num, 10))
	c.ShipGroup(2).CargoType = game.CargoColonist.Ref()
	c.ShipGroup(2).Load = 80.0

	// 4: idx = 3 / on foreign planet / loaded with MAT
	assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Freighter, R0_Planet_0_num, 11))
	c.ShipGroup(3).Destination = R1_Planet_1_num
	c.ShipGroup(3).CargoType = game.CargoMaterial.Ref()
	c.ShipGroup(3).Load = 100.0

	// 5: idx = 4 / on foreign planet / loaded with COL
	assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Freighter, R0_Planet_0_num, 11))
	c.ShipGroup(4).Destination = R1_Planet_1_num
	c.ShipGroup(4).CargoType = game.CargoColonist.Ref()
	c.ShipGroup(4).Load = 2.345

	assert.Len(t, slices.Collect(c.RaceShipGroups(Race_0_idx)), 5)

	// tests
	assert.ErrorContains(t,
		g.ShipGroupDismantle(UnknownRace, c.ShipGroup(0).ID),
		e.GenericErrorText(e.ErrInputUnknownRace))
	assert.ErrorContains(t,
		g.ShipGroupDismantle(Race_Extinct.Name, c.ShipGroup(0).ID),
		e.GenericErrorText(e.ErrRaceExinct))
	assert.ErrorContains(t,
		g.ShipGroupDismantle(Race_0.Name, uuid.New()),
		e.GenericErrorText(e.ErrInputEntityNotExists))
	assert.ErrorContains(t,
		g.ShipGroupDismantle(Race_0.Name, c.ShipGroup(1).ID),
		e.GenericErrorText(e.ErrShipsBusy))

	groupEmptyMass := c.ShipGroup(4).EmptyMass(c.MustShipClass(Race_0_idx, Race_0_Freighter))
	planetMAT := c.MustPlanet(R1_Planet_1_num).Material.F()
	planetCOL := c.MustPlanet(R1_Planet_1_num).Colonists.F()

	assert.NoError(t, g.ShipGroupDismantle(Race_0.Name, c.ShipGroup(4).ID))
	assert.Len(t, slices.Collect(c.RaceShipGroups(Race_0_idx)), 4)
	assert.Equal(t, planetMAT+groupEmptyMass, c.MustPlanet(R1_Planet_1_num).Material.F())
	assert.Equal(t, planetCOL, c.MustPlanet(R1_Planet_1_num).Colonists.F())

	groupEmptyMass = c.ShipGroup(3).EmptyMass(c.MustShipClass(Race_0_idx, Race_0_Freighter))
	groupLoadMAT := c.ShipGroup(3).Load.F()
	planetMAT = c.MustPlanet(R1_Planet_1_num).Material.F()
	assert.NoError(t, g.ShipGroupDismantle(Race_0.Name, c.ShipGroup(3).ID))
	assert.Len(t, slices.Collect(c.RaceShipGroups(Race_0_idx)), 3)
	assert.Equal(t, planetMAT+groupEmptyMass+groupLoadMAT, c.MustPlanet(R1_Planet_1_num).Material.F())
}

func TestShipGroupDestroyItem(t *testing.T) {
	c, _ := newCache()

	assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Freighter, R0_Planet_0_num, 10))
	c.ShipGroup(0).CargoType = game.CargoColonist.Ref()
	c.ShipGroup(0).Load = 100.0

	for c.ShipGroup(0).Number > 0 {
		c.ShipGroupDestroyItem(0)
		assert.Equal(t, float64(c.ShipGroup(0).Number)*10, c.ShipGroup(0).Load.F())
	}
}

func TestState(t *testing.T) {
	assert.Equal(t, "In_Orbit", fmt.Sprintf("%s", game.StateInOrbit))
}

func TestUnsafeDeleteShipGroup(t *testing.T) {
	c, g := newCache()

	assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Gunship, R0_Planet_0_num, 3)) // 0
	assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Gunship, R0_Planet_2_num, 5)) // 1
	assert.NoError(t, g.ShipGroupJoinFleet(Race_0.Name, "Fleet", c.ShipGroup(0).ID))
	assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Freighter, R0_Planet_2_num, 7)) // 2

	assert.Len(t, slices.Collect(c.RaceShipGroups(Race_0_idx)), 3)

	c.UnsafeDeleteShipGroup(1)

	assert.Len(t, slices.Collect(c.RaceShipGroups(Race_0_idx)), 2)
	assert.Equal(t, uint(3), c.ShipGroup(0).Number)
	assert.Equal(t, uint(7), c.ShipGroup(1).Number)
}
