package es.verdnatura.presentation.view.component

import android.app.Dialog
import android.content.Context
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import com.google.android.material.textfield.TextInputEditText
import es.verdnatura.R
import es.verdnatura.databinding.ComponentCustomUbicadorDialogNewBinding

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

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

    init {

        setContentView(binding.root)

    }

    fun setTitle(title: String): CustomDialogUbicadorNew {
        binding.customDialogTitle.visibility = View.VISIBLE
        binding.customDialogTitle.text = title

        return this
    }

    fun setItemValue(value: String): CustomDialogUbicadorNew {
        binding.customDialogItem.setText(value)
        binding.customDialogItem.visibility = View.VISIBLE

        return this
    }

    fun getItemValue(): String {

        return binding.customDialogItem.text.toString()

    }

    fun getEditItem(): TextInputEditText {

        return binding.customDialogItem
    }

    fun setEtiquetaValue(value: String?): CustomDialogUbicadorNew {

        binding.customDialogEtiquetas.setText(value ?: "")
        binding.customDialogEtiquetas.visibility = View.VISIBLE
        return this
    }

    fun setUnits(title: String): CustomDialogUbicadorNew {
        binding.customDialogUnits.visibility = View.VISIBLE
        binding.customDialogUnits.setText(title)

        return this
    }

    fun getUnitsValue(): String {
        return binding.customDialogUnits.text.toString()
    }

    fun getUnitsItem(): TextInputEditText {
        return binding.customDialogUnits
    }

    fun getEtiquetaValue(): String {
        return binding.customDialogEtiquetas.text.toString()
    }

    fun getEtiquetaItem(): TextInputEditText {

        return binding.customDialogEtiquetas
    }
    fun setPackingValue(value: String): CustomDialogUbicadorNew {

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

        return this
    }

    fun getPackingValue(): String {

        return binding.customDialogPacking.text.toString()
    }

    fun getPackingItem(): TextInputEditText {
        return binding.customDialogPacking
    }

    fun setVisibleValue(value: String): CustomDialogUbicadorNew {

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

        return this
    }

    fun getVisibleValue(): String {

        return binding.customDialogVisible.text.toString()

    }

    fun getVisibleItem(): TextInputEditText {
        return binding.customDialogVisible

    }

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

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

        return this
    }

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

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

        return this
    }
    fun addTextWatcher(editText: TextInputEditText, listener: (CharSequence?) -> Unit) {
        editText.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
            }

            override fun afterTextChanged(s: Editable?) {
                listener(s)
            }
        })
    }
}