package com.castlabs.sdk.conviva_demo;

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

import com.castlabs.analytics.AnalyticsSession;
import com.castlabs.android.player.PlayerController;
import com.castlabs.android.player.PlayerViewLifecycleDelegate;
import com.castlabs.sdk.conviva.ConvivaAnalyticsSession;
import com.conviva.sdk.ConvivaAnalytics;
import com.conviva.sdk.ConvivaVideoAnalytics;
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 java.util.HashMap;

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);

		// This is just an example showing on how to send custom events
		playerView.getPlayerController().addPlayerListener(new AbstractPlayerListener() {
			@Override
			public void onPlayerModelChanged() {
				super.onPlayerModelChanged();
				reportCustomAppEvent();
				reportCustomPlaybackEvent();
			}
		});

		// 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);
	}

	/**
	 * Sample showing on how to send custom player insights associated with the video playback
	 */
	private void reportCustomPlaybackEvent() {
		ConvivaAnalyticsSession convivaAnalyticsSession = playerView.getPlayerController().getComponent(ConvivaAnalyticsSession.class);
		if (convivaAnalyticsSession != null) {
			String eventType = "sampleCustomPlaybackEvent";
			HashMap<String, Object> event = new HashMap<>();
			event.put("sampleKey", "sampleValue");
			ConvivaVideoAnalytics videoAnalytics = convivaAnalyticsSession.getVideoAnalytics();
			if (videoAnalytics != null) {
				Log.d(TAG, "Report custom playback event");
     			videoAnalytics.reportPlaybackEvent(eventType, event);
			}
		}
	}

	/**
	 * Sample showing on how to send custom player insights NOT associated with the video playback
	 */
	private void reportCustomAppEvent() {
		String eventType = "sampleCustomAppEvent";
		HashMap<String, Object> event = new HashMap<> ();
		event.put("sampleKey", "sampleValue");
		Log.d(TAG, "Report custom app event");
		ConvivaAnalytics.reportAppEvent(eventType, event);
	}
}
