package ocelot.desktop.ui.widget.traits

import ocelot.desktop.ColorScheme
import ocelot.desktop.color.Color
import ocelot.desktop.ui.Const._
import ocelot.desktop.ui.event.{Capturing, EventAware, HoverEvent}
import ocelot.desktop.ui.event.handlers.HoverHandler
import ocelot.desktop.ui.widget.{Updatable, Widget}
import ocelot.desktop.util.animation.ColorAnimation

/**
  * Helper trait that manages color animations for UI widgets that should be highlighted on mouse hover.
  */
trait HoverAnimation extends Widget with EventAware with HoverHandler with Updatable {
  //noinspection ScalaWeakerAccess
  protected val HoverAnimationSpeedEnter: Float = AnimationSpeedHoverEnter
  //noinspection ScalaWeakerAccess
  protected val HoverAnimationSpeedLeave: Float = AnimationSpeedHoverLeave
  protected val HoverAnimationColorDefault: Color = ColorScheme("ButtonBackground")
  protected val HoverAnimationColorActive: Color = ColorScheme("ButtonBackgroundActive")

  protected lazy val hoverAnimation: ColorAnimation =
    new ColorAnimation(HoverAnimationColorDefault, HoverAnimationSpeedEnter)

  eventHandlers += {
    case Capturing(HoverEvent(HoverEvent.State.Enter)) =>
      hoverAnimation.speed = HoverAnimationSpeedEnter
      hoverAnimation.goto(HoverAnimationColorActive)

    case Capturing(HoverEvent(HoverEvent.State.Leave)) =>
      hoverAnimation.speed = HoverAnimationSpeedLeave
      hoverAnimation.goto(HoverAnimationColorDefault)
  }

  override def update(): Unit = {
    super.update()
    hoverAnimation.update()
  }
}
