import Phaser from './phaser'
import { loadMouseCursor, createMouseCursor, setMouseCursorState } from './mouse'

const observe = {
  'Pizza': 'I get so hungry in the morning ...',
  'Hair': 'Wow, I have a great head of hair.',
  'Key': 'The key to my freedom, and more ...',
  'Window': "I ... can't look..."
}

const observeSounds = {}

export default class GamePlay {
  constructor (game) {
    this.game = game
    this.mode = 'INTERACT'
  }

  preload () {
    this.game.load.image('room', 'assets/background.png')
    this.game.load.json('map', 'assets/map.json')
    this.game.load.audio('hungry', 'assets/hungry.wav')
    this.game.load.audio('hair', 'assets/hair.wav')
    loadMouseCursor.call(this)
  }

  create () {
    const roomImage = this.game.add.image(0, 0, 'room')
    roomImage.scale.setTo(0.75, 0.75)

    observeSounds['Pizza'] = this.game.add.audio('hungry')
    observeSounds['Hair'] = this.game.add.audio('hair')

    const map = this.game.cache.getJSON('map')
    const hotspots = map.layers.find(layer => layer.name === 'Hotspots').objects
    const hotspotSprites = hotspots.map(spot => {
      const width = spot.width * 0.75
      const height = spot.height * 0.75
      const bmd = this.game.add.bitmapData(width, height)

      bmd.ctx.beginPath()
      if (spot.ellipse) {
        bmd.ctx.ellipse(width / 2, height / 2, width / 2, height / 2, 0, 0, 2 * Math.PI)
      } else {
        bmd.ctx.rect(0, 0, width, height)
      }
      bmd.ctx.fillStyle = '#ff0000'
      bmd.ctx.fill()

      const sprite = this.game.add.sprite(spot.x * 0.75, spot.y * 0.75, bmd)
      sprite.boundsPadding = 0
      // sprite.width = spot.width * 0.75
      // sprite.height = spot.height * 0.75
      sprite.inputEnabled = true
      sprite.events.onInputOver.add(() => {
        this.hoverText.text = spot.name
        setMouseCursorState('highlight')
      })
      sprite.events.onInputOut.add(() => {
        this.hoverText.text = ''
        setMouseCursorState('neutral')
      })
      sprite.events.onInputDown.add(() => {
        this.say(observe[spot.name])
        if (observeSounds[spot.name]) observeSounds[spot.name].play()
      })
    })

    this.hoverText = this.game.add.text(this.game.width / 2, this.game.height - 100, '', { fill: '#ff0000' })
    this.speechText = this.game.add.text(this.game.width / 2, this.game.height - 100, '', { fill: '#ff0000' })
    this.speechText.visible = false

    createMouseCursor.call(this)
  }

  update () {

  }

  switchMode (newMode) {
    if (newMode === 'TALKING') {
      this.hoverText.visible = false
      this.speechText.visible = true
    } else if (newMode === 'INTERACT') {
      this.hoverText.visible = true
      this.speechText.visible = false
    }
  }

  say (text) {
    const oldMode = this.mode
    this.switchMode('TALKING')
    this.speechText.text = text

    this.game.time.events.add(Phaser.Timer.SECOND * 3, () => this.switchMode(oldMode), this)
  }
}

@@ -135,16 +135,43 @@
 Pizza': 
+%7B%0A    count: 0,%0A    text: %5B
 'I get s
@@ -194,24 +194,29 @@
 morning ...'
+%5D%0A  %7D
 ,%0A  'Hair': 
@@ -215,16 +215,43 @@
 'Hair': 
+%7B%0A    count: 0,%0A    text: %5B
 'Wow, I 
@@ -277,16 +277,21 @@
 f hair.'
+%5D%0A  %7D
 ,%0A  'Key
@@ -293,16 +293,43 @@
  'Key': 
+%7B%0A    count: 0,%0A    text: %5B
 'The key
@@ -357,16 +357,21 @@
 ore ...'
+%5D%0A  %7D
 ,%0A  'Win
@@ -380,29 +380,357 @@
 w': 
-%22I ... can't look...%22
+%7B%0A    count: 0,%0A    text: %5B'On either side the river lie..', %22No.. I ... can't look...%22%5D%0A  %7D%0A%7D%0A%0Afunction getObserveText(key) %7B%0A  const index = observe%5Bkey%5D.count%0A  const text = observe%5Bkey%5D.text%5Bindex%5D%0A  // If there are multiple observations, move to the next%0A  if (index %3C observe%5Bkey%5D.text.length - 1) %7B%0A    ++observe%5Bkey%5D.count%0A  %7D%0A%0A  return text
 %0A%7D%0A%0A
@@ -2480,24 +2480,31 @@
 his.say(
-o
+getO
 bserve
-%5B
+Text(
 spot.nam
@@ -2504,17 +2504,17 @@
 pot.name
-%5D
+)
 )%0A      

Add support for multiple observations