package gov.nasa.jpl.issm;

import com.google.android.youtube.player.YouTubeBaseActivity;
import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubePlayer;
import com.google.android.youtube.player.YouTubePlayer.Provider;
import com.google.android.youtube.player.YouTubePlayerView;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.widget.Toast;
import android.widget.VideoView;

public class VideoActivity extends YouTubeBaseActivity implements
		YouTubePlayer.OnInitializedListener {
	// String SrcPath =
	// "rtsp://v2.cache1.c.youtube.com/CjYLENy73wIaLQkXH3-3kcYu1RMYDSANFEIJbXYtZ29vZ2xlSARSBXdhdGNoYI-SyfPGoqqyUQw=/0/0/0/video.3gp";
	static private final String DEV_KEY = "AIzaSyBl8llEnYKwVp_myQGPNKtZYtHxvUUkqrc";
	static private final String VIDEO = "GuScXQznl9E";
	private YouTubePlayerView ytView;
	private YouTubePlayer ytPlayer;

	//
	// /** Called when the activity is first created. */
	// @Override
	// public void onCreate(Bundle savedInstanceState) {
	// super.onCreate(savedInstanceState);
	// setContentView(R.layout.videopage);
	// VideoView myVideoView = (VideoView)findViewById(R.id.videoview);
	// myVideoView.setVideoURI(Uri.parse(SrcPath));
	// myVideoView.setMediaController(new MediaController(this));
	// myVideoView.requestFocus();
	// myVideoView.start();
	// }
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getActionBar().setCustomView(R.layout.actionbar_top);
		getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
		overridePendingTransition(R.anim.right_in, R.anim.left_out);
		setContentView(R.layout.videopage);
		ytView = (YouTubePlayerView) findViewById(R.id.youtubeplayer);
		ytView.initialize(DEV_KEY, this);

		// Go back to the main menu
		Button start = (Button) findViewById(R.id.play_tab);
		start.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Intent i = new Intent(VideoActivity.this, MenuPage.class);
				startActivity(i);
				finish();
			}
		});
		// navigate to youtube video
		// Button video = (Button) findViewById(R.id.video_tab);
		// video.setOnClickListener(new View.OnClickListener() {
		// public void onClick(View v) {
		// // String url = "http://www.youtube.com/watch?v=KlDO0C8r_ws";
		// // Intent i = new Intent(Intent.ACTION_VIEW);
		// // i.setData(Uri.parse(url));
		// Intent i = new Intent(VideoActivity.this, VideoActivity.class);
		// startActivity(i);
		// finish();
		// }
		// });
		// pop up a text box that contains information about ISSM
		final Button intro = (Button) findViewById(R.id.about_tab);
		intro.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				// LayoutInflater layoutInflater =
				// (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
				// View popupView = layoutInflater.inflate(R.layout.popuptext,
				// null);
				// final PopupWindow popupWindow = new PopupWindow( popupView,
				// 400,600, true);
				// popupWindow.showAtLocation(popupView, Gravity.CENTER, 0, 0);
				// Button dismiss =
				// (Button)popupView.findViewById(R.id.dismiss);
				// dismiss.setOnClickListener(new Button.OnClickListener()
				// {
				// public void onClick(View v)
				// {
				// popupWindow.dismiss();
				// }
				// });
				Intent i = new Intent(VideoActivity.this, AboutActivity.class);
				startActivity(i);
				finish();
			}
		});
		// Show Team Website
		Button visitus = (Button) findViewById(R.id.team_tab);
		visitus.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				// String url = "http://issm.jpl.nasa.gov/";
				// Intent i = new Intent(Intent.ACTION_VIEW);
				// i.setData(Uri.parse(url));
				// startActivity(i);

				Intent i = new Intent(VideoActivity.this, TeamActivity.class);
				startActivity(i);
				finish();
			}
		});
		// start gallery image
		Button galleryImage = (Button) findViewById(R.id.gallery_tab);
		galleryImage.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Intent i = new Intent(VideoActivity.this, GalleryImage.class);
				startActivity(i);
				finish();
			}
		});

	}

	@Override
	public void onInitializationFailure(Provider provider,
			YouTubeInitializationResult error) {
		Toast.makeText(this, "Initialization Fail! " + error.toString(),
				Toast.LENGTH_LONG).show();
	}

	@Override
	public void onInitializationSuccess(Provider provider,
			YouTubePlayer player, boolean bool) {
		ytPlayer = player;
		Toast.makeText(this, "Initialization  Success.", Toast.LENGTH_LONG)
				.show();
		ytPlayer.loadVideo(VIDEO);

	}

	@Override
	public void onBackPressed() {
		Intent i = new Intent(VideoActivity.this, MenuPage.class);
		startActivity(i);
		finish();
	}

}
