package yuku.alkitab.base.fr;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Handler;
import android.text.SpannableStringBuilder;
import android.text.style.StyleSpan;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TextView.BufferType;

import yuku.afw.V;
import yuku.alkitab.R;
import yuku.alkitab.base.S;
import yuku.alkitab.base.fr.base.BaseGotoFragment;
import yuku.alkitab.base.util.Jumper;

public class GotoDirectFragment extends BaseGotoFragment {
	public static final String TAG = GotoDirectFragment.class.getSimpleName();
	
	private static final String EXTRA_verse = "verse"; //$NON-NLS-1$
	private static final String EXTRA_chapter = "chapter"; //$NON-NLS-1$
	private static final String EXTRA_bookId = "bookId"; //$NON-NLS-1$

	TextView lContohLoncat;
	EditText tAlamatLoncat;
	View bOk;

	int bookId;
	int chapter_1;
	int verse_1;


	
	public static Bundle createArgs(int bookId, int chapter_1, int verse_1) {
		Bundle args = new Bundle();
		args.putInt(EXTRA_bookId, bookId);
		args.putInt(EXTRA_chapter, chapter_1);
		args.putInt(EXTRA_verse, verse_1);
		return args;
	}
	
	@Override public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		Bundle args = getArguments();
		if (args != null) {
			bookId = args.getInt(EXTRA_bookId, -1);
			chapter_1 = args.getInt(EXTRA_chapter);
			verse_1 = args.getInt(EXTRA_verse);
		}
	}

	@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View res = inflater.inflate(R.layout.fragment_goto_direct, container, false);
		lContohLoncat = V.get(res, R.id.lContohLoncat);
		tAlamatLoncat = V.get(res, R.id.tAlamatLoncat);
		bOk = V.get(res, R.id.bOk);

		bOk.setOnClickListener(bOk_click);
		
		tAlamatLoncat.setOnEditorActionListener(new TextView.OnEditorActionListener() {
			@Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
				bOk_click.onClick(bOk);
				return true;
			}
		});
		return res;
	}
	
	@Override public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		{
			String example = S.activeVersion.reference(bookId, chapter_1, verse_1);
			String text = getString(R.string.loncat_ke_alamat_titikdua);
			int pos = text.indexOf("%s"); //$NON-NLS-1$
			if (pos >= 0) {
				SpannableStringBuilder sb = new SpannableStringBuilder();
				sb.append(text.substring(0, pos));
				sb.append(example);
				sb.append(text.substring(pos + 2));
				sb.setSpan(new StyleSpan(Typeface.BOLD), pos, pos + example.length(), 0);
				lContohLoncat.setText(sb, BufferType.SPANNABLE);
			}
		}
		
		tAlamatLoncat.setOnFocusChangeListener(new OnFocusChangeListener() {
			@Override public void onFocusChange(View v, boolean hasFocus) {
				if (hasFocus) {
					showKeyboard();
				}
			}
		});
		tAlamatLoncat.requestFocus();
	}
	
	OnClickListener bOk_click = new OnClickListener() {
		@Override public void onClick(View v) {
			String reference = tAlamatLoncat.getText().toString();
			
			if (reference.trim().length() == 0) {
				return; // do nothing
			}
			
			Jumper jumper = new Jumper(reference);
			if (! jumper.getParseSucceeded()) {
				new AlertDialog.Builder(getActivity())
				.setMessage(getString(R.string.alamat_tidak_sah_alamat, reference))
				.setPositiveButton(R.string.ok, null)
				.show();
				return;
			}
			
			final int bookId = jumper.getBookId(S.activeVersion.getConsecutiveBooks());
			final int chapter = jumper.getChapter();
			final int verse = jumper.getVerse();
			
			InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
			imm.hideSoftInputFromWindow(tAlamatLoncat.getWindowToken(), InputMethodManager.HIDE_IMPLICIT_ONLY);
			imm.hideSoftInputFromWindow(tAlamatLoncat.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
			
			// try to fix a BUG on Samsung and HTC devices
			bOk.setEnabled(false);
			new Handler().postDelayed(new Runnable() {
				@Override public void run() {
					((GotoFinishListener) getActivity()).onGotoFinished(GotoFinishListener.GOTO_TAB_direct, bookId, chapter, verse);
				}
			}, 1000);
		}
	};

	public void onTabSelected() {
		showKeyboard();
	}

	private void showKeyboard() {
		if (getActivity() != null) { 
					InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
					imm.showSoftInput(tAlamatLoncat, InputMethodManager.SHOW_IMPLICIT);
				}
	}
}

@@ -152,35 +152,8 @@
 le;%0A
-import android.os.Handler;%0A
 impo
@@ -4210,166 +4210,8 @@
 %09%09%09%0A
-%09%09%09// try to fix a BUG on Samsung and HTC devices%0A%09%09%09bOk.setEnabled(false);%0A%09%09%09new Handler().postDelayed(new Runnable() %7B%0A%09%09%09%09@Override public void run() %7B%0A%09%09
 %09%09%09(
@@ -4326,27 +4326,8 @@
 e);%0A
-%09%09%09%09%7D%0A%09%09%09%7D, 1000);%0A
 %09%09%7D%0A
@@ -4450,18 +4450,16 @@
 ) %7B %0A%09%09%09
-%09
-%09
 InputMet
@@ -4561,18 +4561,16 @@
 CE);%0A%09%09%09
-%09%09
 imm.show
@@ -4629,18 +4629,16 @@
 LICIT);%0A
-%09%09
 %09%09%7D%0A%09%7D%0A%7D

Remove 1sec delay for closing keyboard on GotoDirectFragment, since bug of not-scrolled-to-verse has been fixed.