package zero1hd.polyjet.audio;

import java.io.IOException;

import org.jaudiotagger.audio.AudioFile;
import org.jaudiotagger.audio.AudioFileIO;
import org.jaudiotagger.audio.exceptions.CannotReadException;
import org.jaudiotagger.audio.exceptions.InvalidAudioFrameException;
import org.jaudiotagger.audio.exceptions.ReadOnlyFileException;
import org.jaudiotagger.audio.mp3.MP3File;
import org.jaudiotagger.audio.wav.WavTag;
import org.jaudiotagger.tag.FieldKey;
import org.jaudiotagger.tag.TagException;

import com.badlogic.gdx.Preferences;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.utils.Disposable;

public class AudioInfo implements Disposable {
	private long durationInSeconds;
	private String songName;
	private Texture albumCover;
	private String author;
	private int previousTop;
	private int ratedDifficulty;
	private byte[] albumWorkBytes;
	private boolean invalidMusic;
	
	public AudioInfo(FileHandle musicFile, Preferences musicData) {
		if (musicFile.extension().toLowerCase().equals("mp3")) {
			MP3File mp3File;
			try {
				mp3File = new MP3File(musicFile.file());
				durationInSeconds = mp3File.getAudioHeader().getTrackLength();
				
				if (mp3File.getTag() != null && mp3File.getTag().getFirstArtwork() != null) {
					albumWorkBytes = mp3File.getTag().getFirstArtwork().getBinaryData();
				}
				
				songName = mp3File.getTag().getFirst(FieldKey.TITLE);
				author = mp3File.getTag().getFirst(FieldKey.ARTIST);
			} catch (IOException | TagException | ReadOnlyFileException | InvalidAudioFrameException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
//			invalidMusic = true;
			
		} else {
			
			try {
				AudioFile audioFile = AudioFileIO.read(musicFile.file());
				WavTag wavTag = (WavTag) AudioFileIO.read(musicFile.file()).getTag();
				songName = wavTag.getFirst(FieldKey.TITLE);
				author = wavTag.getFirst(FieldKey.ARTIST);
				durationInSeconds = audioFile.getAudioHeader().getTrackLength();
			} catch (CannotReadException | IOException | TagException | ReadOnlyFileException
					| InvalidAudioFrameException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (durationInSeconds > 60 * 5) {
			invalidMusic = true;
		}
		
		if (songName == null || songName.isEmpty()) {
			songName = musicFile.nameWithoutExtension();
		}
		
		previousTop = musicData.getInteger(songName + ":previous top", -1);
		ratedDifficulty = musicData.getInteger(songName + ":difficulty", -1);
		
		if (author == null || author.isEmpty()) {
			author = "N/A";
		}
	}
	
	public void setupTexture(Texture defaultAlbumCover) {
		if (albumWorkBytes != null) {
			Pixmap albumCoverFromBytes = new Pixmap(albumWorkBytes, 0, albumWorkBytes.length);
			albumCover = new Texture(albumCoverFromBytes);
			albumCoverFromBytes.dispose();
		} else {
			this.albumCover = defaultAlbumCover;
		}
	}
	
	public long getDurationInSeconds() {
		return durationInSeconds;
	}
	
	public String getSongName() {
		return songName;
	}
	
	public Texture getAlbumCover() {
		return albumCover;
	}
	
	public String getAuthor() {
		return author;
	}
	
	public int getPreviousTop() {
		return previousTop;
	}
	
	public int getRatedDifficulty() {
		return ratedDifficulty;
	}
	
	public boolean isInvalidMusic() {
		return invalidMusic;
	}

	@Override
	public void dispose() {
		albumCover.dispose();
	}
	
}
