package com.castlabs.sdk.switching;

import android.app.Activity;
import android.os.Bundle;
import android.os.Parcelable;
import androidx.annotation.NonNull;
import android.view.View;

import com.castlabs.android.SdkConsts;
import com.castlabs.android.drm.Drm;
import com.castlabs.android.drm.DrmTodayConfiguration;
import com.castlabs.android.player.PlayerController;
import com.castlabs.android.player.PlayerView;
import com.castlabs.sdk.channels.ChannelManager;
import com.castlabs.sdk.playerui.PlayerControllerProgressBar;
import com.castlabs.sdk.playerui.PlayerControllerView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends Activity {
	private static final String TAG = "MainActivity";
	private static Demo[] DEMOS = new Demo[]{
			new Demo("Channel 1", "Channel 1",
					new BundleBuilder()
							.put(SdkConsts.INTENT_URL, "http://usp-demo.castlabs.com/live/channel1/channel1.isml/channel1.mpd")
							.get()),
			new Demo("Channel 2", "Channel 2",
					new BundleBuilder()
							.put(SdkConsts.INTENT_URL, "http://usp-demo.castlabs.com/live/channel2/channel2.isml/channel2.mpd")
							.get()),
			new Demo("Channel 3", "Channel 3",
					new BundleBuilder()
							.put(SdkConsts.INTENT_URL, "http://usp-demo.castlabs.com/live/channel3/channel3.isml/channel3.mpd")
							.put(SdkConsts.INTENT_DRM_CONFIGURATION, new DrmTodayConfiguration.Builder(
									DrmTodayConfiguration.DRMTODAY_STAGING,
									"purchase",
									"sessionId",
									"six",
									"CL_CENC_TEST_LIVE2",
									Drm.Widevine
							).get())
							.get()),
			new Demo("Channel 1", "Channel 1",
					new BundleBuilder()
							.put(SdkConsts.INTENT_URL, "http://usp-demo.castlabs.com/live/channel1/channel1.isml/channel1.mpd")
							.get()),
			new Demo("Channel 2", "Channel 2",
					new BundleBuilder()
							.put(SdkConsts.INTENT_URL, "http://usp-demo.castlabs.com/live/channel2/channel2.isml/channel2.mpd")
							.get()),
			new Demo("Channel 3", "Channel 3",
					new BundleBuilder()
							.put(SdkConsts.INTENT_URL, "http://usp-demo.castlabs.com/live/channel3/channel3.isml/channel3.mpd")
							.put(SdkConsts.INTENT_DRM_CONFIGURATION, new DrmTodayConfiguration.Builder(
									DrmTodayConfiguration.DRMTODAY_STAGING,
									"purchase",
									"sessionId",
									"six",
									"CL_CENC_TEST_LIVE2",
									Drm.Widevine
							).get())
							.get()),
			new Demo("Channel 1", "Channel 1",
					new BundleBuilder()
							.put(SdkConsts.INTENT_URL, "http://usp-demo.castlabs.com/live/channel1/channel1.isml/channel1.mpd")
							.get()),
			new Demo("Channel 2", "Channel 2",
					new BundleBuilder()
							.put(SdkConsts.INTENT_URL, "http://usp-demo.castlabs.com/live/channel2/channel2.isml/channel2.mpd")
							.get()),
			new Demo("Channel 3", "Channel 3",
					new BundleBuilder()
							.put(SdkConsts.INTENT_URL, "http://usp-demo.castlabs.com/live/channel3/channel3.isml/channel3.mpd")
							.put(SdkConsts.INTENT_DRM_CONFIGURATION, new DrmTodayConfiguration.Builder(
									DrmTodayConfiguration.DRMTODAY_STAGING,
									"purchase",
									"sessionId",
									"six",
									"CL_CENC_TEST_LIVE2",
									Drm.Widevine
							).get())
							.get()),
	};

	private PlayerView playerView;
	private PlayerControllerView playerControllerView;
	private PlayerControllerProgressBar playerControllerProgressBar;
	private ChannelManager channelManager;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		playerView = (PlayerView) findViewById(R.id.player_view);
		playerControllerView = (PlayerControllerView) findViewById(R.id.player_controls);
		playerControllerProgressBar = (PlayerControllerProgressBar) findViewById(R.id.progress_bar);

		View nextButton = playerControllerView.find(R.id.presto_next_button);
		View previousButton = playerControllerView.find(R.id.presto_previous_button);
		nextButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				selectChannel(channelManager.getCurrentChannelIndex() + 1);
			}
		});
		previousButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				selectChannel(channelManager.getCurrentChannelIndex() - 1);
			}
		});

		List<Bundle> channleConfigs = new ArrayList<>(DEMOS.length);
		for (Demo demo : DEMOS) {
			channleConfigs.add(demo.bundle);
		}
		channelManager = new ChannelManager(channleConfigs, playerView, 2);
		channelManager.setListener(new ChannelManager.Listener() {
			@Override
			public void onControllerAttached(int channelIndex, @NonNull PlayerController playerController) {
				// bind UI
				playerControllerProgressBar.bind(playerController);
				playerControllerView.bind(playerView);
			}

			@Override
			public void onControllerDetached(int channelIndex, @NonNull PlayerController playerController) {
				// update UI bindings
				playerControllerProgressBar.unbind();
				playerControllerView.unbind();
			}
		});
		channelManager.selectChannel(0);
	}

	private void selectChannel(int nextChannel) {
		if (nextChannel < 0) {
			nextChannel = DEMOS.length - 1;
		} else if (nextChannel >= DEMOS.length) {
			nextChannel = 0;
		}
		channelManager.selectChannel(nextChannel);
	}

	@Override
	protected void onStart() {
		super.onStart();
		playerView.getLifecycleDelegate().start(this);
	}

	@Override
	protected void onResume() {
		super.onResume();
		// TODO: The channel manager needs lifecycle support
		playerControllerView.bind(playerView);
		playerControllerProgressBar.bind(playerView.getPlayerController());
		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);
		playerControllerView.unbind();
		playerControllerProgressBar.unbind();
	}


	static class Demo {
		public final String name;
		public final String description;
		private final Bundle bundle;

		Demo(String name, String description, Bundle bundle) {
			this.name = name;
			this.description = description;
			this.bundle = bundle;
		}

	}


	static class BundleBuilder {
		private final Bundle bundle;

		BundleBuilder() {
			this.bundle = new Bundle();
		}

		BundleBuilder put(String key, String value) {
			bundle.putString(key, value);
			return this;
		}

		BundleBuilder put(String key, int value) {
			bundle.putInt(key, value);
			return this;
		}

		BundleBuilder put(String key, boolean value) {
			bundle.putBoolean(key, value);
			return this;
		}

		BundleBuilder put(String key, Parcelable value) {
			bundle.putParcelable(key, value);
			return this;
		}

		Bundle get() {
			return bundle;
		}
	}
}
