package internal

import "log"

type StateListener interface {
	StateChanged(service, state string)
}
type StateInformer interface {
	InformAboutState(service, state string)
}

type StateService struct {
	listener map[string][]StateListener
}

func NewStateService() *StateService {
	return &StateService{listener: make(map[string][]StateListener)}
}

func (s *StateService) InformAboutState(service, state string) {
	log.Println("InformAboutState")
	listeners, ok := s.listener[service]
	if !ok {
		return
	}
	for _, listener := range listeners {
		listener.StateChanged(service, state)
	}
}

func (s *StateService) RegisterListener(services []string, listener StateListener) {

	for _, service := range services {
		listeners, ok := s.listener[service]
		if !ok {
			listeners = []StateListener{}
		}
		s.listener[service] = append(listeners, listener)
	}

}
