package net.pokeranalytics.android.ui.fragment.components.bottomsheet

import android.os.Bundle
import android.text.InputType
import android.view.LayoutInflater
import android.view.View
import android.view.inputmethod.EditorInfo
import androidx.core.widget.addTextChangedListener
import kotlinx.android.synthetic.main.bottom_sheet_double_edit_text.*
import kotlinx.android.synthetic.main.fragment_bottom_sheet.view.*
import net.pokeranalytics.android.R


class BottomSheetDoubleEditTextFragment : BottomSheetFragment() {

	private val values = ArrayList<String>()

	override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
		super.onViewCreated(view, savedInstanceState)
		initData()
		initUI()
	}

	override fun onStart() {
		super.onStart()
		editText1.requestFocus()
	}

	override fun getValue(): Any? {
		return values
	}

	/**
	 * Init data
	 */
	private fun initData() {
	}

	/**
	 * Init UI
	 */
	private fun initUI() {

		val data = getData()

		values.add(0, "")
		values.add(1, "")

		LayoutInflater.from(requireContext())
			.inflate(R.layout.bottom_sheet_double_edit_text, view?.bottomSheetContainer, true)

		if (data.size == 2) {

			data[0].hint?.let { editText1.hint = getString(it) }
			editText1.inputType = data[0].inputType ?: InputType.TYPE_CLASS_TEXT
			editText1.addTextChangedListener { values[0] = it?.toString() ?: "" }
			editText1.setText((data[0].defaultValue ?: "").toString())

			data[1].hint?.let { editText2.hint = getString(it) }
			editText2.inputType = data[1].inputType ?: InputType.TYPE_CLASS_TEXT
			editText2.addTextChangedListener { values[1] = it?.toString() ?: "" }
			editText2.setText((data[1].defaultValue ?: "").toString())

			editText2.setOnEditorActionListener { v, actionId, event ->
				if (actionId == EditorInfo.IME_ACTION_DONE) {
					bottomSheetDelegate.setValue(values, row)
					dismiss()
					true
				} else {
					false
				}
			}
		}

	}

}