import GamblingManager from './gamblingmanager'

const DialogManager = {
  scene: null,

  stepIndex: 0,
  stepType: 'normal',

  steps: [
    function () {
      GamblingManager.score.alpha = 0.0
      this.scene.loop.show()
      this.scene.dialog.setText('Executioner: Next!', true)
    },
    function () {
      this.scene.dialog.setText('Executioner: Put your head in the loop.', true)
    },
    function () {
      this.scene.dialog.setText()
      this.scene.loop.deathAnim(() => {
        this.scene.loop.hide()
        this.nextStep()
      })
    },
    function () {
      this.scene.dialog.setText('Executioner: Are you ready to die?', true)
    },
    function () {
      this.scene.dialog.setText('Executioner: ... No? ... That\'s an issue... Maybe we can work out an arrangement ?', true)
    },
    function () {
      GamblingManager.score.alpha = 1.0
      this.scene.dialog.setText('Executioner: Give me 100$ and I\'ll set you free.', true)
    },
    function () {
      this.scene.dialog.setText('Executioner: Ho, you\'re broke. Wanna play dice with me?', true)
    },
    function () {
      this.scene.dialog.setText('')
      this.scene.bet.show()
      this.scene.loop.hide()
      GamblingManager.startGame()
    }
  ],

  winSteps: [
    function () {
      this.scene.dialog.setText('Executioner: hahaha! You won I guess. thanks for the entertainment.', true)
    },
    function () {
      GamblingManager.score.alpha = 0.0
      this.scene.eye.startAnim(false)
      this.scene.dialog.setText('')

      this.scene.time.addEvent({
        callback: () => {
          this.scene.scene.restart()
          DialogManager.reset()
        },
        repeat: false,
        delay: 1000
      })
    }
  ],

  loseSteps: [
    function () {
      this.scene.dialog.setText('Looks like you have no money left.', true)
    },
    function () {
      GamblingManager.score.alpha = 0.0
      this.scene.dialog.setText('*Bonk*', false)
      this.scene.cameras.main.shake(400, 0.01)
      this.scene.time.addEvent({
        callback: () => {
          this.nextStep()
        },
        delay: 500
      })
    },
    function () {
      this.scene.eye.startAnim(false)
      this.scene.dialog.setText('')

      this.scene.time.addEvent({
        callback: () => {
          this.scene.scene.restart()
          DialogManager.reset()
        },
        repeat: false,
        delay: 2700
      })
    }
  ],

  init: function (scene) {
    this.scene = scene
    this.executeStep()
  },

  nextStep: function () {
    this.stepIndex += 1
    if (this.steps[this.stepIndex] === undefined) this.stepIndex = 0

    this.executeStep()
  },

  changeStepType: function (type) {
    this.stepType = type
    this.stepIndex = 0
  },

  executeStep: function () {
    if (this.stepType === 'normal') {
      this.steps[this.stepIndex].apply(this, [])
    } else if (this.stepType === 'win') {
      this.winSteps[this.stepIndex].apply(this, [])
    } else if (this.stepType === 'lose') {
      this.loseSteps[this.stepIndex].apply(this, [])
    }
  },

  reset: function () {
    this.stepIndex = 0
    this.stepType = 'normal'
    this.executeStep()
  }
}

export default DialogManager
