/*
 * ************************************************************************
 *
 * ADOBE SYSTEMS INCORPORATED
 * Copyright 2013 Adobe Systems Incorporated
 * All Rights Reserved.
 * NOTICE:  Adobe permits you to use, modify, and distribute this file in accordance with the 
 * terms of the Adobe license agreement accompanying it.  If you have received this file from a 
 * source other than Adobe, then your use, modification, or distribution of it requires the prior 
 * written permission of Adobe.
 *
 **************************************************************************
 */

/*
 *  PlayerActivity class contains the UI Component the menu screen for the PSDK player which also displays the Home button,Logs button and the Settings button.
 *  This class handles the user interaction with menu.
 */

package com.adobe.primetime.reference.ui.player;

import android.annotation.TargetApi;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.Window;

import com.adobe.mediacore.Version;
import com.adobe.primetime.reference.PrimetimeReference;
import com.adobe.primetime.reference.R;
import com.adobe.primetime.reference.ui.catalog.CatalogActivity;
import com.adobe.primetime.reference.ui.catalog.VideoItem;
import com.adobe.primetime.reference.ui.log.LogActivity;
import com.adobe.primetime.reference.ui.settings.SettingsActivity;

public class PlayerActivity extends AppCompatActivity implements
		PlayerClickableAdFragment.OnAdUserInteraction {
	private final String LOG_TAG = PrimetimeReference.LOG_APP_NAME + "PlayerActivity";

	private VideoItem _contentInfo;

	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	@Override
	public void onCreate(Bundle savedInstanceState) {
		PrimetimeReference.logger.i(LOG_TAG + "#onCreate", "Player activity created.");
		super.onCreate(savedInstanceState);
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
			getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
		}
		setContentView(R.layout.activity_player);
		// setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
		setVolumeControlStream(AudioManager.STREAM_MUSIC);

		initialize(getIntent());
	}

	private void logVersion() {
		PrimetimeReference.logger.i(LOG_TAG + "#logVersion",
				"TVSDK version: " + Version.getVersion() + ".");
	}

	@Override
	protected void onNewIntent(Intent intent) {
		PrimetimeReference.logger.i(LOG_TAG + "::onNewIntent", "New media item.");
		// Initialize with the new item.
		setIntent(intent);
		initialize(intent);
	}

	private void initialize(Intent intent) {
		_contentInfo = (VideoItem) intent.getExtras().getSerializable(
				"CONTENT_INFO");
		ActionBar actionBar = getSupportActionBar();
		actionBar.setDisplayHomeAsUpEnabled(true);
		actionBar.setTitle(_contentInfo.getTitle());
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.actionbar_player, menu);

		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		Intent intent;

		switch (item.getItemId()) {
		case android.R.id.home:
			PrimetimeReference.logger.i(LOG_TAG + "#onOptionsItemSelected",
					"Home action bar item activated.");

			intent = new Intent(this, CatalogActivity.class);
			intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent);
			return true;

		case R.id.ActionBarLogs:
			PrimetimeReference.logger.i(LOG_TAG + "#onOptionsItemSelected",
					"Logs action bar item activated.");

			intent = new Intent(this, LogActivity.class);
			startActivity(intent);
			return true;

		case R.id.ActionBarSettings:
			PrimetimeReference.logger.i(LOG_TAG + "#onOptionsItemSelected",
					"Settings action bar item activated.");

			intent = new Intent(this, SettingsActivity.class);
			startActivity(intent);
			return true;

		case R.id.ActionBarAbout:
			PrimetimeReference.logger.i(LOG_TAG + "#onOptionsItemSelected",
					"About action bar item activated.");
			PrimetimeReference.showAbout(this);
			return true;

		default:
			return super.onOptionsItemSelected(item);
		}
	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		super.onRestoreInstanceState(savedInstanceState);

		PlayerFragment fragment = (PlayerFragment) getSupportFragmentManager()
				.findFragmentById(R.id.playerFragment);
		fragment.onFragmentRestoreInstanceState(savedInstanceState);
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		PlayerFragment fragment = (PlayerFragment) getSupportFragmentManager()
				.findFragmentById(R.id.playerFragment);
		fragment.onFragmentSaveInstanceState(outState);
		super.onSaveInstanceState(outState);
	}

	@Override
	public void onAdClick() {
		PlayerFragment playerFragment = (PlayerFragment) getSupportFragmentManager()
				.findFragmentById(R.id.playerFragment);

		if (playerFragment != null) {
			playerFragment.notifyAdClick();
		}
	}
}
