/*
 * Copyright 2014 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.tango.tangojnimotiontracking;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup.LayoutParams;
import android.widget.TextView;

public class MotionTrackingActivity extends Activity {

	MotionTrackingView motionTrackingView;
	TextView tangoPoseStatusText;
	String[] poseStatuses = {"Initializing", "Valid", "Invalid", "Unknown"};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		motionTrackingView = new MotionTrackingView(this);
		tangoPoseStatusText = new TextView(this);

		setContentView(motionTrackingView);
		addContentView(tangoPoseStatusText, new LayoutParams(LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT));
		TangoJNINative.OnCreate();

		new Thread(new Runnable() {
			@Override
			public void run() {
				while (true) {
					try {
						Thread.sleep(10);
						final byte statusIndex = TangoJNINative
								.UpdateStatus();
						final String tangoPoseStatusString = poseStatuses[statusIndex];
						final String tangoPoseString = TangoJNINative.PoseToString();
						runOnUiThread(new Runnable() {
							@Override
							public void run() {
								try {
									tangoPoseStatusText.setText("Pose Status: "+tangoPoseStatusString+"\n"+tangoPoseString);
								} catch (Exception e) {
									e.printStackTrace();
								}
							}
						});
						
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		}).start();
	}

	@Override
	protected void onResume() {
		super.onResume();
		motionTrackingView.onResume();
		TangoJNINative.OnResume();
	}

	@Override
	protected void onPause() {
		super.onPause();
		motionTrackingView.onPause();
		TangoJNINative.OnPause();
	}

	protected void onDestroy() {
		super.onDestroy();
		TangoJNINative.OnDestroy();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.motion_tracking, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.action_first_camera:
			TangoJNINative.SetCamera(0);
			return true;
		case R.id.action_third_camera:
			TangoJNINative.SetCamera(1);
			return true;
		case R.id.action_top_camera:
			TangoJNINative.SetCamera(2);
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}
}

@@ -1002,16 +1002,17 @@
 uses = %7B
+ 
 %22Initial
@@ -1048,16 +1048,51 @@
 Unknown%22
+ %7D;%0A%09int%5B%5D statusCount = %7B 0, 0, 0 
 %7D;%0A%0A%09@Ov
@@ -1381,16 +1381,21 @@
 tParams(
+%0A%09%09%09%09
 LayoutPa
@@ -1412,21 +1412,17 @@
 CONTENT,
-%0A%09%09%09%09
+ 
 LayoutPa
@@ -1637,25 +1637,16 @@
 NINative
-%0A%09%09%09%09%09%09%09%09
 .UpdateS
@@ -1724,16 +1724,78 @@
 Index%5D;%0A
+%09%09%09%09%09%09if (statusIndex %3C 3)%0A%09%09%09%09%09%09%09statusCount%5BstatusIndex%5D++;%0A
 %09%09%09%09%09%09fi
@@ -1837,16 +1837,25 @@
 NINative
+%0A%09%09%09%09%09%09%09%09
 .PoseToS
@@ -2010,17 +2010,30 @@
 tatus: %22
-+
+%0A%09%09%09%09%09%09%09%09%09%09%09+ 
 tangoPos
@@ -2049,14 +2049,195 @@
 ring
-+
+ + 
 %22%5Cn%22
-+
+%0A%09%09%09%09%09%09%09%09%09%09%09+ %22StatusCount: Initializing--%22%0A%09%09%09%09%09%09%09%09%09%09%09+ statusCount%5B0%5D + %22   Valid--%22%0A%09%09%09%09%09%09%09%09%09%09%09+ statusCount%5B1%5D + %22   Invalid--%22%0A%09%09%09%09%09%09%09%09%09%09%09+ statusCount%5B2%5D + %22%5Cn%22%0A%09%09%09%09%09%09%09%09%09%09%09+ 
 tang
@@ -2341,22 +2341,16 @@
 %09%09%09%09%7D);%0A
-%09%09%09%09%09%09
 %0A%09%09%09%09%09%7D 

Add Pose status count on the screen to verify the Pose life cycle.