package ui

import "opslag.de/schobers/zntg"

type EventEmptyFn func(Context)

type EventFn func(Context, interface{})

type EventArgs struct {
	Context Context
	State   interface{}
}

type Events struct {
	zntg.Events
}

type EventHandler interface {
	AddHandler(EventFn) uint
	AddHandlerEmpty(EventEmptyFn) uint
	RemoveHandler(uint)
}

func (e *Events) Notify(ctx Context, state interface{}) bool {
	return e.Events.Notify(EventArgs{Context: ctx, State: state})
}

func (e *Events) AddHandler(handler EventFn) uint {
	return e.Events.AddHandler(func(state interface{}) {
		args := state.(EventArgs)
		handler(args.Context, args.State)
	})
}

func (e *Events) AddHandlerEmpty(handler EventEmptyFn) uint {
	return e.AddHandler(func(ctx Context, _ interface{}) { handler(ctx) })
}