package es.verdnatura.presentation.view.component

import android.app.Dialog
import android.content.Context
import android.text.InputType
import android.view.View
import com.google.android.material.textfield.TextInputEditText
import es.verdnatura.R
import es.verdnatura.databinding.ComponentCustomEditDialogBinding

class CustomDialogInput(context: Context) : Dialog(context, R.style.DialogTheme) {

    private var binding: ComponentCustomEditDialogBinding =
        ComponentCustomEditDialogBinding.inflate(layoutInflater)

    init {

        setContentView(binding.root)
        binding.btOne.setOnClickListener { view -> plusTextButton(view) }
        binding.btTwo.setOnClickListener { view -> plusTextButton(view) }
        binding.btThree.setOnClickListener { view -> plusTextButton(view) }
        binding.btFour.setOnClickListener { view -> plusTextButton(view) }
        binding.btFive.setOnClickListener { view -> plusTextButton(view) }

    }

    fun getEditText(): TextInputEditText {

        return binding.customDialogValue
    }

    fun getEditTextTwo(): TextInputEditText {

        return binding.customDialogValueTwo
    }

    fun getValue(): String {

        return binding.customDialogValue.text.toString()

    }

    fun getValueTwo(): String {
        return binding.customDialogValueTwo.text.toString()
    }

    fun setValue(value: String): CustomDialogInput {

        binding.customDialogValue.setText(value)
        binding.customDialogValue.visibility = View.VISIBLE
        return this
    }

    fun setValueTwo(value: String): CustomDialogInput {

        binding.customDialogValueTwo.setText(value)
        binding.customDialogValueTwo.visibility = View.VISIBLE

        return this
    }

    fun setTitle(title: String): CustomDialogInput {

        binding.customDialogTitle.visibility = View.VISIBLE
        binding.customDialogTitle.text = title
        return this
    }

    fun setDescription(description: String): CustomDialogInput {

        binding.customDialogDescription.visibility = View.VISIBLE
        binding.customDialogDescription.text = description

        return this
    }

    fun setOkButton(text: String, onButtonClicked: () -> Unit): CustomDialogInput {

        binding.customDialogButtonOk.visibility = View.VISIBLE
        binding.customDialogButtonOk.text = text
        binding.customDialogButtonOk.setOnClickListener { onButtonClicked() }

        return this
    }

    fun setKoButton(text: String, onButtonClicked: () -> Unit): CustomDialogInput {

        binding.customDialogButtonKo.visibility = View.VISIBLE
        binding.customDialogButtonKo.text = text
        binding.customDialogButtonKo.setOnClickListener { onButtonClicked() }


        return this
    }

    fun setInputText(typeClassNumber: Int = InputType.TYPE_CLASS_NUMBER) {
        binding.customDialogValue.inputType = typeClassNumber
    }

    fun setFocusText() {
        binding.customDialogValue.requestFocus()
    }
    fun setFocusTextTwo() {
        binding.customDialogValueTwo.requestFocus()
    }

    fun plusTextButton(view: View) {

        var sum = 0
        try {
            sum = getValue().toInt() + Integer.parseInt(view.tag.toString())
            setValue(sum.toString())
        } catch (ex: Exception) {
            setValue(view.tag.toString())
        }

    }

    fun setVisibilityPlusButtons(visibility: Int) {
        binding.layoutButtons.visibility = visibility
    }
}