/*
 * The MIT License
 *
 * Copyright 2012 Masahiko, SAWAI <masahiko.sawai@gmail.com>.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.routine_work.notepad.common;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import org.routine_work.notepad.R;
import org.routine_work.notepad.prefs.NotepadPreferenceUtils;
import org.routine_work.utils.Log;

/**
 *
 * @author Masahiko, SAWAI <masahiko.sawai@gmail.com>
 */
public class EditTextActivity extends Activity
	implements View.OnClickListener
{

	public static final String EXTRA_INPUT_TYPE = EditTextActivity.class.getPackage().getName() + ".INPUT_TYPE";
	private static final String LOG_TAG = "simple-notepad";
	private EditText mainEditText;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		Log.v(LOG_TAG, "Hello");
		setTheme(NotepadPreferenceUtils.getTheme(this));
		super.onCreate(savedInstanceState);

		getWindow().setSoftInputMode(
			WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE
			| WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
		setContentView(R.layout.edit_text_activity);

		EditText singleLineEditText = (EditText) findViewById(R.id.singleLine_edittext);
		EditText multiLineEditText = (EditText) findViewById(R.id.multiLine_edittext);
		ImageButton cancelImageButton = (ImageButton) findViewById(R.id.cancel_button);
		ImageButton okImageButton = (ImageButton) findViewById(R.id.ok_button);

		cancelImageButton.setOnClickListener(this);
		okImageButton.setOnClickListener(this);

		Intent intent = getIntent();

		int defaultInputType = singleLineEditText.getInputType();
		int inputType = intent.getIntExtra(EXTRA_INPUT_TYPE, defaultInputType);
		Log.v(LOG_TAG, "EXTRA_INPUT_TYPE => " + inputType);
		if (((inputType & InputType.TYPE_TEXT_FLAG_MULTI_LINE) != 0)
			|| ((inputType & InputType.TYPE_TEXT_FLAG_IME_MULTI_LINE) != 0))
		{
			mainEditText = multiLineEditText;
			singleLineEditText.setVisibility(View.GONE);
			multiLineEditText.setVisibility(View.VISIBLE);
		}
		else
		{
			mainEditText = singleLineEditText;
			singleLineEditText.setVisibility(View.VISIBLE);
			multiLineEditText.setVisibility(View.GONE);
		}

		if (inputType != mainEditText.getInputType())
		{
			mainEditText.setInputType(inputType);
		}

		String title = intent.getStringExtra(Intent.EXTRA_TITLE);
		Log.v(LOG_TAG, "EXTRA_TITLE => " + title);
		if (!TextUtils.isEmpty(title))
		{
			TextView titleTextView = (TextView) findViewById(R.id.title_textview);
			titleTextView.setText(title);
			mainEditText.setHint(title);
		}

		String text = intent.getStringExtra(Intent.EXTRA_TEXT);
		Log.v(LOG_TAG, "EXTRA_TEXT => " + text);
		if (!TextUtils.isEmpty(text))
		{
			mainEditText.setText(text);
		}

		mainEditText.requestFocus();

		Log.v(LOG_TAG, "Bye");
	}

	public void onClick(View v)
	{
		switch (v.getId())
		{
			case R.id.ok_button:
				Intent okData = new Intent();
				okData.putExtra(Intent.EXTRA_TEXT, mainEditText.getText().toString());
				setResult(RESULT_OK, okData);
				finish();
				break;
			case R.id.cancel_button:
				setResult(RESULT_CANCELED);
				finish();
				break;
		}
	}
}
