package gui

import "github.com/hajimehoshi/ebiten/v2"

type Events interface {
	OnMouseClick(event MouseEvent) (consumed bool)
	OnMouseMove(event MouseEvent) (consumed bool)
	OnMouseScroll(event MouseEvent) (consumed bool)
}

type EventTmpl struct {
	MouseClick  func(event MouseEvent) (consumed bool)
	MouseMove   func(event MouseEvent) (consumed bool)
	MouseScroll func(event MouseEvent) (consumed bool)
}

var _ Events = &EventTmpl{}

func (e *EventTmpl) OnMouseClick(event MouseEvent) (consumed bool) {
	if e.MouseClick == nil {
		return false
	}
	return e.MouseClick(event)
}

func (e *EventTmpl) OnMouseMove(event MouseEvent) (consumed bool) {
	if e.MouseMove == nil {
		return false
	}
	return e.MouseMove(event)
}

func (e *EventTmpl) OnMouseScroll(event MouseEvent) (consumed bool) {
	if e.MouseScroll == nil {
		return false
	}
	return e.MouseScroll(event)
}

type MouseEvent struct {
	Position Point
	Buttons  ButtonMask
	Wheel    Point
}

func makeMouseEvent() MouseEvent {
	return MouseEvent{
		Position: P(ebiten.CursorPosition()),
		Buttons:  Buttons(),
		Wheel:    Pf(ebiten.Wheel()),
	}
}

func (e MouseEvent) SubtractPos(p Point) MouseEvent {
	e.Position = e.Position.Sub(p)
	return e
}

func (e MouseEvent) AddPos(p Point) MouseEvent {
	e.Position = e.Position.Add(p)
	return e
}

func (e MouseEvent) IsScrollEvent() bool {
	return e.Wheel.X != 0 || e.Wheel.Y != 0
}

func (e MouseEvent) HasMouseMoved(o MouseEvent) bool {
	return e.Position != o.Position
}

func (e MouseEvent) Pressed(button MouseButton) bool {
	return e.Buttons.Pressed(button)
}

func (e MouseEvent) JustPressed(button MouseButton) bool {
	return e.Buttons.JustPressed(button)
}

func (e MouseEvent) Clicked(button MouseButton) bool {
	return e.Buttons.Clicked(button)
}
