package game

import (
	"fmt"
	"image"
	. "openquell/components"

	"github.com/mlange-42/arche/ecs"
	"github.com/mlange-42/arche/generic"
)

type CollectibleSystem struct {
	World    *ecs.World
	Selector *generic.Filter2[Position, Collectible]
}

func NewCollectibleSystem(world *ecs.World) *CollectibleSystem {
	system := &CollectibleSystem{
		Selector: generic.NewFilter2[Position, Collectible](),
	}

	return system
}

func (system *CollectibleSystem) CheckPlayerCollision(
	playerposition *Position,
	playervelocity *Velocity) (bool, image.Point) {

	toRemove := []ecs.Entity{}
	particle_pos := image.Point{}
	var bumped bool

	query := system.Selector.Query(system.World)

	for query.Next() {
		colposition, collectible := query.Get()

		ok, _ := playerposition.Intersects(colposition, playervelocity)
		if ok {
			fmt.Printf("bumped into collectible %v\n", collectible)
			toRemove = append(toRemove, query.Entity())
			particle_pos.X = colposition.X
			particle_pos.Y = colposition.Y
			bumped = true
		}
	}

	// remove collectible if collected
	for _, entity := range toRemove {
		system.World.RemoveEntity(entity)
	}

	return bumped, particle_pos
}
