package tins2020

import (
	"opslag.de/schobers/zntg/ui"
)

type Dialogs struct {
	ui.OverlayProxy

	intro    ui.Overlay
	research ui.Overlay
	settings ui.Overlay
	nothing  ui.Control

	closed ui.Events
	opened ui.Events
}

const dialogsOverlayName = "dialogs"

func NewDialogs(game *Game) *Dialogs {
	intro := NewIntro()
	research := NewResearch(game)
	settings := NewLargeDialog("settings", &ui.Label{})

	dialogs := &Dialogs{
		intro:    intro,
		settings: settings,
		research: research,
		nothing:  &ui.ControlBase{},
	}

	intro.CloseRequested().AddHandlerEmpty(dialogs.Close)
	research.CloseRequested().AddHandlerEmpty(dialogs.Close)
	settings.CloseRequested().AddHandlerEmpty(dialogs.Close)

	return dialogs
}

func (d *Dialogs) Init(ctx ui.Context) {
	overlays := ctx.Overlays()
	overlays.AddOnTop(dialogsOverlayName, d, false)

	d.Content = d.nothing
}

func (d *Dialogs) showDialog(ctx ui.Context, control ui.Control) {
	if control == nil {
		d.Content = d.nothing
		ctx.Overlays().Hide(dialogsOverlayName)
		d.closed.Notify(ctx, control)
	} else {
		d.Content = control
		ctx.Overlays().Show(dialogsOverlayName)
		d.opened.Notify(ctx, control)
	}
}

func (d *Dialogs) Close(ctx ui.Context) {
	d.showDialog(ctx, nil)
}

func (d *Dialogs) DialogClosed() ui.EventHandler { return &d.closed }
func (d *Dialogs) DialogOpened() ui.EventHandler { return &d.opened }

func (d *Dialogs) ShowIntro(ctx ui.Context) {
	d.showDialog(ctx, d.intro)
}

func (d *Dialogs) ShowResearch(ctx ui.Context) {
	d.showDialog(ctx, d.research)
}

func (d *Dialogs) ShowSettings(ctx ui.Context) {
	d.showDialog(ctx, d.settings)
}