package game

import (
	"context"
	"sync"
)

type GameContext struct {
	ctx        context.Context
	cancel     context.CancelFunc
	wg         sync.WaitGroup
	assetsLock sync.RWMutex
}

func NewGameContext() *GameContext {
	ctx, cancel := context.WithCancel(context.Background())
	return &GameContext{
		ctx:    ctx,
		cancel: cancel,
	}
}

func (gc *GameContext) Shutdown() {
	gc.cancel()
	gc.wg.Wait()
}

func (gc *GameContext) LoadAssets(fn func() error) error {
	gc.assetsLock.Lock()
	defer gc.assetsLock.Unlock()
	return fn()
}

func (gc *GameContext) UnloadAssets(fn func()) {
	gc.assetsLock.Lock()
	defer gc.assetsLock.Unlock()
	fn()
}
