package controller_test

import (
	"testing"

	"galaxy/model/report"

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

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

	c.TurnCalculateVotes()

	rep := c.InitReport(2)
	assert.Equal(t, 2, int(rep.Turn))

	c.ReportRace(Race_0_idx, rep, nil, nil)

	assert.Equal(t, Race_0.Name, rep.Race)
	assert.Equal(t, Race_0.ID, rep.RaceID)
	assert.Equal(t, 0.1, float64(rep.Votes))

	for i := range rep.Player {
		p := &rep.Player[i]
		switch p.ID {
		case Race_0_ID:
			assert.Equal(t, Race_0.Name, p.Name)
			assert.Equal(t, 1.1, float64(p.Drive))
			assert.Equal(t, 1.2, float64(p.Weapons))
			assert.Equal(t, 1.3, float64(p.Shields))
			assert.Equal(t, 1.4, float64(p.Cargo))
			assert.Equal(t, 100., float64(p.Population))
			assert.Equal(t, 100., float64(p.Industry))
			assert.Equal(t, 2, int(p.Planets))
			assert.Equal(t, 0.1, float64(p.Votes))
			assert.Equal(t, "-", p.Relation)
		case Race_1_ID:
			assert.Equal(t, Race_1.Name, p.Name)
			assert.Equal(t, 2.1, float64(p.Drive))
			assert.Equal(t, 2.2, float64(p.Weapons))
			assert.Equal(t, 2.3, float64(p.Shields))
			assert.Equal(t, 2.4, float64(p.Cargo))
			assert.Equal(t, 0., float64(p.Population))
			assert.Equal(t, 0., float64(p.Industry))
			assert.Equal(t, 1, int(p.Planets))
			assert.Equal(t, 0., float64(p.Votes))
			assert.Equal(t, "WAR", p.Relation)
		}
	}
}

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

	r := &report.Report{}
	assert.Len(t, r.LocalShipClass, 0)

	c.ReportLocalShipClass(Race_0_idx, r)

	assert.Len(t, r.LocalShipClass, 3)
	for i := range r.LocalShipClass {
		assert.NotEmpty(t, r.LocalShipClass[i].Name)
		switch n := r.LocalShipClass[i].Name; n {
		case Cruiser.Name:
			assert.Equal(t, report.F(Cruiser.Drive.F()), r.LocalShipClass[i].Drive)
			assert.Equal(t, Cruiser.Armament, r.LocalShipClass[i].Armament)
			assert.Equal(t, report.F(Cruiser.Weapons.F()), r.LocalShipClass[i].Weapons)
			assert.Equal(t, report.F(Cruiser.Shields.F()), r.LocalShipClass[i].Shields)
			assert.Equal(t, report.F(Cruiser.Cargo.F()), r.LocalShipClass[i].Cargo)
		case Race_0_Gunship:
			assert.Equal(t, report.F(60.), r.LocalShipClass[i].Drive)
			assert.Equal(t, uint(3), r.LocalShipClass[i].Armament)
			assert.Equal(t, report.F(30.), r.LocalShipClass[i].Weapons)
			assert.Equal(t, report.F(100.), r.LocalShipClass[i].Shields)
			assert.Equal(t, report.F(0.), r.LocalShipClass[i].Cargo)
		case Race_0_Freighter:
			assert.Equal(t, report.F(8.), r.LocalShipClass[i].Drive)
			assert.Equal(t, uint(0), r.LocalShipClass[i].Armament)
			assert.Equal(t, report.F(0.), r.LocalShipClass[i].Weapons)
			assert.Equal(t, report.F(2.), r.LocalShipClass[i].Shields)
			assert.Equal(t, report.F(10.), r.LocalShipClass[i].Cargo)
		default:
			assert.Failf(t, "unexpected ship class", "name=%s", n)
		}
	}
}
