package com.module.backstage.adapter

import android.util.Log
import android.view.LayoutInflater
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.listener.DebugClickListener
import com.quyunshuo.androidbaseframemvvm.base.constant.DebugType
import com.quyunshuo.androidbaseframemvvm.common.util.UiUtil

class DebugParamAdapter(var productList: MutableList<PlcDebugAddressEnum>) :
    RecyclerView.Adapter<DebugParamAdapter.MyViewHolder>() {

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

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


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

        fun bind(plcDebugAddressEnum: PlcDebugAddressEnum) {

            mBinding.run {
                cbManual.text = UiUtil.getStringRes(plcDebugAddressEnum.aNameId)
                clickItem.setOnClickListener {
                  //  if (plcDebugAddressEnum.type == DebugType.OUT_IO) {
                        itemListener?.onClickListener(
                            plcDebugAddressEnum.type,
                            plcDebugAddressEnum.pit,
                            !cbManual.isChecked,
                            layoutPosition,
                            plcDebugAddressEnum.address
                        )

                }

                if (paraStateMap != null) {
                    if (plcDebugAddressEnum.type == DebugType.OUT_IO) {
                        if ("1".equals(paraStateMap!!.get(plcDebugAddressEnum.pit))) {
                            cbManual.isChecked=true
                        } else {
                            cbManual.isChecked=false

                        }
                    } else if (plcDebugAddressEnum.type == DebugType.PWM1_IO) {
                        var type =
                            Integer.parseInt(paraStateMap!!.get(plcDebugAddressEnum.pit - 1), 16);
                        if (type > 0 && type < 40) {
                            cbManual.isChecked=true

                        } else {
                            cbManual.isChecked=false

                        }
                    } else if (plcDebugAddressEnum.type == DebugType.PWM2_IO) {
                        var type =
                            Integer.parseInt(paraStateMap!!.get(plcDebugAddressEnum.pit - 1), 16);
                        if (type >= 40 && type <= 80) {
                            cbManual.isChecked=true

                        } else {
                            cbManual.isChecked=false

                        }
                    } else if (plcDebugAddressEnum.type == DebugType.PWM3_IO) {
                        var type =
                            Integer.parseInt(paraStateMap!!.get(plcDebugAddressEnum.pit - 1), 16);
                        if (type > 80 && type < 101) {
                            cbManual.isChecked=true

                        } else {
                            cbManual.isChecked=false

                        }
                    }else if (plcDebugAddressEnum.type == DebugType.PWMCLEAN){
                        if ("1".equals(paraStateMap!!.get(plcDebugAddressEnum.pit - 1))){
                            cbManual.isChecked=true
                        }else{
                            cbManual.isChecked=false
                        }
                    }

                }
            }
        }

    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        val projectVH = MyViewHolder(
            BackstageItemDebugParamBinding.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: MutableList<String>) {
        // val split = paraStateMap.trim().split("")
        Log.d("backinfo", "DebugParamAdaptersplit:" + Gson().toJson(paraStateMap))
        this.paraStateMap = paraStateMap
    }
    fun getState(): MutableList<String>?{
        return paraStateMap
    }
}