package com.castlabs.sdk.youbora_demo;

import android.app.Activity;
import android.os.Bundle;
import androidx.annotation.NonNull;

import com.castlabs.sdk.youbora.YouboraAnalyticsSession;
import com.google.android.material.snackbar.Snackbar;
import android.util.Log;
import android.widget.Toast;

import com.castlabs.android.player.AbstractPlayerListener;
import com.castlabs.android.player.PlayerView;
import com.castlabs.android.player.exceptions.CastlabsPlayerException;
import com.castlabs.sdk.playerui.PlayerControllerProgressBar;
import com.castlabs.sdk.playerui.PlayerControllerView;
import com.npaw.youbora.lib6.plugin.Plugin;

public class SimplePlaybackDemo extends Activity {
	private static final String TAG = "SimplePlaybackDemo";
	// This is the player view that we use to start playback
	private PlayerView playerView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_simple_playback_demo);

		// Get the view components from the layout
		playerView = (PlayerView) findViewById(R.id.player_view);

		// (Optional) Youbora Sessions tracking
		Plugin youboraPlugin = ((DemoApp) getApplicationContext()).youboraPlugin;
		youboraPlugin.setActivity(this);
		youboraPlugin.getInfinity().begin(TAG);

		// (Optional) Set the Youbora plugin to use before opening the PlayerController
		// This is not needed if the Youbora's Plugin instance is the same as set on
		// the app's onCreate
		YouboraAnalyticsSession component = playerView.getPlayerController().getComponent(YouboraAnalyticsSession.class);
		//component.setPlugin(youboraPlugin);

		// This demo assumes that you send an intent to this Activity that contains the
		// playback information. We need to pass the intents bundle on to the PlayerController
		// to start playback. The open() method might throw an Exception in case the bundle
		// contains not all mandatory parameters or the parameters are malformed.
		if (getIntent() != null) {
			try {
				playerView.getPlayerController().open(getIntent().getExtras());
			} catch (Exception e) {
				Log.e(TAG, "Error while opening player: " + e.getMessage(), e);
				Snackbar.make(playerView, "Error while opening player: " + e.getMessage(),
						Snackbar.LENGTH_INDEFINITE).show();
			}
		} else {
			Snackbar.make(playerView, "No intent specified", Snackbar.LENGTH_INDEFINITE).show();
		}
	}

	// Delegate the onStart event to the player views lifecycle delegate.
	// The delegate will make sure that the screen safer will be disabled and
	// the display will not go to sleep
	@Override
	protected void onStart() {
		super.onStart();
		playerView.getLifecycleDelegate().start(this);
	}

	// Delegate the onResume event to the player views lifecycle delegate.
	// The delegate ensures that the player recovers from a saved state. This needs to
	// be implemented to ensure the the user can for example go to the home screen and
	// come back to this activity.
	@Override
	protected void onResume() {
		super.onResume();

		final PlayerControllerView controls = (PlayerControllerView) findViewById(R.id.player_controls);
		controls.bind(playerView);

		PlayerControllerProgressBar progressBar = (PlayerControllerProgressBar) findViewById(R.id.progress_bar);
		progressBar.bind(playerView.getPlayerController());

		playerView.getPlayerController().addPlayerListener(new AbstractPlayerListener() {
			@Override
			public void onError(@NonNull CastlabsPlayerException error) {
				Toast.makeText(SimplePlaybackDemo.this, error.toString(), Toast.LENGTH_LONG).show();
			}
		});

		playerView.getLifecycleDelegate().resume();
	}

	// Delegate the onStop event to the player views lifecycle delegate.
	// We release the player when the activity is stopped. This will release all the player
	// resources and save the current playback state. Saving the state is required so the
	// onResume callback can recover properly.
	@Override
	protected void onStop() {
		super.onStop();
		playerView.getLifecycleDelegate().releasePlayer(false);
	}
}
