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 androidx.core.widget.addTextChangedListener
import kotlinx.android.synthetic.main.bottom_sheet_edit_text_multi_lines.*
import kotlinx.android.synthetic.main.fragment_bottom_sheet.view.*
import net.pokeranalytics.android.exceptions.RowRepresentableEditDescriptorException


class BottomSheetEditTextMultiLinesFragment : BottomSheetFragment() {

	private var value: String? = null

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

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

	override fun getValue(): Any? {
		this.value?.let {
			return it.trim()
		}
		return null
	}

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

	/**
	 * Init UI
	 */
	private fun initUI() {
		val data = getData()?:throw RowRepresentableEditDescriptorException("RowRepresentableEditDescriptor not found")
		if (data.size != 1) {
			throw RowRepresentableEditDescriptorException("RowRepresentableEditDescriptor inconsistency")
		}

		LayoutInflater.from(requireContext()).inflate(net.pokeranalytics.android.R.layout.bottom_sheet_edit_text_multi_lines, view?.bottomSheetContainer, true)

		data[0].hint?.let { editText1.hint = getString(it) }
		editText1.inputType = data[0].inputType  ?: InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_FLAG_MULTI_LINE or InputType.TYPE_TEXT_FLAG_CAP_SENTENCES
		editText1.addTextChangedListener { this.value = it?.toString() }
		data[0].defaultValue?.let {
			editText1.setText(it.toString())
		}
	}

}