package es.verdnatura.presentation.view.component

import android.annotation.SuppressLint
import android.app.Dialog
import android.app.TimePickerDialog
import android.content.Context
import android.view.View
import com.google.android.material.textfield.TextInputEditText
import es.verdnatura.R
import es.verdnatura.databinding.ComponentCustomHourBinding
//import kotlinx.android.synthetic.main.component_custom_hour.*

import java.text.SimpleDateFormat
import java.util.*


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

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

    init {

        setContentView(binding.root)

        //setContentView(R.layout.component_custom_hour)
    }

    fun getDestinoEditText(): TextInputEditText {

        //return custom_dialog_destino_value
        return binding.customDialogDestinoValue
    }

    fun getDestinoValue(): String {
        // return custom_dialog_destino_value.text.toString()
        return binding.customDialogDestinoValue.text.toString()
    }

    fun setDestinoValue(value: String): CustomDialogHour {

        binding.customDialogDestinoValue.setText(value)
        binding.customDialogDestinoValue.visibility = View.VISIBLE
        return this
        /*custom_dialog_destino_value.setText(value)
        custom_dialog_destino_value.visibility = View.VISIBLE
        return this*/
    }


    fun getHoraEditText(): TextInputEditText {
        return binding.customDialogHourValue
        //return custom_dialog_hour_value
    }

    fun getHoraValue(): String {
        return binding.customDialogHourValue.text.toString()
        //return custom_dialog_hour_value.text.toString()
    }

    @SuppressLint("SimpleDateFormat")
    fun setHoraValue(value: String): CustomDialogHour {

        binding.customDialogHourValue.setText(value)
        binding.customDialogHourValue.visibility = View.VISIBLE
        binding.ibObtenerHora.setOnClickListener {

        }
        //custom_dialog_hour_value.setText(value)
        //custom_dialog_hour_value.visibility = View.VISIBLE
        // ib_obtener_hora.setOnClickListener {
        binding.ibObtenerHora.setOnClickListener {
            val cal = Calendar.getInstance()
            val timeSetListener = TimePickerDialog.OnTimeSetListener { _, hour, minute ->
                cal.set(Calendar.HOUR_OF_DAY, hour)
                cal.set(Calendar.MINUTE, minute)
                binding.customDialogHourValue.setText(SimpleDateFormat(context.getString(R.string.timeFormat)).format(cal.time))
                //custom_dialog_hour_value.setText(SimpleDateFormat(getString(R.string.timeFormat)).format(cal.time))
            }
            TimePickerDialog(
                context,
                timeSetListener,
                cal.get(Calendar.HOUR_OF_DAY),
                cal.get(Calendar.MINUTE),
                true
            ).show()
        }
        return this
    }

    fun setTitle(title: String): CustomDialogHour {
        binding.customDialogTitle.visibility = View.VISIBLE
        binding.customDialogTitle.text = title
        // custom_dialog_title.visibility = View.VISIBLE
        //custom_dialog_title.text = title
        return this
    }

    fun setDescription(description: String): CustomDialogHour {
        binding.customDialogDescription.visibility = View.VISIBLE
        binding.customDialogDescription.text = description

        //custom_dialog_description.visibility = View.VISIBLE
        //custom_dialog_description.text = description
        return this
    }

    fun setOkButton(text: String, onButtonClicked: () -> Unit): CustomDialogHour {
        binding.customDialogButtonOk.visibility = View.VISIBLE
        binding.customDialogButtonOk.text = text

        //custom_dialog_button_ok.visibility = View.VISIBLE
        // custom_dialog_button_ok.text = text
        // custom_dialog_button_ok.setOnClickListener { onButtonClicked() }
        binding.customDialogButtonOk.setOnClickListener { onButtonClicked() }
        return this
    }

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

        binding.customDialogButtonKo.visibility = View.VISIBLE
        binding.customDialogButtonKo.text = text
        binding.customDialogButtonKo.setOnClickListener { onButtonClicked() }
        /*custom_dialog_button_ko.visibility = View.VISIBLE
        custom_dialog_button_ko.text = text
        custom_dialog_button_ko.setOnClickListener { onButtonClicked() }*/
        return this
    }

}