package com.evgenii.aescrypto;

import java.io.IOException;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;

import com.evgenii.jsevaluator.JsEvaluator;

public class MainActivity extends Activity {

	public String currentDecryptMenuTitle;
	protected JsEncryptor mJsEncryptor;

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

		currentDecryptMenuTitle = getResources().getString(R.string.menu_decrypt_title);
		setContentView(R.layout.activity_main);

		final AssetsFileReader assetsFileReader = new AssetsFileReader(this);
		final JsEvaluator jsEvaluator = new JsEvaluator(this);
		mJsEncryptor = new JsEncryptor(this, assetsFileReader, jsEvaluator);
		try {
			mJsEncryptor.readScripts();
		} catch (final IOException e) {
			showFataErrorAlertAndExit("Can not read JavaScript program", e);
		}
		mJsEncryptor.evaluateScripts();
	}

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

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		menu.findItem(R.id.action_decrypt).setTitle(currentDecryptMenuTitle);
		return super.onPrepareOptionsMenu(menu);
	}

	public void onSendClicked(View view) {
		// mJsEvaluator.evaluate("2 * 17", new JsCallback() {
		// @Override
		// public void onResult(final String value) {
		// currentDecryptMenuTitle = value;
		// invalidateOptionsMenu();
		// }
		// });
	}

	public void showFataErrorAlertAndExit(String message, final Exception e) {
		new AlertDialog.Builder(this).setTitle("Fatal Error").setMessage(message)
				.setCancelable(false)
				.setNeutralButton(android.R.string.ok, new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						e.printStackTrace();
						finish();
					}
				}).show();
	}
}

@@ -408,236 +408,39 @@
 ;%0A%0A%09
-@Override%0A%09protected void onCreate(Bundle savedInstanceState) %7B%0A%09%09super.onCreate(savedInstanceState);%0A%0A%09%09currentDecryptMenuTitle = getResources().getString(R.string.menu_decrypt_title);%0A%09%09setContentView(R.layout.activity_main);%0A
+public void evaluateScripts() %7B
 %0A%09%09f
@@ -767,15 +767,13 @@
 ipt 
-program
+file.
 %22, e
@@ -813,24 +813,279 @@
 ipts();%0A%09%7D%0A%0A
+%09@Override%0A%09protected void onCreate(Bundle savedInstanceState) %7B%0A%09%09super.onCreate(savedInstanceState);%0A%0A%09%09currentDecryptMenuTitle = getResources().getString(R.string.menu_decrypt_title);%0A%09%09setContentView(R.layout.activity_main);%0A%0A%09%09evaluateScripts();%0A%09%7D%0A%0A
 %09@Override%0A%09
@@ -1702,16 +1702,209 @@
 on e) %7B%0A
+%09%09String errorMessage = e.getMessage();%0A%09%09if (errorMessage.length() %3E 200) %7B%0A%09%09%09errorMessage = errorMessage.substring(0, 199) + %22...%22;%0A%09%09%7D%0A%09%09message = message + %22%5Cn%5CnDetails: %22 + errorMessage;%0A
 %09%09new Al

Handle error when opening files