import Phaser from 'phaser'

class IntroEye extends Phaser.GameObjects.Group {
  constructor (scene) {
    super(scene)

    this.top = new IntroSprite(scene, false)
    this.bottom = new IntroSprite(scene, true)

    this.add(this.top, true)
    this.add(this.bottom, true)
  }

  show (isIntro) {
    this.setAlpha(1)

    if (isIntro) {
      this.top.reset()
      this.bottom.reset()
    }
  }

  startAnim (isIntro) {
    this.show(isIntro)

    this.top.startAnim(isIntro)
    this.bottom.startAnim(isIntro)
  }

  hide () {
    this.setAlpha(0)
  }
}

class IntroSprite extends Phaser.GameObjects.Sprite {
  constructor (scene, bottom) {
    super(
      scene, 0, 0,
      'eye',
      0
    )
    this.scene = scene
    this.bottom = bottom
    this.reset()
  }

  reset () {
    this.alpha = 1
    if (this.bottom) {
      this.setOrigin(0, 1)
      this.setFlip(false, true)
      this.y = this.scene.game.canvas.height
    } else {
      this.setOrigin(0, 0)
      this.y = 0
    }
  }

  startAnim (isIntro) {
    let target = 0

    if (isIntro) {
      if (this.bottom) target = this.scene.game.canvas.height + 500
      else target = -500
    } else {
      if (this.bottom) target = this.scene.game.canvas.height
      else target = 0
    }

    this.scene.tweens.add({
      targets: this,
      y: target,
      duration: 3000,
      ease: 'Power2'
    })
  }
}

export default IntroEye
