/*
 * Copyright (C) 2008,2009  OMRON SOFTWARE Co., Ltd.
 *
 * 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.

 * test message
 */

package com.hiroshica.android.input.nicownn2.JAJP;

import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputConnection;
import android.inputmethodservice.Keyboard;
import android.util.Log;
import android.view.View;
import android.content.SharedPreferences;

import android.preference.PreferenceManager;


import java.util.HashMap;
import java.util.Locale;
import java.util.List;

import com.hiroshica.android.input.nicownn2.*;

/**
 * The default Software Keyboard class for Japanese IME.
 *
 * @author Copyright (C) 2009 OMRON SOFTWARE CO., LTD.  All Rights Reserved.
 */
public class DefaultSoftKeyboardTest extends DefaultSoftKeyboard {
	/** new keycode **/
	private static final int KEYCODE_NEWKEY_0 = -410;
	private static final int KEYCODE_NEWKEY_1 = -411;
	private static final int KEYCODE_NEWKEY_2 = -412;
	private static final int KEYCODE_NEWKEY_3 = -413;
	private static final int KEYCODE_NEWKEY_4 = -414;

	/** Enable English word prediction on half-width alphabet mode */
	private static final boolean USE_ENGLISH_PREDICT = true;

	/** Key code for case toggle key */
	private static final int KEYCODE_SELECT_CASE = -309;

	/** Key code for EISU-KANA conversion */
	private static final int KEYCODE_EISU_KANA = -305;

	/** Key code for switching to full-width Nicotouch mode */
	private static final int KEYCODE_SWITCH_FULL_NICO = -400;

	/** Key code for NOP (no-operation) */
	private static final int KEYCODE_NOP = -310;


	/** Input mode toggle cycle table */
	private static final int[] JP_MODE_CYCLE_TABLE = {
		KEYMODE_JA_FULL_NICO,
	};

	/** Definition for {@code mInputType} (toggle) */
	private static final int INPUT_TYPE_TOGGLE = 1;

	/** Definition for {@code mInputType} (commit instantly) */
	private static final int INPUT_TYPE_INSTANT = 2;

	/** Max key number of the 12 key keyboard (depends on the definition of keyboards) */
	private static final int KEY_NUMBER_12KEY = 20;

	/* */
	SetupKeyboard              mSetupKeyboard;
	String[][][]               mCycleTable;
	HashMap<String, String>    mReplaceTable;
	
	/** Character table for full-width number */
	private static final char[] INSTANT_CHAR_CODE_FULL_NUMBER = 
		"\uff11\uff12\uff13\uff14\uff15\uff16\uff17\uff18\uff19\uff10\uff03\uff0a".toCharArray();

	/** Character table for half-width number */
	private static final char[] INSTANT_CHAR_CODE_HALF_NUMBER = 
		"1234567890#*".toCharArray();

	/** The constant for mFixedKeyMode. It means that input mode is not fixed. */
	private static final int INVALID_KEYMODE = -1;

	/** KeyIndex of "Moji" key on 12 keyboard (depends on the definition of keyboards) */
	private static final int KEY_INDEX_CHANGE_MODE_12KEY = 15;
    
	/** KeyIndex of "Moji" key on QWERTY keyboard (depends on the definition of keyboards) */
	private static final int KEY_INDEX_CHANGE_MODE_QWERTY = 29;

	/** Type of input mode */
	private int mInputType = INPUT_TYPE_TOGGLE;

	private static final NicoWnnEvent mEventTouchOtherKey    = new NicoWnnEvent(NicoWnnEvent.TOUCH_OTHER_KEY);
	private static final NicoWnnEvent mEventCommitText       = new NicoWnnEvent(NicoWnnEvent.COMMIT_COMPOSING_TEXT);
	private static final NicoWnnEvent mEventConvert          = new NicoWnnEvent(NicoWnnEvent.CONVERT);

	private static final NicoWnnEvent mEventChangeMode12Key    = new NicoWnnEvent(NicoWnnEvent.CHANGE_MODE, NicoWnnJAJP.ENGINE_MODE_OPT_TYPE_12KEY);
	private static final NicoWnnEvent mEventChangeModeQwerty   = new NicoWnnEvent(NicoWnnEvent.CHANGE_MODE, NicoWnnJAJP.ENGINE_MODE_OPT_TYPE_QWERTY);
	private static final NicoWnnEvent mEventChangeModeSymbol   = new NicoWnnEvent(NicoWnnEvent.CHANGE_MODE, NicoWnnJAJP.ENGINE_MODE_SYMBOL);
	private static final NicoWnnEvent mEventChangeModeDocomo   = new NicoWnnEvent(NicoWnnEvent.CHANGE_MODE, NicoWnnJAJP.ENGINE_MODE_DOCOMOSYMBOL);
	private static final NicoWnnEvent mEventChangeModeEisuKana = new NicoWnnEvent(NicoWnnEvent.CHANGE_MODE, NicoWnnJAJP.ENGINE_MODE_EISU_KANA);

	private static final NicoWnnEvent mEventInputShiftLeft   = new NicoWnnEvent(NicoWnnEvent.INPUT_SOFT_KEY, new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_SHIFT_LEFT));

	private static final NicoWnnEvent mEventInputKeyDel      = new NicoWnnEvent(NicoWnnEvent.INPUT_SOFT_KEY, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL));
	private static final NicoWnnEvent mEventInputEnter       = new NicoWnnEvent(NicoWnnEvent.INPUT_SOFT_KEY, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ENTER));
	private static final NicoWnnEvent mEventInputBack        = new NicoWnnEvent(NicoWnnEvent.INPUT_KEY, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK));
	private static final NicoWnnEvent mEventInputDpadLeft    = new NicoWnnEvent(NicoWnnEvent.INPUT_SOFT_KEY, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_LEFT));
	private static final NicoWnnEvent mEventInputDpadRight   = new NicoWnnEvent(NicoWnnEvent.INPUT_SOFT_KEY, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_RIGHT));


	/**
	 * Character table to input when mInputType becomes INPUT_TYPE_INSTANT.
	 * (Either INSTANT_CHAR_CODE_FULL_NUMBER or INSTANT_CHAR_CODE_HALF_NUMBER)
	 */
	private char[] mCurrentInstantTable = null;

	/** Input mode that is not able to be changed. If ENABLE_CHANGE_KEYMODE is set, input mode can change. */
	private int[] mLimitedKeyMode = null;

	/** Input mode that is given the first priority. If ENABLE_CHANGE_KEYMODE is set, input mode can change. */
	private int mPreferenceKeyMode = INVALID_KEYMODE;

	/** The last input type */
	private int mLastInputType = 0;

	/** Auto caps mode */
	private boolean mEnableAutoCaps = true;

	/** PopupResId of "Moji" key (this is used for canceling long-press) */
	private int mPopupResId = 0;
    
	/** Whether the InputType is null */
	private boolean mIsInputTypeNull = false;
    
	/** {@code SharedPreferences} for save the keyboard type */
	private SharedPreferences.Editor mPrefEditor = null;
    
	/** "Moji" key (this is used for canceling long-press) */
	private Keyboard.Key mChangeModeKey = null;
    
	private boolean mNicoFirst = false;

	private Keyboard[][] mNicoKeyboard;

	private boolean mGetNoFlipScreen = false;
	private boolean mNoFlipScreen = false;

	/** option keyboard type */
	private int     mHookStockKeyType;

	/** change eisu change mode **/
	private boolean mChange12keyQwertyMode = false;
	private final int[] m12keyTable = {
		KEYBOARD_12KEY,  KEYBOARD_12KEY, KEYBOARD_QWERTY, KEYBOARD_QWERTY,  KEYBOARD_12KEY, KEYBOARD_QWERTY, KEYBOARD_QWERTY,  KEYBOARD_12KEY,  KEYBOARD_12KEY
	};
	/** option bell type **/
	private final int ALPHAMODE_SMALL = 0;
	private final int ALPHAMODE_BIG   = 1;
	private int mChangeAlphaBigMode = ALPHAMODE_SMALL;

	/** Default constructor */
	public DefaultSoftKeyboardTest(SetupKeyboard keyboard) {
		mSetupKeyboard       = keyboard;
		mCycleTable   = keyboard.SetupCycleTable();
		mReplaceTable = keyboard.SetupReplaceTable();

		mCurrentLanguage     = LANG_JA;
		mCurrentKeyboardType = KEYBOARD_12KEY;
		mHookStockKeyType    = KEYBOARD_12KEY;
		mShiftOn             = KEYBOARD_SHIFT_OFF;
		mCurrentKeyMode      = KEYMODE_JA_FULL_NICO;
		mCurrentSlide        = NICO_SLIDE_MODE_TOP;
	}

	/** @see com.hiroshica.android.input.nicownn2.DefaultSoftKeyboard#createKeyboards */
	@Override protected void createKeyboards(NicoWnn parent) {

		/* Keyboard[# of Languages][portrait/landscape][# of keyboard type][shift off/on][max # of key-modes][noinput/input] */
		mKeyboard = new Keyboard[3][2][4][2][9][2];

		SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(parent);
		mChange12keyQwertyMode = pref.getBoolean("change_12key_qwerty", false);
		boolean getalphamode  = pref.getBoolean("change_alphamode", false);
		mGetNoFlipScreen       = pref.getBoolean("no_flip_screen", false);
		mFlickNicoInput        = flickModeTable.get(pref.getString("nicoflick_mode", "none_flick"));
		//mHiddenSoftKeyboard    = pref.getBoolean("hidden_softkeyboard", false);
		
		if (false == getalphamode) {
			mChangeAlphaBigMode = ALPHAMODE_SMALL;
		}
		else{
			mChangeAlphaBigMode = ALPHAMODE_BIG;
		}

		if (true == mGetNoFlipScreen) {
			mNoFlipScreen = true;
		}
		else{
			mNoFlipScreen = false;
		}
		
		/* Create the suitable keyboard object */
		if (mDisplayMode == DefaultSoftKeyboard.PORTRAIT) {
			//mCurrentKeyboardType = KEYBOARD_12KEY;
			createKeyboardsPortrait(parent);
		} else {
			//mCurrentKeyboardType = KEYBOARD_QWERTY;
			createKeyboardsLandscape(parent);
		}
			
		if (mCurrentKeyboardType == KEYBOARD_12KEY) {
			mWnn.onEvent(mEventChangeMode12Key);
		} else {
			mWnn.onEvent(mEventChangeModeQwerty);
		}
	}

	/**
	 *
	 */
	@Override public void checkHiddenKeyboard() {
		super.checkHiddenKeyboard();
	}

	/**
	 * Commit the pre-edit string for committing operation that is not explicit
	 * (ex. when a candidate is selected)
	 */
	private void commitText() {
		if (!mNoInput) {
			mWnn.onEvent(mEventCommitText);
		}
	}

	/**
	 * Change input mode
	 * <br>
	 * @param keyMode   The type of input mode
	 */
	public void changeKeyMode(int keyMode) {
		int targetMode = filterKeyMode(keyMode);
		if (targetMode == INVALID_KEYMODE) {
			return;
		}
        
		commitText();

		if (mCapsLock) {
			mWnn.onEvent(mEventInputShiftLeft);
			mCapsLock = false;
		}
		mShiftOn = KEYBOARD_SHIFT_OFF;
		Keyboard kbd = getModeChangeKeyboard(targetMode);
		mCurrentKeyMode = targetMode;
		mPrevInputKeyCode = 0;
		mCurrentSlide = 0;
        
		int mode = NicoWnnEvent.Mode.DIRECT;
        
		switch (targetMode) {

		case KEYMODE_JA_FULL_NICO:
			mInputType = INPUT_TYPE_TOGGLE;
			mode = NicoWnnEvent.Mode.DEFAULT;
			mNicoFirst = false;
			mNicoFlick = false;
			break;

		default:
			break;
		}
        
		setStatusIcon();
		changeKeyboard(kbd);
		mWnn.onEvent(new NicoWnnEvent(NicoWnnEvent.CHANGE_MODE, mode));
		if (mChange12keyQwertyMode == true) {
			changeKeyboardType(m12keyTable[targetMode]);
		}
	}

	/** @see com.hiroshica.android.input.nicownn2.DefaultSoftKeyboard#initView */
	@Override public View initView(NicoWnn parent, int width, int height) {
		View view = super.initView(parent, width, height);
		changeKeyboard(mKeyboard[mCurrentLanguage][mDisplayMode][mCurrentKeyboardType][mShiftOn][mCurrentKeyMode][0]);
		return view;
	}

	/** @see com.hiroshica.android.input.nicownn2.DefaultSoftKeyboard#changeKeyboard */
	@Override protected boolean changeKeyboard(Keyboard keyboard) {

		if (keyboard != null) {
			if (mIsInputTypeNull) {
				mChangeModeKey.popupResId = mPopupResId;
			}

			List<Keyboard.Key> keys = keyboard.getKeys();
			int keyIndex = (KEY_NUMBER_12KEY < keys.size())
				? KEY_INDEX_CHANGE_MODE_QWERTY : KEY_INDEX_CHANGE_MODE_12KEY;
			mChangeModeKey = keys.get(keyIndex);

			if (mIsInputTypeNull) {
				mPopupResId = mChangeModeKey.popupResId;
				mChangeModeKey.popupResId = 0;
			}
		}
		return super.changeKeyboard(keyboard);
	}

	/** @see com.hiroshica.android.input.nicownn2.DefaultSoftKeyboard#changeKeyboardType */
	@Override public void changeKeyboardType(int type) {
		commitText();
		Keyboard kbd = getTypeChangeKeyboard(type);
		if (kbd != null) {
			mCurrentKeyboardType = type;
			mPrefEditor.putBoolean("opt_enable_qwerty", type == KEYBOARD_QWERTY);
			mPrefEditor.commit();
			changeKeyboard(kbd);
		}
		if (type == KEYBOARD_12KEY) {
			mWnn.onEvent(mEventChangeMode12Key);
		} else {
			mWnn.onEvent(mEventChangeModeQwerty);
		}
	}
	/** @see com.hiroshica.android.input.nicownn2.DefaultSoftKeyboard#setPreferences */
	@Override public void setPreferences(SharedPreferences pref, EditorInfo editor) {
		mPrefEditor = pref.edit();
		boolean isQwerty = pref.getBoolean("opt_enable_qwerty", false);
		if (isQwerty && (mCurrentKeyboardType != KEYBOARD_QWERTY)) {
			changeKeyboardType(KEYBOARD_QWERTY);
		}

		super.setPreferences(pref, editor);

		int inputType = editor.inputType;
		if (mHardKeyboardHidden) {
			if (inputType == EditorInfo.TYPE_NULL) {
				if (!mIsInputTypeNull) {
					mIsInputTypeNull = true;
					mPopupResId = mChangeModeKey.popupResId;
					mChangeModeKey.popupResId = 0;
				}
				return;
			}
            
			if (mIsInputTypeNull) {
				mIsInputTypeNull = false;
				mChangeModeKey.popupResId = mPopupResId;
			}
		}

		mEnableAutoCaps = pref.getBoolean("auto_caps", true);
		mLimitedKeyMode = null;
		mPreferenceKeyMode = INVALID_KEYMODE;
		mNoInput = true;
		mDisableKeyInput = false;
		mCapsLock = false;

		switch (inputType & EditorInfo.TYPE_MASK_CLASS) {

		case EditorInfo.TYPE_CLASS_NUMBER:
		case EditorInfo.TYPE_CLASS_DATETIME:
			mPreferenceKeyMode = KEYMODE_JA_HALF_NUMBER;
			break;

		case EditorInfo.TYPE_CLASS_PHONE:
			if (mHardKeyboardHidden) {
				//mLimitedKeyMode = new int[] {KEYMODE_JA_FULL_NICO};
				mPreferenceKeyMode = KEYMODE_JA_FULL_NICO;
			} else {
				//mLimitedKeyMode = new int[] {KEYMODE_JA_HALF_ALPHABET};
				mPreferenceKeyMode = KEYMODE_JA_FULL_NICO;
			}
			break;

		case EditorInfo.TYPE_CLASS_TEXT:
			switch (inputType & EditorInfo.TYPE_MASK_VARIATION) {

			case EditorInfo.TYPE_TEXT_VARIATION_PASSWORD:
				//mLimitedKeyMode = new int[] {KEYMODE_JA_HALF_ALPHABET, KEYMODE_JA_HALF_NUMBER};
				break;

			case EditorInfo.TYPE_TEXT_VARIATION_EMAIL_ADDRESS:
				//mLimitedKeyMode = new int[] {KEYMODE_JA_HALF_ALPHABET, KEYMODE_JA_HALF_NUMBER};
				break;
			case EditorInfo.TYPE_TEXT_VARIATION_URI:
				//mPreferenceKeyMode = KEYMODE_JA_HALF_ALPHABET;
				mPreferenceKeyMode = KEYMODE_JA_FULL_NICO;
				break;

			default:
				break;
			}
			break;

		default:
			break;
		}

		boolean restartkey = false;
		boolean old12keyqwerty = mChange12keyQwertyMode;

		mChange12keyQwertyMode = pref.getBoolean("change_12key_qwerty", false);
		boolean getalphamode  = pref.getBoolean("change_alphamode", false);
		mGetNoFlipScreen       = pref.getBoolean("no_flip_screen", false);
		mFlickNicoInput        = flickModeTable.get(pref.getString("nicoflick_mode", "none_flick"));
		//mHiddenSoftKeyboard    = pref.getBoolean("hidden_softkeyboard", false);
		
		if (false == getalphamode) {
			mChangeAlphaBigMode = ALPHAMODE_SMALL;
		}
		else{
			mChangeAlphaBigMode = ALPHAMODE_BIG;
		}
		
		if (true == mGetNoFlipScreen) {
			mNoFlipScreen = true;
		}
		else{
			mNoFlipScreen = false;
		}

		if (old12keyqwerty != mChange12keyQwertyMode) {
			restartkey = true;
			if (false == mChange12keyQwertyMode) {
				mCurrentKeyboardType = mHookStockKeyType; // change last keyboard type.
			}
		}

		if (inputType != mLastInputType) {
			setDefaultKeyboard();
			mLastInputType = inputType;
		}
		else if (true == restartkey) {
			setDefaultKeyboard();
		}

		setStatusIcon();
		setShiftByEditorInfo();
	}

	/** @see com.hiroshica.android.input.nicownn2.DefaultSoftKeyboard#onUpdateState */
	@Override public void onUpdateState(NicoWnn parent) {
		super.onUpdateState(parent);
		if (!mCapsLock) {
			setShiftByEditorInfo();
			if (true == mNoInput && true == mNicoFirst) {
				resetNicoKeyboard();
			}
		}
	}
	/**
	 * Change the keyboard to default
	 */
	@Override public void setDefaultKeyboard() {
		Locale locale = Locale.getDefault();
		int keymode = KEYMODE_JA_FULL_NICO;


		if (mPreferenceKeyMode != INVALID_KEYMODE) {
			keymode = mPreferenceKeyMode;
		} else if (mLimitedKeyMode != null) {
			keymode = mLimitedKeyMode[0];
		}
		changeKeyMode(keymode);
	}

    
	/**
	 * Change to the next input mode
	 */
	@Override public void nextKeyMode() {
		/* Search the current mode in the toggle table */
		boolean found = false;
		int index;
		for (index = 0; index < JP_MODE_CYCLE_TABLE.length; index++) {
			if (JP_MODE_CYCLE_TABLE[index] == mCurrentKeyMode) {
				found = true;
				break;
			}
		}

		if (!found) {
			/* If the current mode not exists, set the default mode */
			setDefaultKeyboard();
		} else {
			/* If the current mode exists, set the next input mode */
			int size = JP_MODE_CYCLE_TABLE.length;
			int keyMode = INVALID_KEYMODE;
			for (int i = 0; i < size; i++) {
				index = (++index) % size;

				keyMode = filterKeyMode(JP_MODE_CYCLE_TABLE[index]);
				if (keyMode != INVALID_KEYMODE) {
					break;
				}
			}

			if (keyMode != INVALID_KEYMODE) {
				changeKeyMode(keyMode);
			}
		}
	}

	/**
	 * Create the keyboard for portrait mode
	 * <br>
	 * @param parent  The context
	 */
	private void createKeyboardsPortrait(NicoWnn parent) {
		Keyboard[][] keyList;
		/* qwerty shift_off (portrait) */
		keyList = mKeyboard[LANG_JA][PORTRAIT][KEYBOARD_QWERTY][KEYBOARD_SHIFT_OFF];
		keyList[KEYMODE_JA_FULL_NICO][0]     = new Keyboard(parent, R.xml.keyboard_qwerty_jp);

		/* qwerty shift_on (portrait) */
		keyList = mKeyboard[LANG_JA][PORTRAIT][KEYBOARD_QWERTY][KEYBOARD_SHIFT_ON];
		keyList[KEYMODE_JA_FULL_NICO][0]     = new Keyboard(parent, R.xml.keyboard_qwerty_jp_shift);

		/* 12-keys shift_off (portrait) */
		keyList = mKeyboard[LANG_JA][PORTRAIT][KEYBOARD_12KEY][KEYBOARD_SHIFT_OFF];

		/* 12-keys shift_on (portrait) */
		keyList = mKeyboard[LANG_JA][PORTRAIT][KEYBOARD_12KEY][KEYBOARD_SHIFT_ON];

		mNicoKeyboard = mSetupKeyboard.SetupSoftKeyboard(parent, PORTRAIT, mInputViewHeightIndex);
		
		Keyboard[][] keyListOn;
		keyList   = mKeyboard[LANG_JA][PORTRAIT][KEYBOARD_12KEY][KEYBOARD_SHIFT_OFF];
		keyListOn = mKeyboard[LANG_JA][PORTRAIT][KEYBOARD_12KEY][KEYBOARD_SHIFT_ON];
		keyList[KEYMODE_JA_FULL_NICO]     = mNicoKeyboard[NICO_SLIDE_MODE_TOP];
		keyListOn[KEYMODE_JA_FULL_NICO]   = mNicoKeyboard[NICO_SLIDE_MODE_A];
	}

	/**
	 * Create the keyboard for landscape mode
	 * <br>
	 * @param parent  The context
	 */
	private void createKeyboardsLandscape(NicoWnn parent) {
		//if ((true == isHardQwertyOnLandscape()) && (true == mHiddenSoftKeyboard)) {
		if (true == isHardQwertyOnLandscape()) {
			return;				// not create soft keyboard
		}

		Keyboard[][] keyList;
		/* qwerty shift_off (landscape) */
		keyList = mKeyboard[LANG_JA][LANDSCAPE][KEYBOARD_QWERTY][KEYBOARD_SHIFT_OFF];
		keyList[KEYMODE_JA_FULL_NICO][0]     = new Keyboard(parent, R.xml.keyboard_qwerty_jp_land);

		/* qwerty shift_on (landscape) */
		keyList = mKeyboard[LANG_JA][LANDSCAPE][KEYBOARD_QWERTY][KEYBOARD_SHIFT_ON];
		keyList[KEYMODE_JA_FULL_NICO][0]     = new Keyboard(parent, R.xml.keyboard_qwerty_jp_shift_land);

		/* 12-keys shift_off (landscape) */
		keyList = mKeyboard[LANG_JA][LANDSCAPE][KEYBOARD_12KEY][KEYBOARD_SHIFT_OFF];

		/* 12-keys shift_on (landscape) */
		keyList = mKeyboard[LANG_JA][LANDSCAPE][KEYBOARD_12KEY][KEYBOARD_SHIFT_ON];

		mNicoKeyboard = mSetupKeyboard.SetupSoftKeyboard(parent, LANDSCAPE, mInputViewHeightIndex);
		
		Keyboard[][] keyListOn;
		keyList   = mKeyboard[LANG_JA][LANDSCAPE][KEYBOARD_12KEY][KEYBOARD_SHIFT_OFF];
		keyListOn = mKeyboard[LANG_JA][LANDSCAPE][KEYBOARD_12KEY][KEYBOARD_SHIFT_ON];
		keyList[KEYMODE_JA_FULL_NICO]     = mNicoKeyboard[NICO_SLIDE_MODE_TOP];
		keyListOn[KEYMODE_JA_FULL_NICO]   = mNicoKeyboard[NICO_SLIDE_MODE_A];
	}

	/**
	 * Convert the key code to the index of table
	 * <br>
	 * @param index     The key code
	 * @return          The index of the toggle table for input
	 */
	@Override public int getTableIndex(int keyCode) {
		int index =
			(keyCode == KEYCODE_JP12_1)     ?  0 :
			(keyCode == KEYCODE_JP12_2)     ?  1 :
			(keyCode == KEYCODE_JP12_3)     ?  2 :
			(keyCode == KEYCODE_JP12_4)     ?  3 :
			(keyCode == KEYCODE_JP12_5)     ?  4 :
			(keyCode == KEYCODE_JP12_6)     ?  5 :
			(keyCode == KEYCODE_JP12_7)     ?  6 :
			(keyCode == KEYCODE_JP12_8)     ?  7 :
			(keyCode == KEYCODE_JP12_9)     ?  8 :
			(keyCode == KEYCODE_JP12_0)     ?  9 :
			(keyCode == KEYCODE_NEWKEY_0)   ? 10 :
			(keyCode == KEYCODE_NEWKEY_1)   ? 11 :
			(keyCode == KEYCODE_NEWKEY_2)   ? 12 :
			(keyCode == KEYCODE_NEWKEY_3)   ? 13 :
			(keyCode == KEYCODE_NEWKEY_4)   ? 14 :
			0;
		return index;
	}

	/**
	 * Get the toggle table for input that is appropriate in current mode.
	 * 
	 * @return      The toggle table for input
	 */
	private String[][] getCycleTable() {
		String[][] cycleTable = cycleTable = mCycleTable[mChangeAlphaBigMode];
		return cycleTable;
	}

	/**
	 * Get the replace table that is appropriate in current mode.
	 * 
	 * @return      The replace table
	 */
	private HashMap<String,String> getReplaceTable() {
		HashMap<String,String> hashTable = null;
		switch (mCurrentKeyMode) {
		case KEYMODE_JA_FULL_NICO:
			hashTable = mReplaceTable;
			break;

		default:
			break;
		}
		return hashTable;
	}
    
	/**
	 * Set the status icon that is appropriate in current mode
	 */
	private void setStatusIcon() {
		int icon = 0;

		switch (mCurrentKeyMode) {
		case KEYMODE_JA_FULL_NICO:
			icon = mSetupKeyboard.SetupIcon();
			break;

		default:
			break;
		}

		mWnn.showStatusIcon(icon);
	}

	/**
	 * Get the shift key state from the editor.
	 * <br>
	 * @param editor    The editor information
	 * @return          The state id of the shift key (0:off, 1:on)
	 */
	protected int getShiftKeyState(EditorInfo editor) {
		InputConnection connection = mWnn.getCurrentInputConnection();
		if (connection != null) {
			int caps = connection.getCursorCapsMode(editor.inputType);
			return (caps == 0) ? 0 : 1;
		} else {
			return 0;
		}
	}

	/**
	 * Set the shift key state from {@link EditorInfo}.
	 */
	private void setShiftByEditorInfo() {
	}

	/** @see com.hiroshica.android.input.nicownn2.DefaultSoftKeyboard#setHardKeyboardHidden */
	@Override public void setHardKeyboardHidden(boolean hidden, boolean qwerty) {
		if (mWnn != null) {
			if (!hidden) {
				mWnn.onEvent(mEventChangeModeQwerty);
			}

			if (mHardKeyboardHidden != hidden) {
				if ((mLimitedKeyMode != null) || ((mCurrentKeyMode != KEYMODE_JA_FULL_NICO))) {
					
					mLastInputType = EditorInfo.TYPE_NULL;
					if (mWnn.isInputViewShown()) {
						setDefaultKeyboard();
					}
				}
			}
		}
		super.setHardKeyboardHidden(hidden, qwerty);
	}

	/**
	 * Change the key-mode to the allowed one which is restricted
	 *  by the text input field or the type of the keyboard.
	 * @param keyMode The key-mode
	 * @return the key-mode allowed
	 */
	private int filterKeyMode(int keyMode) {
		int targetMode = keyMode;
		int[] limits = mLimitedKeyMode;
		/* restrict by the type of the text field */
		if (limits != null) {
			boolean hasAccepted = false;
			boolean hasRequiredChange = true;
			int size = limits.length;
			int nowMode = mCurrentKeyMode;

			for (int i = 0; i < size; i++) {
				if (targetMode == limits[i]) {
					hasAccepted = true;
					break;
				}
				if (nowMode == limits[i]) {
					hasRequiredChange = false;
				}
			}

			if (!hasAccepted) {
				if (hasRequiredChange) {
					targetMode = mLimitedKeyMode[0];
				} else {
					targetMode = INVALID_KEYMODE;
				}
			}
		}

		return targetMode;
	}

	/*****************************************
	 * onkey (normal)
	 */
	/** @see com.hiroshica.android.input.nicownn2.DefaultSoftKeyboard#onKey */
	@Override public void onKey(int primaryCode, int[] keyCodes) {
		Keyboard newKeyboard = null;
		if (mDisableKeyInput) {
			return;
		}
		switch (primaryCode) {
		case KEYCODE_JP12_TOGGLE_MODE:
		case KEYCODE_QWERTY_TOGGLE_MODE:
			if (!mIsInputTypeNull) {
				nextKeyMode();
			}
			break;

		case DefaultSoftKeyboard.KEYCODE_QWERTY_BACKSPACE:
		case KEYCODE_JP12_BACKSPACE:
			if (mNicoFirst == true) {
				resetNicoKeyboard();
			}
			else {
				mWnn.onEvent(mEventInputKeyDel);
			}
			break;

		case DefaultSoftKeyboard.KEYCODE_QWERTY_SHIFT:
			toggleShiftLock();
			break;
            
		case DefaultSoftKeyboard.KEYCODE_QWERTY_ALT:
			processAltKey();
			break;

		case KEYCODE_QWERTY_ENTER:
		case KEYCODE_JP12_ENTER:
			if (mNicoFirst == false) {
				mWnn.onEvent(mEventInputEnter);
			}
			break;

		case KEYCODE_QWERTY_KBD:
		case KEYCODE_JP12_KBD:
			resetNicoKeyboard();
			break;

		case KEYCODE_JP12_EMOJI:
		case KEYCODE_QWERTY_EMOJI:
			if (mNicoFirst == false) {
				commitText();
				mWnn.onEvent(mEventChangeModeSymbol);
			}
			break;

		case KEYCODE_JP12_SHARP:
			if (mNicoFirst == false && mNoInput) {
				commitText();
				mWnn.onEvent(mEventChangeModeDocomo);
			}
			break;

		case KEYCODE_JP12_1:
		case KEYCODE_JP12_2:
		case KEYCODE_JP12_3:
		case KEYCODE_JP12_4:
		case KEYCODE_JP12_5:
		case KEYCODE_JP12_6:
		case KEYCODE_JP12_7:
		case KEYCODE_JP12_8:
		case KEYCODE_JP12_9:
		case KEYCODE_JP12_0:
		case KEYCODE_NEWKEY_0:
		case KEYCODE_NEWKEY_1:
		case KEYCODE_NEWKEY_2:
		case KEYCODE_NEWKEY_3:
		case KEYCODE_NEWKEY_4:
			if ((NICOFLICK_1STROKE == mFlickNicoInput) && (false == mNicoFirst)) {
				break;
			}
			if ((NICOFLICK_NICOSTROKE == mFlickNicoInput) && (true == mNicoFlick) && (mPrevInputKeyCode == primaryCode)) {
				mNicoFlick = false;
				break;
			}
			if (mNicoFirst == false) {
				mWnn.onEvent(mEventTouchOtherKey);
				mPrevInputKeyCode = primaryCode;
				mNicoFirst = true;

				/* change keymap */
				int index = getTableIndex(primaryCode);
				Keyboard[][] keyList = mKeyboard[LANG_JA][mDisplayMode][KEYBOARD_12KEY][KEYBOARD_SHIFT_ON];

				keyList[KEYMODE_JA_FULL_NICO] = mNicoKeyboard[index+1];
				if (false == mNoFlipScreen) {
					newKeyboard = getShiftChangeKeyboard(KEYBOARD_SHIFT_ON);
					mShiftOn = KEYBOARD_SHIFT_ON;
					changeKeyboard(newKeyboard);
				}
			}
			else{
				int col = getTableIndex(mPrevInputKeyCode);
				int row = getTableIndex(primaryCode);
				String[][] cycleTable = getCycleTable();
				mWnn.onEvent(new NicoWnnEvent(NicoWnnEvent.TOGGLE_CHAR, cycleTable[col * 15 + row]));
				mNicoFirst = false;
				mNicoFlick = false;
				if (false == mNoFlipScreen) {
					newKeyboard = getShiftChangeKeyboard(KEYBOARD_SHIFT_OFF);
					mShiftOn = KEYBOARD_SHIFT_OFF;
					changeKeyboard(newKeyboard);
				}
			}
			break;

		case KEYCODE_JP12_ASTER:
			if (mNicoFirst == true) {
				resetNicoKeyboard();
			}
			if (mNicoFirst == false && !mNoInput) {
				/* Processing to toggle Dakuten, Handakuten, and capital */
				HashMap<String,String> replaceTable = getReplaceTable();
				if (replaceTable == null) {
					Log.e("NicoWnn", "not founds replace table");
				} else {
					mWnn.onEvent(new NicoWnnEvent(NicoWnnEvent.REPLACE_CHAR, replaceTable));
					mPrevInputKeyCode = primaryCode;
				}
			}
			break;

		case KEYCODE_JP12_SPACE:
			if (mNicoFirst == false) {
				if ((mCurrentKeyMode == KEYMODE_JA_FULL_HIRAGANA) && !mNoInput) {
					mWnn.onEvent(mEventConvert);
				} else {
					mWnn.onEvent(new NicoWnnEvent(NicoWnnEvent.INPUT_CHAR, ' '));
				}
			}
			break;

		case KEYCODE_SWITCH_FULL_NICO:
			/* Change mode to Full width nicotouch */
			changeKeyMode(KEYMODE_JA_FULL_NICO);
			break;

		case KEYCODE_SELECT_CASE:
			int shifted = (mShiftOn == 0) ? 1 : 0;
			newKeyboard = getShiftChangeKeyboard(shifted);
			if (newKeyboard != null) {
				mShiftOn = shifted;
				changeKeyboard(newKeyboard);
			}
			break;

		case KEYCODE_JP12_CLOSE:
			mWnn.onEvent(mEventInputBack);
			break;
            
		case KEYCODE_JP12_LEFT:
			mWnn.onEvent(mEventInputDpadLeft);
			break;
            
		case KEYCODE_JP12_RIGHT:
			mWnn.onEvent(mEventInputDpadRight);
			break;
		case KEYCODE_NOP:
			break;

		default:
			if (primaryCode >= 0) {
				if (mKeyboardView.isShifted()) {
					primaryCode = Character.toUpperCase(primaryCode);
				}
				mWnn.onEvent(new NicoWnnEvent(NicoWnnEvent.INPUT_CHAR, (char)primaryCode));
			}
			break;
		}

		/* update shift key's state */
		if (!mCapsLock && (primaryCode != DefaultSoftKeyboard.KEYCODE_QWERTY_SHIFT)) {
			setShiftByEditorInfo();
		}
	}

	/*
	 * reset nicotouch keyboard
	 */
	@Override public void resetNicoKeyboard() {
		Keyboard newKeyboard = getShiftChangeKeyboard(KEYBOARD_SHIFT_OFF);
		if (newKeyboard != null) {
			mShiftOn = KEYBOARD_SHIFT_OFF;
			changeKeyboard(newKeyboard);
		}
		mNicoFirst = false;
		mNicoFlick = false;
	} // resetNicoKeyboard

	/**
	 */
	@Override public void onPress(int primaryCode) {
		super.onPress(primaryCode);
	}
	/**
	 */
	@Override public void onRelease(int primaryCode) {
		super.onRelease(primaryCode);
	} // onRelease
	/**
	 */
	@Override public int convertModeFlick(int prev, int key) {
		return mSetupKeyboard.GetFlickKeyCode(prev, key);
	}
}
