package controller_test

import (
	"testing"

	e "galaxy/error"

	"galaxy/game/internal/controller"
	"galaxy/game/internal/model/game"

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

func TestScienceCreate(t *testing.T) {
	c, g := newCache()
	first := "Drive_Shields"
	second := "Hyperdrive"

	assert.Len(t, c.RaceScience(Race_0_idx), 0)
	assert.NoError(t, g.ScienceCreate(Race_0.Name, first, 0.4, 0, 0.6, 0))
	assert.Len(t, c.RaceScience(Race_0_idx), 1)
	sc := c.RaceScience(Race_0_idx)[0]
	assert.NoError(t, uuid.Validate(sc.ID.String()))
	assert.Equal(t, first, sc.Name)
	assert.Equal(t, 0.4, sc.Drive.F())
	assert.Equal(t, 0., sc.Weapons.F())
	assert.Equal(t, 0.6, sc.Shields.F())
	assert.Equal(t, 0., sc.Cargo.F())

	assert.ErrorContains(t,
		g.ScienceCreate(UnknownRace, second, 0.4, 0, 0.6, 0),
		e.GenericErrorText(e.ErrInputUnknownRace))
	assert.ErrorContains(t,
		g.ScienceCreate(Race_Extinct.Name, second, 0.4, 0, 0.6, 0),
		e.GenericErrorText(e.ErrRaceExinct))
	assert.ErrorContains(t,
		g.ScienceCreate(Race_0.Name, BadEntityName, 0.4, 0, 0.6, 0),
		e.GenericErrorText(e.ErrInputEntityTypeNameInvalid))
	assert.ErrorContains(t,
		g.ScienceCreate(Race_0.Name, first, 0.4, 0, 0.6, 0),
		e.GenericErrorText(e.ErrInputNewEntityDuplicateIdentifier))
	assert.ErrorContains(t,
		g.ScienceCreate(Race_0.Name, second, -0.1, 0, 1.1, 0),
		e.GenericErrorText(e.ErrInputDriveValue))
	assert.ErrorContains(t,
		g.ScienceCreate(Race_0.Name, second, 1.5, -0.5, 0, 0),
		e.GenericErrorText(e.ErrInputWeaponsValue))
	assert.ErrorContains(t,
		g.ScienceCreate(Race_0.Name, second, 1.3, 0, -0.3, 0),
		e.GenericErrorText(e.ErrInputShieldsValue))
	assert.ErrorContains(t,
		g.ScienceCreate(Race_0.Name, second, 0, 1.07, 0, -0.07),
		e.GenericErrorText(e.ErrInputCargoValue))
	assert.ErrorContains(t,
		g.ScienceCreate(Race_0.Name, second, 0.26, 0.25, 0.25, 0.25),
		e.GenericErrorText(e.ErrInputScienceSumValues))
	assert.ErrorContains(t,
		g.ScienceCreate(Race_0.Name, second, 0.25, 0.26, 0.25, 0.25),
		e.GenericErrorText(e.ErrInputScienceSumValues))
	assert.ErrorContains(t,
		g.ScienceCreate(Race_0.Name, second, 0.25, 0.25, 0.26, 0.25),
		e.GenericErrorText(e.ErrInputScienceSumValues))
	assert.ErrorContains(t,
		g.ScienceCreate(Race_0.Name, second, 0.25, 0.25, 0.25, 0.26),
		e.GenericErrorText(e.ErrInputScienceSumValues))

	assert.NoError(t, g.ScienceCreate(Race_0.Name, second, 0.25, 0.25, 0.25, 0.25))
	assert.Len(t, c.RaceScience(Race_0_idx), 2)
	sc = c.RaceScience(Race_0_idx)[1]
	assert.NoError(t, uuid.Validate(sc.ID.String()))
	assert.Equal(t, second, sc.Name)
	assert.Equal(t, 0.25, sc.Drive.F())
	assert.Equal(t, 0.25, sc.Weapons.F())
	assert.Equal(t, 0.25, sc.Shields.F())
	assert.Equal(t, 0.25, sc.Cargo.F())
}

func TestScienceRemove(t *testing.T) {
	c, g := newCache()
	first := "Drive_Shields"
	second := "Hyperdrive"
	assert.Len(t, c.RaceScience(Race_0_idx), 0)
	assert.NoError(t, g.ScienceCreate(Race_0.Name, first, 0.4, 0, 0.6, 0))
	assert.NoError(t, g.ScienceCreate(Race_0.Name, second, 0.25, 0.25, 0.25, 0.25))
	assert.Len(t, c.RaceScience(Race_0_idx), 2)

	assert.NoError(t, g.ScienceRemove(Race_0.Name, first))
	assert.Len(t, c.RaceScience(Race_0_idx), 1)

	g.PlanetProduce(Race_0.Name, int(R0_Planet_0_num), "SCIENCE", second)

	assert.ErrorContains(t,
		g.ScienceRemove(UnknownRace, second),
		e.GenericErrorText(e.ErrInputUnknownRace))
	assert.ErrorContains(t,
		g.ScienceRemove(Race_Extinct.Name, second),
		e.GenericErrorText(e.ErrRaceExinct))
	assert.ErrorContains(t,
		g.ScienceRemove(Race_0.Name, first),
		e.GenericErrorText(e.ErrInputEntityNotExists))
	assert.ErrorContains(t,
		g.ScienceRemove(Race_0.Name, second),
		e.GenericErrorText(e.ErrDeleteSciencePlanetProduction))
}

func TestResearchTech(t *testing.T) {
	r := Race_0
	rr := &r
	assert.Equal(t, 1.1, rr.Tech.Value(game.TechDrive))
	assert.Equal(t, 1.2, rr.Tech.Value(game.TechWeapons))
	assert.Equal(t, 1.3, rr.Tech.Value(game.TechShields))
	assert.Equal(t, 1.4, rr.Tech.Value(game.TechCargo))

	controller.ResearchTech(rr, 500, 1.0, 0.0, 0.0, 0.0)
	assert.InDelta(t, 1.2, rr.Tech.Value(game.TechDrive), 0.000001)
	assert.Equal(t, 1.2, rr.Tech.Value(game.TechWeapons))
	assert.Equal(t, 1.3, rr.Tech.Value(game.TechShields))
	assert.Equal(t, 1.4, rr.Tech.Value(game.TechCargo))

	controller.ResearchTech(rr, 500, 0.0, 0.5, 0.0, 0.5)
	assert.InDelta(t, 1.2, rr.Tech.Value(game.TechDrive), 0.000001)
	assert.Equal(t, 1.25, rr.Tech.Value(game.TechWeapons))
	assert.Equal(t, 1.3, rr.Tech.Value(game.TechShields))
	assert.Equal(t, 1.45, rr.Tech.Value(game.TechCargo))

	controller.ResearchTech(rr, 500, 0.5, 0.0, 0.5, 0.0)
	assert.InDelta(t, 1.25, rr.Tech.Value(game.TechDrive), 0.000001)
	assert.Equal(t, 1.25, rr.Tech.Value(game.TechWeapons))
	assert.Equal(t, 1.35, rr.Tech.Value(game.TechShields))
	assert.Equal(t, 1.45, rr.Tech.Value(game.TechCargo))

	controller.ResearchTech(rr, 1000, 0.0, 1.0, 0.0, 0.0)
	assert.InDelta(t, 1.25, rr.Tech.Value(game.TechDrive), 0.000001)
	assert.Equal(t, 1.45, rr.Tech.Value(game.TechWeapons))
	assert.Equal(t, 1.35, rr.Tech.Value(game.TechShields))
	assert.Equal(t, 1.45, rr.Tech.Value(game.TechCargo))
}
