package com.castlabs.sdk.demos;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

import com.castlabs.android.PlayerSDK;
import com.castlabs.sdk.debug.DebugPlugin;
import com.castlabs.sdk.debug.DebugPlugin.DebugOverlayPosition;
import com.castlabs.sdk.demos.databinding.ActivityDebugPluginBinding;

public class DebugPluginActivity extends AppCompatActivity {

	private ActivityDebugPluginBinding b;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
		b = ActivityDebugPluginBinding.inflate(getLayoutInflater());
        setContentView(b.getRoot());
		initial();
		openPlayer();
    }

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

	@Override
	protected void onResume() {
		super.onResume();
		b.playerControllerView.bind(b.playerView);
		b.playerView.getLifecycleDelegate().resume();
	}

	@Override
	protected void onStop() {
		super.onStop();
		b.playerControllerView.unbind();
		b.playerView.getLifecycleDelegate().releasePlayer(false);
	}

	private void initial() {

		b.playerControllerView.showExtendedTrackInfo(true);
		final DebugPlugin debugPlugin = PlayerSDK.getPlugin(DebugPlugin.class);
		if(debugPlugin != null) {
			debugPlugin.showDebugOverlay();
		}

		b.btShow.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (debugPlugin != null) debugPlugin.showDebugOverlay();
			}
		});

		b.btHide.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (debugPlugin != null) debugPlugin.hideDebugOverlay();
			}
		});

		b.btFullScreen.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (debugPlugin != null)
					debugPlugin.setDebugOverlayPosition(DebugOverlayPosition.Fullscreen);
			}
		});

		b.btLeftTop.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (debugPlugin != null)
					debugPlugin.setDebugOverlayPosition(DebugOverlayPosition.LeftTop);
			}
		});

		b.btRightTop.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (debugPlugin != null)
					debugPlugin.setDebugOverlayPosition(DebugOverlayPosition.RightTop);
			}
		});

		b.btLeftBottom.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (debugPlugin != null)
					debugPlugin.setDebugOverlayPosition(DebugOverlayPosition.LeftBottom);
			}
		});

		b.btRightBottom.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (debugPlugin != null)
					debugPlugin.setDebugOverlayPosition(DebugOverlayPosition.RightBottom);
			}
		});
	}

	private void openPlayer() {
		Bundle playbackBundle = getIntent().getExtras();
		b.playerView.getPlayerController().open(playbackBundle);
	}
}
