import Ember from 'ember';
import layout from '../templates/components/music-player';

export default Ember.Component.extend({
  layout: layout,
  isPlaying: false,
  defaultVolume: 15,
  volume: 0,

  playing: {
    title: '',
    artist: '',
    cover: ''
  },

  current_song: null,

  handleMeta( song ) {
    this.set('playing.title', song.title);
    this.set('playing.artist', song.artist);
    this.set('playing.cover', song.cover);
  },

  stopCurrentSong() {
    if ( this.get('isPlaying') ) {
      this.get('current_song').pause();
      this.get('current_song').currentTime = 0;
    }
  },

  actions: {
    playAudio( audio ) {
      let song = audio;

      this.stopCurrentSong();

      if ( !audio ) {
        song = this.get('playlist')[0];
      }

      this.set('current_song', new Audio('data/' + song.file.mp3));
      this.get('current_song').play();

      this.handleMeta( song );

      this.set('isPlaying', true);
      this.set('status', 'playing');

      this.get('current_song').addEventListener('timeupdate', () => {
        let time = parseInt(this.get('current_song').currentTime, 10);
        this.set('current_time', time);
      });
    },

    pauseAudio() {
      if ( this.get('isPlaying') ) {
        this.get('current_song').pause();
      }
    },

    stopAudio() {
      this.stopCurrentSong();

      this.set('isPlaying', false);
    },

    setVolume() {
      this.set('volume', this.$().find('input[type="range"]').val() / 100)
      this.get('current_song').volume = this.get('volume');
    }
  }
});

@@ -830,21 +830,43 @@
 .mp3));%0A
+%0A
      
+ const current_song =
  this.ge
@@ -882,16 +882,37 @@
 t_song')
+;%0A%0A      current_song
 .play();
@@ -1020,34 +1020,24 @@
 g');%0A%0A      
-this.get('
 current_song
@@ -1036,18 +1036,16 @@
 ent_song
-')
 .addEven
@@ -1104,26 +1104,16 @@
 arseInt(
-this.get('
 current_
@@ -1108,34 +1108,32 @@
 Int(current_song
-')
 .currentTime, 10
@@ -1222,57 +1222,143 @@
     
-if ( this.get('isPlaying') ) %7B%0A        this.get('
+let current_song = this.get('current_song');%0A%0A      if ( !current_song.paused ) %7B%0A        current_song.pause();%0A      %7D else %7B%0A        
 curr
@@ -1361,32 +1361,29 @@
 current_song
-').pause
+.play
 ();%0A      %7D%0A
@@ -1575,16 +1575,17 @@
 ) / 100)
+;
 %0A      t

add pause and resume functionality