package internal

import "golang.source-fellows.com/pattern/observer/internal/model"

type CommoditiesNewsObserver interface {
	Update()
}

type NewsSubject interface {
	Attach(observer CommoditiesNewsObserver)
	Detach(observer CommoditiesNewsObserver)
	Notify()
}

type CommoditiesNewsManager struct {
	observers          []CommoditiesNewsObserver
	allCommoditiesNews []*model.CommoditiesNews
}

func (m *CommoditiesNewsManager) Attach(
	observer CommoditiesNewsObserver) {
	m.observers = append(m.observers, observer)
}

func (m *CommoditiesNewsManager) Detach(observer CommoditiesNewsObserver) {

}

func (m *CommoditiesNewsManager) Notify() {
	for _, concreteObserver := range m.observers {
		concreteObserver.Update()
	}
}

func (m *CommoditiesNewsManager) CreateCommoditiesNews(commoditiesNews *model.CommoditiesNews) {
	m.allCommoditiesNews = append(m.allCommoditiesNews, commoditiesNews)
	m.Notify()
}

func (m *CommoditiesNewsManager) GetCommoditiesNews() []*model.CommoditiesNews {
	return m.allCommoditiesNews
}
