package zero1hd.polyjet.ui.builders;

import com.badlogic.gdx.Preferences;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.scenes.scene2d.ui.Button;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.utils.Align;
import com.badlogic.gdx.utils.Disposable;

import zero1hd.polyjet.audio.AudioInfo;

public class MusicSelectable extends Button implements Disposable {
	
	private Image imageIcon;
	private ScrollText displayName;
	private Label runTime;
	private Label authorLabel;
	private Label previousTopLabel;
	private Label ratedDifficultyLabel;
	private Skin skin;
	
	private FileHandle musicFile;
	
	private Texture defaultAlbumCover;
	AudioInfo audioInfo;
	
	public MusicSelectable(FileHandle musicFile, Preferences musicData, final Skin skin, Texture defaultAlbumC) {
		super(skin, "info-button");
		this.skin = skin;
		this.defaultAlbumCover = defaultAlbumC;
		this.musicFile = musicFile;
		
		setName(musicFile.name());
		
		audioInfo = new AudioInfo(musicFile, musicData);
		
		defaults().pad(10f);
	}
	
	
	public void addInfoToPanel(float coverSize) {
		displayName = new ScrollText(audioInfo.getSongName(), skin, true);
		
		defaults().align(Align.top);
		
		add(displayName).expandX().pad(0).fillX().padTop(10f).top().padBottom(10f);
		row();

		String formattedTime = "Run time: "+ String.valueOf(audioInfo.getDurationInSeconds()/60) + ":";
		if (audioInfo.getDurationInSeconds() - (audioInfo.getDurationInSeconds()/60)*60 < 10) {
			formattedTime = formattedTime.concat("0");
		}
		
		Table songInfoTable = new Table();
		
		formattedTime = formattedTime.concat(String.valueOf(audioInfo.getDurationInSeconds() - (audioInfo.getDurationInSeconds()/60)*60));
		runTime = new Label(formattedTime, skin, "sub-font", skin.getColor("default"));
		songInfoTable.add(runTime).expandX().left();
		songInfoTable.row();
		
		authorLabel = new Label("Author: " + audioInfo.getAuthor(), skin, "sub-font", skin.getColor("default"));
		songInfoTable.add(authorLabel).left();
		songInfoTable.row();
		
		previousTopLabel = new Label("High Score: " + (audioInfo.getPreviousTop() != -1 ? audioInfo.getPreviousTop() : "N/A"), skin, "sub-font", skin.getColor("default"));
		songInfoTable.add(previousTopLabel).left();
		songInfoTable.row();
		
		ratedDifficultyLabel = new Label("Difficulty: " + (audioInfo.getRatedDifficulty() != -1 ? audioInfo.getRatedDifficulty() : "N/A"), skin, "sub-font", skin.getColor("default"));
		songInfoTable.add(ratedDifficultyLabel).left();
		songInfoTable.row();
		
		row();
		add(songInfoTable).expandY().center();
		row();
		
		audioInfo.setupTexture(defaultAlbumCover);
		
		imageIcon = new Image(audioInfo.getAlbumCover());
		if (audioInfo.isInvalidMusic()) {
			imageIcon.setColor(Color.RED);
		}
		add(imageIcon).expandY().center().pad(15f).size(coverSize);
	}
	
	public FileHandle getMusicFile() {
		return musicFile;
	}
	
	public AudioInfo getAudioInfo() {
		return audioInfo;
	}
	
	@Override
	public void dispose() {
		audioInfo.dispose();
	}
	
	public boolean isMusicInvalid() {
		return audioInfo.isInvalidMusic();
	}
	
}
