package controller_test

import (
	"testing"

	e "galaxy/error"

	"galaxy/game/internal/model/game"

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

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

	assert.Equal(t, c.Voted(Race_0_idx), Race_0_idx)
	assert.Equal(t, c.Voted(Race_1_idx), Race_1_idx)

	assert.NoError(t, g.RaceVote(Race_0.Name, Race_1.Name))
	assert.Equal(t, Race_1_idx, c.Voted(Race_0_idx))
	assert.Equal(t, Race_1_idx, c.Voted(Race_1_idx))

	assert.ErrorContains(t,
		g.RaceVote(UnknownRace, Race_1.Name),
		e.GenericErrorText(e.ErrInputUnknownRace))
	assert.ErrorContains(t,
		g.RaceVote(Race_0.Name, UnknownRace),
		e.GenericErrorText(e.ErrInputUnknownRace))
	assert.ErrorContains(t,
		g.RaceVote(Race_0.Name, Race_Extinct.Name),
		e.GenericErrorText(e.ErrRaceExinct))
	assert.ErrorContains(t,
		g.RaceVote(Race_Extinct.Name, Race_1.Name),
		e.GenericErrorText(e.ErrRaceExinct))
}

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

	assert.NoError(t, g.RaceRelation(Race_0.Name, Race_1.Name, "war"))
	assert.NoError(t, g.RaceRelation(Race_1.Name, Race_0.Name, "PEACE"))

	assert.Equal(t, game.RelationWar, c.Relation(Race_0_idx, Race_1_idx))
	assert.Equal(t, game.RelationPeace, c.Relation(Race_1_idx, Race_0_idx))

	assert.ErrorContains(t,
		g.RaceRelation(Race_0.Name, Race_1.Name, "Wojna"),
		e.GenericErrorText(e.ErrInputUnknownRelation))
	assert.ErrorContains(t,
		g.RaceRelation(Race_0.Name, UnknownRace, "War"),
		e.GenericErrorText(e.ErrInputUnknownRace))
	assert.ErrorContains(t,
		g.RaceRelation(UnknownRace, Race_0.Name, "Peace"),
		e.GenericErrorText(e.ErrInputUnknownRace))
	assert.ErrorContains(t,
		g.RaceRelation(Race_0.Name, Race_Extinct.Name, "War"),
		e.GenericErrorText(e.ErrRaceExinct))
	assert.ErrorContains(t,
		g.RaceRelation(Race_Extinct.Name, Race_0.Name, "War"),
		e.GenericErrorText(e.ErrRaceExinct))
}

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

	assert.ErrorContains(t,
		g.RaceQuit(UnknownRace),
		e.GenericErrorText(e.ErrInputUnknownRace))

	assert.ErrorContains(t,
		g.RaceQuit(Race_Extinct.Name),
		e.GenericErrorText(e.ErrRaceExinct))

	assert.NoError(t, g.RaceQuit(Race_0.Name))
	assert.Equal(t, 3, int(c.Race(Race_0_idx).TTL))
}

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

	c.Race(Race_0_idx).TTL = 9

	_, err := g.RaceID(UnknownRace)
	assert.ErrorContains(t, err, e.GenericErrorText(e.ErrInputUnknownRace))

	_, err = g.RaceID(Race_Extinct.Name)
	assert.ErrorContains(t, err, e.GenericErrorText(e.ErrRaceExinct))

	id, err := g.RaceID(Race_0.Name)
	assert.NoError(t, err)
	assert.Equal(t, Race_0_ID, id)
}
