package com.module.backstage.adapter

import android.util.Log
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.module.backstage.R
import com.module.backstage.databinding.BackstageItemCloseParamBinding
import com.module.backstage.databinding.BackstageItemGeneralParamBinding
import com.quyunshuo.androidbaseframemvvm.base.addressenum.PlcCloseAddressEnum
import com.quyunshuo.androidbaseframemvvm.base.addressenum.PlcParamAddressEnum
import com.quyunshuo.androidbaseframemvvm.base.constant.DebugType
import com.quyunshuo.androidbaseframemvvm.common.listener.AdapterClickListener
import com.quyunshuo.androidbaseframemvvm.common.util.UiUtil

class CloseReParamAdapter(var productList: MutableList<PlcCloseAddressEnum>) :
    RecyclerView.Adapter<CloseReParamAdapter.MyViewHolder>() {

    private val TAG = "HomeProductAdapter"

    fun setItemListener(itemListener: AdapterClickListener?) {
        this.itemListener = itemListener
    }

    private var itemListener: AdapterClickListener? = null


    private var generalParamData: List<Int>? = null

    inner class MyViewHolder(binding: BackstageItemCloseParamBinding) :
        RecyclerView.ViewHolder(binding.root) {
        private val mBinding = binding
        fun bind(plcParamAddressEnum: PlcCloseAddressEnum) {
            mBinding.run {
                cbManual.text = UiUtil.getStringRes(productList[layoutPosition].aNameId)
                if (generalParamData != null) {
                    if (generalParamData!!.get(plcParamAddressEnum.pit) == 1) {
                        cbManual.setBackgroundResource(R.drawable.backstage_shape_debug_param_cb_select)
                    } else {
                        cbManual.setBackgroundResource(R.drawable.backstage_shape_debug_param_cb_unselect)
                    }
                }
                cbManual.setOnClickListener {
                    if (generalParamData != null) {
                        if (generalParamData!!.get(plcParamAddressEnum.pit) == 1) {
                            itemListener?.onClickListener(
                                cbManual,
                                plcParamAddressEnum.pit,
                                plcParamAddressEnum.address + plcParamAddressEnum.pit.toStrictTwoDigitHex() + "000000"
                            )
                        } else {
                            itemListener?.onClickListener(
                                cbManual,
                                plcParamAddressEnum.pit,
                                plcParamAddressEnum.address + plcParamAddressEnum.pit.toStrictTwoDigitHex() + "000001"
                            )

                        }
                    }

                }

                /*  btnUpdate.setOnClickListener {
                      var data = "";
                      if (etValue.text.trim().toString().isNullOrBlank()) {
                          data = productList[layoutPosition].address + "0000" + intToTwoDigitString(
                              layoutPosition + 1
                          ) + "0000"
                      }else{
                          data = productList[layoutPosition].address + intTo4DigitHex(Integer.parseInt(etValue.text.trim().toString())) + intToTwoDigitString(
                              layoutPosition + 1
                          ) + "0000"
                      }
                      Log.d("backinfo","要更新的数据："+data)
                      itemListener?.onClickListener(
                          it,
                          layoutPosition,
                          data
                      )
                  }
                  if (generalParamData != null) {
                      val value = generalParamData?.get(layoutPosition)
                      etValue.setText("" + value)
                  }
  */
            }
        }
    }

    fun Int.toStrictTwoDigitHex(): String {
        require(this in 0..255) { "Value must be in 0-255 range" }
        return String.format("%02X", this)
    }

    fun intToTwoDigitString(number: Int): String {
        // 使用 "%02d" 格式化为两位，不足补零
        return String.format("%02d", number)
    }

    fun intTo4DigitHex(number: Int): String {
        // 1. 取低16位（兼容负数补码，并限制在 0x0000 ~ 0xFFFF 范围内）
        val masked = number and 0xFFFF
        // 2. 格式化为4位十六进制，自动补零
        return String.format("%04X", masked)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        val projectVH = MyViewHolder(
            BackstageItemCloseParamBinding.inflate(
                LayoutInflater.from(parent.context),
                parent,
                false
            )
        )
        return projectVH
    }

    override fun getItemCount(): Int {
        return productList.size
    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        holder.bind(productList.get(position))
        itemListener?.onClickListener(holder.itemView, position, null)


    }

    fun setData(generalParamData: List<Int>) {
        this.generalParamData = generalParamData
        notifyDataSetChanged()
    }
}