import { h, Component } from 'preact'

const audio = document.createElement('audio')

export default function AudioPlayer (props) {
  if (props.file) {
    document.body.style.backgroundImage = 'url(' + pngpath(file) + ')'
    audio.src = mp3path(props.file)
    audio.play()
    return (
      <div>
        Playing {props.file.name}
      </div>
    )
  }
  else {
    return (
      <div>
        Not Playing
      </div>
    )
  }
}

function filepath (file) {
  return '/data/' + file.folder_id + '/' + encodeURIComponent(file.name)
}
function mp3path (file) {
  if (file.mime !== 'audio/mp3') {
    return filepath(file) + '.mp3'
  }
  return filepath(file)
}
function pngpath (file) {
  return filepath(file) + '.png'
}
