/*
 * Copyright (C) 2012,2013 Renard Wellnitz.
 * 
 * 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.renard.ocr;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.Pair;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.renard.ocr.help.OCRLanguageActivity;
import com.renard.ocr.help.OCRLanguageAdapter.OCRLanguage;
import com.renard.util.PreferencesUtils;

public class LayoutQuestionDialog {

	public enum LayoutKind {
		SIMPLE, COMPLEX, DO_NOTHING;
	}

	private static LayoutKind mLayout = LayoutKind.SIMPLE;
	private static String mLanguage;

	public interface LayoutChoseListener {
		void onLayoutChosen(final LayoutKind layoutKind, final String language);
	}

	public static AlertDialog createDialog(final Context context,
			final LayoutChoseListener listener) {

		mLayout = LayoutKind.SIMPLE;
		Pair<String, String> language = PreferencesUtils
				.getOCRLanguage(context);
		mLanguage = language.first;

		AlertDialog.Builder builder;

		// builder = new AlertDialog.Builder(new ContextThemeWrapper(context,
		// R.style.Theme_Sherlock_Light_Dialog));
		builder = new AlertDialog.Builder(context);
		builder.setCancelable(false);
		View layout = View.inflate(context, R.layout.dialog_layout_question,
				null);
		builder.setView(layout);

		final TextView speech = (TextView) layout.findViewById(R.id.help_header);
		speech.setText(context.getString(R.string.document_layout_dialog));
		speech.setCompoundDrawables(context.getResources().getDrawable(R.drawable.fairy_question), null,null,null);
		final RadioGroup layoutRadioGroup = (RadioGroup) layout
				.findViewById(R.id.radioGroup_layout_buttons);
		layoutRadioGroup.check(R.id.radio_simple);

		final Button langButton = (Button) layout
				.findViewById(R.id.button_language);
		langButton.setText(language.second);
		langButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				ChooseLanguageDialog.createDialog(context,
						new ChooseLanguageDialog.OnLanguageChosenListener() {

							@Override
							public void onLanguageChosen(OCRLanguage lang) {
								mLanguage = lang.getValue();
								langButton.setText(lang.getDisplayText());
								PreferencesUtils.saveOCRLanguage(context, lang);
							}
						}).show();

			}
		});

		builder.setPositiveButton(android.R.string.ok,
				new DialogInterface.OnClickListener() {

					public void onClick(DialogInterface dialog, int id) {
						final String lang;
						final String english = "eng";
						if (!mLanguage.equals(english)) {
							lang = mLanguage + "+" + english;
						} else {
							lang = mLanguage;
						}
						int checked = layoutRadioGroup
								.getCheckedRadioButtonId();
						if (checked == R.id.radio_complex) {
							mLayout = LayoutKind.COMPLEX;
						} else if (checked == R.id.radio_no_ocr) {
							mLayout = LayoutKind.DO_NOTHING;
						} else if (checked == R.id.radio_simple) {
							mLayout = LayoutKind.SIMPLE;
						}
						listener.onLayoutChosen(mLayout, lang);

					}
				});

		builder.setNegativeButton(R.string.cancel,
				new DialogInterface.OnClickListener() {

					public void onClick(DialogInterface dialog, int id) {
						dialog.cancel();
					}
				});
		final AlertDialog dialog = builder.create();

		final Button downloadButton = (Button) layout
				.findViewById(R.id.button_load_language);
		downloadButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent i = new Intent(context, OCRLanguageActivity.class);
				context.startActivity(i);
				dialog.cancel();
			}
		});

		return dialog;

	}
}

@@ -3279,16 +3279,44 @@
 english)
+&& addEnglishData(mLanguage)
 ) %7B%0A%09%09%09%09
@@ -4380,11 +4380,845 @@
 log;%0A%0A%09%7D
+%0A%0A    //when combining languages that have multi byte characters with english training data the ocr text gets corrupted%0A    //but adding english will improve overall accuracy for the other languages%0A    private static boolean addEnglishData(String mLanguage) %7B%0A        if (mLanguage.startsWith(%22chi%22)%7C%7C%0A                mLanguage.equalsIgnoreCase(%22kor%22)%7C%7C%0A                mLanguage.equalsIgnoreCase(%22hin%22)%7C%7C%0A                mLanguage.equalsIgnoreCase(%22heb%22)%7C%7C%0A                mLanguage.equalsIgnoreCase(%22ell%22)%7C%7C%0A                mLanguage.equalsIgnoreCase(%22bel%22)%7C%7C%0A                mLanguage.equalsIgnoreCase(%22ara%22)%7C%7C%0A                mLanguage.equalsIgnoreCase(%22grc%22)%7C%7C%0A                mLanguage.equalsIgnoreCase(%22rus%22)%7C%7C%0A                mLanguage.equalsIgnoreCase(%22vie%22))%7B%0A            return false;%0A%0A        %7D%0A        return true;%0A    %7D
 %0A%7D%0A

work around issue with multi byte encoding for tesseract results