/*
 * Copyright 2016 Tim Harvey <harvey.tim@gmail.com>
 *
 * 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.tharvey.blocklybot;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

public class RobotControlActivity extends AppCompatActivity implements IConnection {
	private final static String TAG = RobotControlActivity.class.getSimpleName();

	private Mobbob mRobot;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_robot_control);
		mRobot = Mobbob.getMobob();
		if (mRobot != null) {
			mRobot.setConnectionListener(this);
			updateStatus();
		}
	}

	@Override
	public void connectionStateChanged(IConnection.connectionStateEnum state) {
		Log.i(TAG, "connection state changed:" + state);
		mRobot = Mobbob.getMobob();
		updateStatus();
	}

	private void updateStatus() {
		Log.i(TAG, "updateStatus");
		final ActionBar action = getSupportActionBar();
		if (action != null) {
			if (mRobot != null && mRobot.getConnectionState() == IConnection.connectionStateEnum.isConnected)
				action.setTitle("Remote Control: " + mRobot.getName());
			else
				action.setTitle("Remote Control: not connected");
		}
	}

	public void onClick(View v) {
		int cmd;
		switch (v.getId()) {
			case R.id.up:
				cmd = Mobbob.commands.MOVE_FORWARD.ordinal();
				break;
			case R.id.right:
				cmd = Mobbob.commands.TURN_RIGHT.ordinal();
				break;
			case R.id.left:
				cmd = Mobbob.commands.TURN_LEFT.ordinal();
				break;
			case R.id.down:
				cmd = Mobbob.commands.MOVE_BACKWARD.ordinal();
				break;
			case R.id.shake:
				cmd = Mobbob.commands.SHAKE_HEAD.ordinal();
				break;
			case R.id.bounce:
				cmd = Mobbob.commands.BOUNCE.ordinal();
				break;
			case R.id.wobble:
				cmd = Mobbob.commands.WOBBLE.ordinal();
				break;
			case R.id.wobble_right:
				cmd = Mobbob.commands.WOBBLE_RIGHT.ordinal();
				break;
			case R.id.wobble_left:
				cmd = Mobbob.commands.WOBBLE_LEFT.ordinal();
				break;
			case R.id.tap_feet:
				cmd = Mobbob.commands.TAP_FEET.ordinal();
				break;
			case R.id.tap_right:
				cmd = Mobbob.commands.TAP_FOOT_RIGHT.ordinal();
				break;
			case R.id.tap_left:
				cmd = Mobbob.commands.TAP_FOOT_LEFT.ordinal();
				break;
			case R.id.shake_legs:
				cmd = Mobbob.commands.SHAKE_LEGS.ordinal();
				break;
			case R.id.shake_right:
				cmd = Mobbob.commands.SHAKE_LEG_RIGHT.ordinal();
				break;
			case R.id.shake_left:
				cmd = Mobbob.commands.SHAKE_LEG_LEFT.ordinal();
				break;
			default:
				cmd = Mobbob.commands.STOP.ordinal();
				break;
		}
		if (mRobot != null)
			mRobot.doFunction(null, cmd, 1);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

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

		switch (item.getItemId()) {
			case R.id.menu_settings:
				intent = new Intent(this, SettingsActivity.class);
				startActivity(intent);
				break;
			case R.id.menu_about:
				AboutDialog about = new AboutDialog(this);
				about.setTitle("About this app");
				about.show();
				break;
			case R.id.menu_connect:
				DiscoverySelector dialog = new DiscoverySelector(this, this);
				dialog.showDialog();
				break;
			case R.id.menu_blockly:
				intent = new Intent(this, BlocklyActivity.class);
				startActivity(intent);
				break;
		}
		return true;
	}

	@Override
	protected void onStop() {
		Log.d(TAG, "onStop()");
		super.onStop();
		if (mRobot != null)
			mRobot.disconnect();
	}

	@Override
	protected void onStart() {
		Log.d(TAG, "onStart()");
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
		SharedPreferences.Editor editor = prefs.edit();
		editor.putString("pref_defaultView", "panel");
		editor.commit();
		super.onStart();
		if (mRobot != null)
			mRobot.connect();
	}

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == DiscoverySelector.REQUEST_ENABLE_BT) {
			if (resultCode == RESULT_OK) {
				DiscoverySelector dialog = new DiscoverySelector(this, this);
				dialog.showDialog();
			}
		}
	}
}

@@ -1773,24 +1773,103 @@
  != null) %7B%0A
+%09%09%09this.runOnUiThread(new Runnable() %7B%0A%09%09%09%09@Override%0A%09%09%09%09public void run() %7B%0A%09%09
 %09%09%09if (mRobo
@@ -1957,24 +1957,26 @@
 nected)%0A%09%09%09%09
+%09
+%09
 action.setTi
@@ -2019,16 +2019,18 @@
 ame());%0A
+%09%09
 %09%09%09else%0A
@@ -2029,24 +2029,26 @@
 %09%09%09else%0A%09%09%09%09
+%09%09
 action.setTi
@@ -2085,16 +2085,29 @@
 cted%22);%0A
+%09%09%09%09%7D%0A%09%09%09%7D);%0A
 %09%09%7D%0A%09%7D%0A%0A

fix crash on robot connect