package gov.nasa.jpl.issm;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import gov.nasa.jpl.issm.MyWebViewClient;

public class AboutActivity extends BaseActivity {

	private WebView myWebView;
	String SrcPath = "file:///android_asset/";
	String html = "";

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// super.onCreate(savedInstanceState);
		// setContentView(R.layout.aboutpage);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.aboutpage);
		transition();
		loadAboutFile();
		// Go back to the main menu
		Button homeTab = (Button) findViewById(R.id.play_tab);
		homeTab.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Intent i = new Intent(AboutActivity.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(AboutActivity.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(AboutActivity.this, AboutActivity.class);
		// startActivity(i);
		// }
		// });
		// 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(AboutActivity.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(AboutActivity.this, GalleryImage.class);
				startActivity(i);
				finish();
			}
		});

		myWebView = (WebView) findViewById(R.id.aboutpage);
		myWebView.setWebViewClient(new MyWebViewClient());
		// myWebView.getSettings().setJavaScriptEnabled(true);
		// myWebView.loadUrl(SrcPath + "about.html");
		myWebView
				.loadDataWithBaseURL(SrcPath, html, "text/html", "utf-8", null);
	}

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

	private void loadAboutFile() {
		InputStream is;
		byte[] buffer = null;
		try {
			is = getResources().getAssets().open("about.html");
			int size = is.available();
			buffer = new byte[size];
			is.read(buffer);
			is.close();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		html = new String(buffer);
		html = html.replace("old string", "new string");
	}

}
