// Copyright 2014 Globo.com Player authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

var Playback = require('playback')
var Events = require('events')

class HTML5Audio extends Playback {
  get name() { return 'html5_audio' }
  get tagName() { return 'audio' }
  get events() {
    return {
      'timeupdate': 'timeUpdated',
      'ended': 'ended',
      'canplaythrough': 'bufferFull'
    }
  }

  constructor(params) {
    super(params)
    this.el.src = params.src
    this.settings = {
      left: ['playpause', 'position', 'duration'],
      right: ['fullscreen', 'volume'],
      default: ['seekbar']
    }
    this.render()
    params.autoPlay && this.play()
  }

  bindEvents() {
    this.listenTo(this.container, Events.CONTAINER_PLAY, this.play)
    this.listenTo(this.container, Events.CONTAINER_PAUSE, this.pause)
    this.listenTo(this.container, Events.CONTAINER_SEEK, this.seek)
    this.listenTo(this.container, Events.CONTAINER_VOLUME, this.volume)
    this.listenTo(this.container, Events.CONTAINER_STOP, this.stop)
  }

  getPlaybackType() {
    return "aod"
  }

  play() {
    this.el.play()
    this.trigger(Events.PLAYBACK_PLAY);
  }

  pause() {
    this.el.pause()
  }

  stop() {
    this.pause()
    this.el.currentTime = 0
  }

  volume(value) {
    this.el.volume = value / 100
  }

  mute() {
    this.el.volume = 0
  }

  unmute() {
    this.el.volume = 1
  }

  isMuted() {
    return !!this.el.volume
  }

  ended() {
    this.trigger(Events.CONTAINER_TIMEUPDATE, 0)
  }

  seek(seekBarValue) {
    var time = this.el.duration * (seekBarValue / 100)
    this.el.currentTime = time
  }

  getCurrentTime() {
    return this.el.currentTime
  }

  getDuration() {
    return this.el.duration
  }

  isPlaying() {
    return !this.el.paused && !this.el.ended
  }

  timeUpdated() {
    this.trigger(Events.PLAYBACK_TIMEUPDATE, this.el.currentTime, this.el.duration, this.name)
  }

  bufferFull() {
    this.trigger(Events.PLAYBACK_TIMEUPDATE, this.el.currentTime, this.el.duration, this.name)
    this.trigger(Events.PLAYBACK_BUFFERFULL)
  }

  render() {
    this.trigger(Events.PLAYBACK_READY, this.name)
    return this
  }
 }

HTML5Audio.canPlay = function(resource) {
  return !!resource.match(/(.*).mp3/)
}


module.exports = HTML5Audio

@@ -228,16 +228,46 @@
 events')
+%0Avar _ = require('underscore')
 %0A%0Aclass 
@@ -2318,43 +2318,424 @@
 %7B%0A  
-return !!resource.match(/(.*).mp3/)
+var mimetypes = %7B%0A    'wav': %5B'audio/wav'%5D,%0A    'mp3': %5B'audio/mp3', 'audio/mpeg;codecs=%22mp3%22'%5D,%0A    'aac': %5B'audio/mp4;codecs=%22mp4a.40.5%22'%5D%0A  %7D%0A  var extension = resource.split('?')%5B0%5D.match(/.*%5C.(.*)$/)%5B1%5D%0A%0A  if (_.has(mimetypes, extension)) %7B%0A    var a = document.createElement('audio')%0A    return !!_.find(mimetypes%5Bextension%5D, function (ext) %7B return !!a.canPlayType(ext).replace(/no/, '') %7D)%0A  %7D%0A  return false
 %0A%7D%0A%0A

use canPlayType on canPlay method (close #339)