package ecs

// Observer for ECS events.
//
// Observers react to structural changes, such as entity creation, removal, and component addition/removal.
// Use the methods Observe, With, Without, and Do to configure the observer before registering it.
//
// See [EventType] for available events.
// See also [Observer1], [Observer2], etc.
type Observer struct {
	observerData
	comps     []Comp
	with      []Comp
	without   []Comp
	event     EventType
	exclusive bool
}

// observerData contains the observer data that is required by the observerManager.
type observerData struct {
	compsMask   bitMask
	withMask    bitMask
	withoutMask bitMask
	callback    func(Entity)
	id          observerID
	hasComps    bool
	hasWithout  bool
	hasWith     bool
}

// Observe creates a new ECS event observer for the specified event type.
//
// Observers react to structural changes, such as entity creation, removal, and component addition/removal.
// Use the methods For, With, Without, Exclusive, and Do to configure the observer before registering it.
//
// See also [Observe1], [Observe2], etc.
func ( EventType) *Observer {
	return &Observer{
		event: ,
		observerData: observerData{
			id: maxObserverID,
		},
	}
}

// New creates a new [Observer]. It is safe to call on `nil` instance.
// It is a helper method, provided for consistency with generic observers like [Observer1].
func (*Observer) ( EventType) *Observer {
	return Observe()
}

// For adds components that the observer observes.
// The component events, the observer triggers if these components are added to or removed from an entity.
//
// If not specified, the observer triggers on any component addition or removal.
// If multiple components are provided, all must be added/removed at the same time to trigger the observer.
//
// For entity events (OnCreateEntity, OnRemoveEntity), is has the same effect as With.
//
// Method calls can be chained, which has the same effect as calling with multiple arguments.
func ( *Observer) ( ...Comp) *Observer {
	if .id != maxObserverID {
		panic("can't modify a registered observer")
	}
	if len() == 0 {
		return 
	}
	.comps = append(.comps, ...)
	return 
}

// With adds components that entities must have to trigger the observer.
// If multiple components are provided, the entity must have all of them.
//
// Method calls can be chained, which has the same effect as calling with multiple arguments.
func ( *Observer) ( ...Comp) *Observer {
	if .id != maxObserverID {
		panic("can't modify a registered observer")
	}
	if len() == 0 {
		return 
	}
	.with = append(.with, ...)
	return 
}

// Without adds components that entities must not have to trigger the observer.
// If multiple components are provided, the entity must not have any of them.
//
// Method calls can be chained, which has the same effect as calling with multiple arguments.
func ( *Observer) ( ...Comp) *Observer {
	if .id != maxObserverID {
		panic("can't modify a registered observer")
	}
	if len() == 0 {
		return 
	}
	.without = append(.without, ...)
	return 
}

// Exclusive makes the observer exclusive in the sense that the components given by [Observer.With]
// are matched exactly, and no other components are allowed.
//
// Overwrites components set via [Observer.Without].
func ( *Observer) () *Observer {
	if .id != maxObserverID {
		panic("can't modify a registered observer")
	}
	.exclusive = true
	return 
}

// Do sets the observer's callback. Must be called exactly once before registration.
func ( *Observer) ( func(Entity)) *Observer {
	if .callback != nil {
		panic("observer already has a callback")
	}
	.callback = 
	return 
}

// Register this observer. This is mandatory for the observer to take effect.
func ( *Observer) ( *World) *Observer {
	.registerObserver()
	return 
}

// Unregister this observer.
func ( *Observer) ( *World) *Observer {
	.unregisterObserver()
	return 
}

func ( *observerData) ( *bitMask) bool {
	if .hasWith && !.Contains(&.withMask) {
		return false
	}
	if .hasWithout && .ContainsAny(&.withoutMask) {
		return false
	}
	return true
}

func ( *observerData) (,  *bitMask) bool {
	if .hasComps && !.Contains(&.compsMask) {
		return false
	}
	if .hasWith && !.Contains(&.withMask) {
		return false
	}
	if .hasWithout && .ContainsAny(&.withoutMask) {
		return false
	}
	return true
}