/*
 * Pixel Dungeon
 * Copyright (C) 2012-2015  Oleg Dolya
 *
 * Shattered Pixel Dungeon
 * Copyright (C) 2014-2019 Evan Debenham
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 */

package com.watabou.noosa.audio;

import com.badlogic.gdx.Gdx;
import com.watabou.noosa.Game;

public enum Music {
	
	INSTANCE;
	
	private com.badlogic.gdx.audio.Music player;
	
	private String lastPlayed;
	private boolean lastLooping;
	
	private boolean enabled = true;
	private float volume = 1f;
	
	public synchronized void play( String assetName, boolean looping ) {
		
		if (isPlaying() && lastPlayed.equals( assetName )) {
			return;
		}
		
		stop();
		
		lastPlayed = assetName;
		lastLooping = looping;
		
		if (!enabled || assetName == null) {
			return;
		}
		
		try {
			player = Gdx.audio.newMusic(Gdx.files.internal(assetName));
			player.setLooping(looping);
			player.setVolume(volume);
			player.play();
		} catch (Exception e){
			Game.reportException(e);
			player = null;
		}
	}
	
	public synchronized void mute() {
		lastPlayed = null;
		stop();
	}

	public synchronized void pause() {
		if (player != null) {
			player.pause();
		}
	}
	
	public synchronized void resume() {
		if (player != null) {
			player.play();
		}
	}
	
	public synchronized void stop() {
		if (player != null) {
			player.stop();
			player.dispose();
			player = null;
		}
	}
	
	public synchronized void volume( float value ) {
		volume = value;
		if (player != null) {
			player.setVolume( value );
		}
	}
	
	public synchronized boolean isPlaying() {
		return player != null && player.isPlaying();
	}
	
	public synchronized void enable( boolean value ) {
		enabled = value;
		if (isPlaying() && !value) {
			stop();
		} else
		if (!isPlaying() && value) {
			play( lastPlayed, lastLooping );
		}
	}
	
	public synchronized boolean isEnabled() {
		return enabled;
	}
}
