package com.module.backstage.adapter

import android.graphics.Color
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.google.gson.Gson
import com.module.backstage.R
import com.quyunshuo.androidbaseframemvvm.base.addressenum.PlcDebugAddressEnum
import com.module.backstage.databinding.BackstageItemDebugParamBinding
import com.module.backstage.databinding.DebugStateItemBinding
import com.module.backstage.listener.DebugClickListener
import com.quyunshuo.androidbaseframemvvm.base.addressenum.PlcDebugStataAddressEnum
import com.quyunshuo.androidbaseframemvvm.base.constant.DebugType
import com.quyunshuo.androidbaseframemvvm.common.util.UiUtil

class DebugStateParamAdapter(var productList: MutableList<PlcDebugStataAddressEnum>) :
    RecyclerView.Adapter<DebugStateParamAdapter.MyViewHolder>() {

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

    private var itemListener: DebugClickListener? = null
    private var paraStateMap: List<String>? = null


    inner class MyViewHolder(binding: DebugStateItemBinding) :
        RecyclerView.ViewHolder(binding.root) {
        val mBinding = binding

        fun bind(plcDebugAddressEnum: PlcDebugStataAddressEnum) {

            mBinding.run {
                if (layoutPosition == 0) {
                    name.text = "名称"
                    name.setTextColor(Color.parseColor("#0000FF"))
                    stataTv.visibility = View.VISIBLE
                    stataIv.visibility = View.GONE
                } else {
                    name.text = UiUtil.getStringRes(plcDebugAddressEnum.aNameId)
                    name.setTextColor(Color.parseColor("#000000"))
                    stataTv.visibility = View.GONE
                    stataIv.visibility = View.VISIBLE
                }
                if ("1".equals(paraStateMap?.get(plcDebugAddressEnum.pit-1))) {
                    stataIv.setBackgroundResource(R.drawable.use_state)
                } else {
                    stataIv.setBackgroundResource(R.drawable.normal_state)
                }
                //cbManual.text = UiUtil.getStringRes(plcDebugAddressEnum.aNameId)
                /* cbManual.setOnClickListener {
                     Log.d("backinfo", "类型：" + plcDebugAddressEnum.pit)
                     if (plcDebugAddressEnum.type == DebugType.OUT_IO) {
                         if ("1".equals(paraStateMap!!.get(plcDebugAddressEnum.pit))) {
                             itemListener?.onClickListener(
                                 false,
                                 layoutPosition,
                                 plcDebugAddressEnum.address
                             )

                         } else {
                             itemListener?.onClickListener(
                                 true,
                                 layoutPosition,
                                 plcDebugAddressEnum.address
                             )

                         }
                     }

                 }
                 if (paraStateMap != null) {
                     if (plcDebugAddressEnum.type == DebugType.OUT_IO) {
                         if ("1".equals(paraStateMap!!.get(plcDebugAddressEnum.pit))) {
                             cbManual.setBackgroundResource(R.drawable.backstage_shape_debug_param_cb_select)
                         } else {
                             cbManual.setBackgroundResource(R.drawable.backstage_shape_debug_param_cb_unselect)
                         }
                     }

                 }*/
            }
        }

    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        val projectVH = MyViewHolder(
            DebugStateItemBinding.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))

    }

    fun setState(paraStateMap: List<String>) {
       // val split = paraStateMap.trim().split("")
        Log.d("backinfo", "DebugStateParamAdaptersplit:" + Gson().toJson(paraStateMap))

        this.paraStateMap = paraStateMap
    }
}