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_sum.*
import kotlinx.android.synthetic.main.fragment_bottom_sheet.view.*
import net.pokeranalytics.android.R
import net.pokeranalytics.android.exceptions.RowRepresentableEditDescriptorException
import net.pokeranalytics.android.util.extensions.round
import net.pokeranalytics.android.util.extensions.toCurrency
import java.text.NumberFormat


class BottomSheetSumFragment : BottomSheetFragment() {

	private var value = 0.0
	private var currentDefaultValue = 0.0

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

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

	override fun getValue(): Any? {
		val editText2Value = editText2.text ?: ""
		if (editText2Value.isEmpty()) {
			return currentDefaultValue
		}
		return value
	}

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

	/**
	 * Init UI
	 */
	private fun initUI() {
		val data = getData()?:throw RowRepresentableEditDescriptorException("RowRepresentableEditDescriptor not found")
		if (data.size != 5) {
			throw RowRepresentableEditDescriptorException("RowRepresentableEditDescriptor inconsistency")
		}
		LayoutInflater.from(requireContext()).inflate(R.layout.bottom_sheet_sum, view?.bottomSheetContainer, true)

		if (data.size == 5) {

			// Default value for the sum
			currentDefaultValue = try {
				data[2].defaultValue as Double
			} catch (e: Exception) {
				0.0
			}

			val formatter = NumberFormat.getNumberInstance()
			formatter.maximumFractionDigits = 6
			formatter.isGroupingUsed = false
			currentValue.text = formatter.format(currentDefaultValue)

			// First value
			val defaultValue1 = try {
				data[0].defaultValue as Double
			} catch (e: Exception) {
				0.0
			}

			button1.text = "+ ${defaultValue1.toCurrency(currentCurrency)}"
			button1.visibility = if (defaultValue1 > 0) View.VISIBLE else View.GONE
			button1.setOnClickListener {
				this.delegate.onRowValueChanged(currentDefaultValue + defaultValue1, row)
				dismiss()
			}

			// Second value
			val defaultValue2 = try {
				data[1].defaultValue as Double
			} catch (e: Exception) {
				0.0
			}

			button2.text = "+ ${defaultValue2.toCurrency(currentCurrency)}"
			button2.visibility = if (defaultValue2 > 0) View.VISIBLE else View.GONE
			button2.setOnClickListener {
				this.delegate.onRowValueChanged(currentDefaultValue + defaultValue2, row)
				dismiss()
			}

			// First edit text
			data[3].hint?.let { editText1.hint = getString(it) }
			editText1.inputType = data[3].inputType ?: InputType.TYPE_CLASS_TEXT
			editText1.addTextChangedListener {
				val valueToAdd = try {
					editText1.text.toString().toDouble()
				} catch (e: Exception) {
					0.0
				}
				editText2.setText((currentDefaultValue + valueToAdd).round())

			}

			// Second edit text
			data[4].hint?.let { editText2.hint = getString(it) }
			editText2.inputType = data[4].inputType ?: InputType.TYPE_CLASS_TEXT
			editText2.addTextChangedListener {
				value = try {
					it.toString().toDouble()
				} catch (e: Exception) {
					0.0
				}
			}

			editText2.setOnEditorActionListener { _, actionId, _ ->
				if (actionId == EditorInfo.IME_ACTION_DONE) {
					this.delegate.onRowValueChanged(value, row)
					dismiss()
					true
				} else {
					false
				}
			}
		}

	}

}