package zero1hd.rhythmbullet.ui.windows;

import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.audio.Music.OnCompletionListener;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.ImageButton;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.TextField;
import com.badlogic.gdx.scenes.scene2d.ui.Window;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;

import zero1hd.rhythmbullet.audio.MusicDataPack;

public class MusicController extends Window implements OnCompletionListener {
	Skin skin;
	private Image togglePlay;
	private TextField info;
	private MusicDataPack audiofile;
	
	public MusicController(final Skin skin) {
		super("Playback Controller", skin, "tinted");
		
		defaults().space(5f);
		
		this.skin = skin;
		final ImageButton rewind = new ImageButton(skin.getDrawable("left-double-arrow"));
		rewind.addListener(new ChangeListener() {
			
			@Override
			public void changed(ChangeEvent event, Actor actor) {
				if (audiofile.getPlaybackMusic() != null) {
					audiofile.getPlaybackMusic().setPosition(audiofile.getPlaybackMusic().getPosition() - 2);
					info.setText(String.valueOf(MathUtils.round(audiofile.getPlaybackMusic().getPosition())) + " sec");	
				}
			}
		});
		
		add(rewind).center();

		togglePlay = new Image(skin.getDrawable("loading")) {
			@Override
			public void act(float delta) {
				super.act(delta);
			}
		};
		togglePlay.addListener(new ClickListener() {
			@Override
			public void clicked(InputEvent event, float x, float y) {
				if (audiofile != null) {
					if (audiofile.getPlaybackMusic().isPlaying()) {
						audiofile.getPlaybackMusic().pause();
						
						togglePlay.setDrawable(skin.getDrawable("arrow"));

					} else {
						togglePlay.setDrawable(skin.getDrawable("pause"));
						audiofile.getPlaybackMusic().play();
					}
				}
				super.clicked(event, x, y);
			}
		});
		add(togglePlay).minWidth(togglePlay.getDrawable().getMinWidth()).center();

		final ImageButton fastForward = new ImageButton(skin.getDrawable("right-double-arrow"));
		fastForward.addListener(new ChangeListener() {
			@Override
			public void changed(ChangeEvent event, Actor actor) {
				if (audiofile != null) {
					audiofile.getPlaybackMusic().play();
					audiofile.getPlaybackMusic().setPosition(audiofile.getPlaybackMusic().getPosition() + 2);
					info.setText(String.valueOf(MathUtils.round(audiofile.getPlaybackMusic().getPosition())) + " sec");
				}				
			}
		});
		
		add(fastForward);
		
		info = new TextField(null, skin, "ui") {
			@Override
			public void act(float delta) {
				if (audiofile != null && audiofile.getPlaybackMusic().isPlaying()) {
					setText(String.valueOf(MathUtils.round(audiofile.getPlaybackMusic().getPosition())) + " sec");
				}
				super.act(delta);
			}
		};
		add(info);
		
		
		addListener(new InputListener() {
			@Override
			public boolean keyUp(InputEvent event, int keycode) {
				if (keycode == Keys.ENTER) {
					if (!info.getText().replaceAll("(?![0-9])\\S+", "").trim().isEmpty()) {
						audiofile.getPlaybackMusic().setPosition(Float.valueOf(info.getText().replaceAll("(?![0-9])\\S+", "").trim()));
					}
				}
				return super.keyUp(event, keycode);
			}
		});

		
		setSize(260, 75);
	}
	
	public void setAudiofile(MusicDataPack audiofile) {
		this.audiofile = audiofile;
		if (audiofile == null) {
			togglePlay.setDrawable(skin.getDrawable("loading"));
			info.setText("Analyzing...");
		} else {
			audiofile.reset();
			togglePlay.setDrawable(skin.getDrawable("arrow"));
			info.setText("Ready.");
			audiofile.getPlaybackMusic().play();
			audiofile.getPlaybackMusic().pause();
			audiofile.getPlaybackMusic().setOnCompletionListener(this);
		}
	}
	
	public MusicDataPack getAudiofile() {
		return audiofile;
	}

	@Override
	public void onCompletion(Music music) {
		audiofile.dispose();
		audiofile = null;
	}
}
