package de.nicidienase.chaosflix.touch.playback;

import android.util.Log;

import com.google.android.exoplayer2.ExoPlaybackException;
import com.google.android.exoplayer2.PlaybackParameters;
import com.google.android.exoplayer2.Player;
import com.google.android.exoplayer2.SimpleExoPlayer;
import com.google.android.exoplayer2.Timeline;
import com.google.android.exoplayer2.source.TrackGroupArray;
import com.google.android.exoplayer2.trackselection.TrackSelectionArray;

/**
 * Created by felix on 27.09.17.
 */
class PlayerEventListener implements Player.EventListener, SimpleExoPlayer.VideoListener {
	private static final String TAG = PlayerEventListener.class.getSimpleName();
	private SimpleExoPlayer           player;
	private PlayerStateChangeListener listener;

	public PlayerEventListener(SimpleExoPlayer player, PlayerStateChangeListener listener) {
		this.player = player;
		this.listener = listener;
	}

	@Override
	public void onTimelineChanged(Timeline timeline, Object manifest, int reason) {

	}

	@Override
	public void onTracksChanged(TrackGroupArray trackGroups, TrackSelectionArray trackSelections) {

	}

	@Override
	public void onLoadingChanged(boolean isLoading) {
		if (isLoading && player.getPlaybackState() != Player.STATE_READY) {
			listener.notifyLoadingStart();
		} else {
			listener.notifyLoadingFinished();
		}

	}

	@Override
	public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
		switch (playbackState) {
			case Player.STATE_BUFFERING:
				if (player.isLoading()) {
					listener.notifyLoadingStart();
				}
				break;
			case Player.STATE_ENDED:
				Log.d(TAG, "Finished Playback");
				listener.notifyEnd();
				break;
			case Player.STATE_IDLE:
			case Player.STATE_READY:
			default:
				listener.notifyLoadingFinished();
		}
	}

	@Override
	public void onRepeatModeChanged(int repeatMode) {

	}

	@Override
	public void onShuffleModeEnabledChanged(boolean shuffleModeEnabled) {

	}

	@Override
	public void onPlayerError(ExoPlaybackException error) {
		String errorMessage = error.getCause().getMessage();
		listener.notifyError(errorMessage);
		Log.d(TAG, errorMessage, error);
	}

	@Override
	public void onPositionDiscontinuity(int reason) {

	}

	@Override
	public void onPlaybackParametersChanged(PlaybackParameters playbackParameters) {

	}

	@Override
	public void onSeekProcessed() {

	}

	@Override
	public void onVideoSizeChanged(int width, int height, int unappliedRotationDegrees, float pixelWidthHeightRatio) {

	}

	@Override
	public void onRenderedFirstFrame() {
		listener.notifyLoadingFinished();
	}

	public interface PlayerStateChangeListener {
		void notifyLoadingStart();

		void notifyLoadingFinished();

		void notifyError(String errorMessage);

		void notifyEnd();
	}
}

@@ -991,20 +991,8 @@
 fest
-, int reason
 ) %7B%0A
@@ -1849,95 +1849,8 @@
 %09%7D%0A%0A
-%09@Override%0A%09public void onShuffleModeEnabledChanged(boolean shuffleModeEnabled) %7B%0A%0A%09%7D%0A%0A
 %09@Ov
@@ -2097,18 +2097,8 @@
 ity(
-int reason
 ) %7B%0A
@@ -2204,57 +2204,8 @@
 %09%7D%0A%0A
-%09@Override%0A%09public void onSeekProcessed() %7B%0A%0A%09%7D%0A%0A
 %09@Ov

fix Exoplayer EventListener