package com.akamai.ampdemoui;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ProgressBar;

import com.akamai.amp.media.VideoPlayerContainer;
import com.akamai.amp.media.VideoPlayerView;
import com.akamai.amp.media.elements.AMPLibraryInfo;
import com.akamai.amp.media.elements.MediaResource;
import com.akamai.amp.media.errors.ErrorType;
import com.akamai.amp.uimobile.listeners.IMediaPlayerControllerListener;
import com.akamai.amp.uimobile.media.MediaPlayerController;

import com.npaw.youbora.lib.ClientOptions;
import com.npaw.youbora.lib.YouboraClient;
import com.npaw.youbora.lib6.akamai.video.AkamaiAdapter;

public class MainActivity extends Activity implements IMediaPlayerControllerListener,
        VideoPlayerContainer.VideoPlayerContainerCallback {

    private static final String TAG = "AMP-UI-Sample";
    private static final String ANDROID_SDK_LICENSE = "isb3QJbqz9b2sDKO-362MyavEGd2EEH9aOSjSyWFHKB25jLLGy3xgyAYtFD_24uph3p0_1FPucKkslJbheijqQ==";
    private static final String MEDIA_ASSET_URL = "http://multiplatform-f.akamaihd.net/i/multi/will/bunny/big_buck_bunny_,640x360_400,640x360_700,640x360_1000,950x540_1500,.f4v.csmil/master.m3u8";

    private VideoPlayerContainer mVideoPlayerContainer;
    private VideoPlayerView mVideoPlayerView;
    private MediaPlayerController mMediaPlayerController;
    private ProgressBar mProgressBar;

    private boolean mFullScreenMode;

    // Youbora
    private YouboraClient youboraClient;

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

        if (savedInstanceState != null) {
            mFullScreenMode = savedInstanceState.getBoolean("FULLSCREEN");
        } else {
            mFullScreenMode = false;
        }

        mProgressBar = findViewById(R.id.progressBar);
        mVideoPlayerContainer = findViewById(R.id.playerViewCtrl);
        mVideoPlayerContainer.addVideoPlayerContainerCallback(this);
        mVideoPlayerContainer.enableAutoRecovery();
        mVideoPlayerContainer.prepareResource(MEDIA_ASSET_URL);
    }

    @Override
    public void onVideoPlayerCreated() {
        Log.i(TAG, "onVideoPlayerCreated()");
    }

    @Override
    public void onResourceReady(MediaResource mediaResource) {
        mVideoPlayerView = mVideoPlayerContainer.getVideoPlayer();
        initYoubora();

        mVideoPlayerView.setLicense(ANDROID_SDK_LICENSE);
        mVideoPlayerView.setLogEnabled(true);

        mMediaPlayerController = findViewById(R.id.playerControls);
        setFullScreenState(mFullScreenMode);
        mMediaPlayerController.setVideoPlayerView(mVideoPlayerContainer);
        mMediaPlayerController.setEventsListener(MainActivity.this);

        mVideoPlayerView.setProgressBarControl(mProgressBar);
        mVideoPlayerView.play(mediaResource);

        Log.i(TAG, "AMP v" + AMPLibraryInfo.VERSION);
    }

    private void initYoubora() {
        com.npaw.youbora.lib.utils.Log.INSTANCE.setCurrentLogLevel(com.npaw.youbora.lib.utils.Log.Level.VERBOSE);

        youboraClient = new YouboraClient(new ClientOptions() {{
            setAccountCode("powerinfinitydev");
        }}, this);

        youboraClient.getVideo().setExtension(new AkamaiAdapter(mVideoPlayerView));
    }

    @Override
    public void onResourceError(ErrorType errorType, Exception exception) {
        Log.e(TAG, "onResourceError()");
    }

    private void validateFullScreenState() {
        if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
        {
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        }
        else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
        {
            requestWindowFeature(Window.FEATURE_ACTION_BAR);
            this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN, WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
        }
    }

    @Override
    protected void onResume() {
        Log.e(TAG, "onResume");
        if (mVideoPlayerView != null) {
            mVideoPlayerView.onResume();
        }
        super.onResume();
    }

    @Override
    protected void onPause() {
        Log.e(TAG, "onPause");
        if(mVideoPlayerView != null) {
            mVideoPlayerView.onPause();
        }
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        Log.e(TAG, "onDestroy");
        if (mVideoPlayerView != null) {
            mVideoPlayerView.onDestroy();
        }
        super.onDestroy();
    }

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
        savedInstanceState.putBoolean("FULLSCREEN", mFullScreenMode);
        // Always call the superclass so it can save the view hierarchy state
        super.onSaveInstanceState(savedInstanceState);
    }

    private void setFullScreenMode(boolean fullscreen)
    {
        if(fullscreen == mFullScreenMode)
            return;
        mFullScreenMode = fullscreen;
        // Checks the orientation of the screen
        if (fullscreen) {
            mVideoPlayerView.setFullScreen(true);
            mVideoPlayerView.setFullScreenMode(VideoPlayerView.FULLSCREEN_MODE_KEEP_ASPECT_RATIO);

            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

        } else {
            mVideoPlayerView.setFullScreen(false);
            mVideoPlayerView.setFullScreenMode(VideoPlayerView.FULLSCREEN_MODE_NONE);

            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
        setFullScreenState(mFullScreenMode);
    }

    private void setFullScreenState(boolean fullscreen){
        if (fullscreen) {
            mMediaPlayerController.setCurrentFullScreenMode(MediaPlayerController.UI_MODE_FULLSCREEN);
        } else {
            mMediaPlayerController.setCurrentFullScreenMode(MediaPlayerController.UI_MODE_NON_FULLSCREEN);
        }
    }

    @Override
    public void onButtonClicked(int arg0) {
        if(arg0 == IMediaPlayerControllerListener.MEDIAPLAYERCONTROLLER_BUTTON_FULLSCREEN)
            setFullScreenMode(!mFullScreenMode);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        return false;
    }

}
