import type { Point } from 'pixi.js'
import { Button } from './Button'
import type { Dimension } from '@/common/interfaces'
import { Timer } from './utilities/Timer'

export class TimedButton extends Button {
  private timer: Timer
  private countdown: number = 0

  constructor(
    protected text: string,
    protected point: Point,
    dimension: Dimension,
    protected action: Function,
    private seconds: number,
  ) {
    super(text, point, dimension, action)
    this.timer = new Timer(seconds)
    this.timer.on('timeout', this.onTImeOut.bind(this))
    this.timer.on('tick', this.onTick.bind(this))
    this.countdown = seconds
    this.render()
  }

  private onTImeOut() {
    this.disabled = true
    this.timer.reset()
    this.emit('timeout')
  }

  private onTick(seconds: number) {
    this.countdown = seconds
    this.render()
  }

  start() {
    this.timer.reset()
    this.timer.start()
  }

  protected getTextString() {
    return `${this.text} (${this.countdown})`
  }

  protected executeAction() {
    this.action()
    this.timer.reset()
  }
}
