package es.verdnatura.presentation.view.component

import android.annotation.SuppressLint
import android.app.Dialog
import android.content.Context
import android.content.res.TypedArray
import android.view.View
import android.widget.Button
import android.widget.LinearLayout
import androidx.appcompat.view.ContextThemeWrapper
import es.verdnatura.R
import es.verdnatura.databinding.ComponentCustomDynamicDialogBinding
import es.verdnatura.presentation.common.OnClickDynamic
import es.verdnatura.presentation.common.setMargins
import es.verdnatura.presentation.view.feature.articulo.model.AddressLoses

class CustomDialogDynamicButtons(context: Context, private val onClickDynamic: OnClickDynamic) : Dialog(context, R.style.DialogTheme) {

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


    init {
        setContentView(binding.root)

    }

    fun setTitle(title: String): CustomDialogDynamicButtons {

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

    fun setDescription(description: String): CustomDialogDynamicButtons {

        binding.customDialogDescription.visibility = View.VISIBLE
        binding.customDialogDescription.text = description
        return this
    }


    fun createDynamicButtons(buttonInfoList: List<AddressLoses>) {
        for (buttonInfo in buttonInfoList) {
            setDynamicButton(buttonInfo.nickname,buttonInfo.id)
        }
    }


    @SuppressLint("ResourceType")
    private fun setDynamicButton(buttonText: String, addressFk: Int) {

        val button = Button(ContextThemeWrapper(context, R.style.DefaultButton_DynamicButton))
        button.text = buttonText

        val layoutParams = LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT, // anchura
            context.resources.getDimensionPixelSize(R.dimen.layout_height_dynamic) // altura
        )
        button.layoutParams = layoutParams

        val paddingStart =
            context.resources.getDimensionPixelSize(R.dimen.padding_start_dynamic)
        val paddingEnd =
            context.resources.getDimensionPixelSize(R.dimen.padding_end_dynamic)
        val marginTop = context.resources.getDimensionPixelSize(R.dimen.margin_top_dynamic)
        val marginBottom =
            context.resources.getDimensionPixelSize(R.dimen.margin_bottom_dynamic)
        button.setPaddingRelative(paddingStart, 0, paddingEnd, 0)
        button.setMargins(0, marginTop, 0, marginBottom)
        button.text

        val attrs = intArrayOf(android.R.attr.textColor, android.R.attr.background)
        val typedArray: TypedArray =
            context.obtainStyledAttributes(R.style.DefaultButton_DynamicButton, attrs)
        button.setTextColor(typedArray.getColor(0, 0))
        button.setBackgroundResource(typedArray.getResourceId(1, 0))

        typedArray.recycle()
        button.setOnClickListener {
            onClickDynamic.onClickDynamic(addressFk)
        }
        val buttonContainer = binding.layoutButtons
        buttonContainer.addView(button)


    }


}