package game

import (
	"github.com/soer/football/internal/entities"
)

type GameState int

const (
	Playing GameState = iota
	GoalPause
	MatchEnded
)

type MatchManager struct {
	World         *World
	ScoreRed      int
	ScoreBlue     int
	State         GameState
	MatchDuration float64
	CurrentTime   float64
	PauseDuration float64
	PauseTimer    float64
}

func NewMatchManager(world *World, matchDuration, pauseDuration float64) *MatchManager {
	return &MatchManager{
		World:         world,
		State:         Playing,
		MatchDuration: matchDuration,
		PauseDuration: pauseDuration,
	}
}

func (m *MatchManager) Update() {
	switch m.State {
	case Playing:
		team, scored := m.World.CheckGoal()
		if scored {
			if team == entities.TeamRed {
				m.ScoreRed++
			} else {
				m.ScoreBlue++
			}
			m.State = GoalPause
			m.PauseTimer = m.PauseDuration
		} else {
			m.World.Update()
			m.CurrentTime += 1.0
		}

		if m.CurrentTime >= m.MatchDuration {
			m.State = MatchEnded
		}

	case GoalPause:
		m.PauseTimer -= 1.0
		if m.PauseTimer <= 0 {
			m.World.ResetPositions()
			m.State = Playing
		}

	case MatchEnded:
		// Do nothing
	}
}
