package ui

type Context interface {
	Animate()
	HasQuit() bool
	Images() *Images
	Quit()
	Renderer() Renderer
	Style() *Style
}

var _ Context = &context{}
var _ EventTarget = &context{}

type context struct {
	animate bool
	quit    chan struct{}
	r       Renderer
	view    Control
	ims     *Images
	style   *Style
}

func (c *context) Animate() { c.animate = true }

func (c *context) HasQuit() bool {
	select {
	case <-c.quit:
		return true
	default:
		return false
	}
}

func (c *context) Images() *Images { return c.ims }

func (c *context) Quit() {
	if !c.HasQuit() {
		close(c.quit)
	}
}

func (c *context) Renderer() Renderer { return c.r }

func (c *context) Style() *Style { return c.style }

// Handle implement EventTarget
func (c *context) Handle(e Event) {
	switch e.(type) {
	case *DisplayCloseEvent:
		c.Quit()
		return
	}
	c.view.Handle(c, e)
}