package controller_test

import (
	"slices"
	"testing"

	e "galaxy/error"

	"galaxy/game/internal/model/game"

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

func TestFleetSend(t *testing.T) {
	c, g := newCache()
	// group #1 - in_orbit Planet_0
	assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Freighter, R0_Planet_0_num, 1))
	// group #2 - in_space (later)
	assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Gunship, R0_Planet_0_num, 3))
	// group #3 - in_orbit Planet_0, unmovable
	g.ShipClassCreate(Race_0.Name, "Fortress", 0, 50, 30, 100, 0)
	assert.NoError(t, c.CreateShips(Race_0_idx, "Fortress", R0_Planet_0_num, 1))
	// group #4 - in_orbit Planet_0
	assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Gunship, R0_Planet_0_num, 2))

	// ensure race has no Fleets
	assert.Len(t, slices.Collect(c.ListFleets(Race_0_idx)), 0)

	fleetSending := "R0_Fleet_one"
	fleetInSpace := "R0_Fleet_inSpace"
	fleetUnmovable := "R0_Fleet_unmovable"
	fleetUnmovable2 := "R0_Fleet_unmovable2"

	assert.NoError(t, g.ShipGroupJoinFleet(Race_0.Name, fleetSending, c.ShipGroup(0).ID))
	assert.Len(t, slices.Collect(c.ListFleets(Race_0_idx)), 1)

	assert.NoError(t, g.ShipGroupJoinFleet(Race_0.Name, fleetSending, c.ShipGroup(2).ID))
	assert.Len(t, slices.Collect(c.ListFleets(Race_0_idx)), 1)

	assert.NoError(t, g.ShipGroupJoinFleet(Race_0.Name, fleetInSpace, c.ShipGroup(1).ID))
	assert.Len(t, slices.Collect(c.ListFleets(Race_0_idx)), 2)

	assert.NoError(t, g.ShipGroupJoinFleet(Race_0.Name, fleetUnmovable, c.ShipGroup(2).ID))
	assert.Len(t, slices.Collect(c.ListFleets(Race_0_idx)), 3)

	assert.NoError(t, g.ShipGroupJoinFleet(Race_0.Name, fleetUnmovable2, c.ShipGroup(3).ID))
	assert.Len(t, slices.Collect(c.ListFleets(Race_0_idx)), 4)

	assert.NoError(t, g.ShipGroupJoinFleet(Race_0.Name, fleetUnmovable, c.ShipGroup(3).ID))
	assert.Len(t, slices.Collect(c.ListFleets(Race_0_idx)), 3)

	// group #2 - in_space
	c.ShipGroup(1).StateInSpace = &InSpace

	assert.ErrorContains(t,
		g.FleetSend(UnknownRace, fleetSending, 2),
		e.GenericErrorText(e.ErrInputUnknownRace))
	assert.ErrorContains(t,
		g.FleetSend(Race_Extinct.Name, fleetSending, 2),
		e.GenericErrorText(e.ErrRaceExinct))
	assert.ErrorContains(t,
		g.FleetSend(Race_0.Name, "UnknownFleet", 2),
		e.GenericErrorText(e.ErrInputEntityNotExists))
	assert.ErrorContains(t,
		g.FleetSend(Race_0.Name, fleetInSpace, 2),
		e.GenericErrorText(e.ErrShipsBusy))
	assert.ErrorContains(t,
		g.FleetSend(Race_0.Name, fleetSending, 200),
		e.GenericErrorText(e.ErrInputEntityNotExists))
	assert.ErrorContains(t,
		g.FleetSend(Race_0.Name, fleetSending, 3),
		e.GenericErrorText(e.ErrSendUnreachableDestination))
	assert.ErrorContains(t,
		g.FleetSend(Race_0.Name, fleetUnmovable, 2),
		e.GenericErrorText(e.ErrSendShipHasNoDrives))

	assert.NoError(t, g.FleetSend(Race_0.Name, fleetSending, 2))
	fleetState := c.FleetState(c.MustFleetID(Race_0_idx, fleetSending))
	assert.Equal(t, game.StateLaunched, fleetState.State)
	for sgi := range c.FleetGroupIdx(Race_0_idx, c.MustFleetIndex(c.MustFleetID(Race_0_idx, fleetSending))) {
		assert.Equal(t, game.StateLaunched, c.ShipGroup(sgi).State())
	}

	assert.NoError(t, g.FleetSend(Race_0.Name, fleetSending, 0))
	fleetState = c.FleetState(c.MustFleetID(Race_0_idx, fleetSending))
	assert.Equal(t, game.StateInOrbit, fleetState.State)
	for sgi := range c.FleetGroupIdx(Race_0_idx, c.MustFleetIndex(c.MustFleetID(Race_0_idx, fleetSending))) {
		assert.Equal(t, game.StateInOrbit, c.ShipGroup(sgi).State())
	}
}
