package com.module.backstage.adapter

import android.util.Log
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.module.backstage.R
import com.module.backstage.databinding.BackstageItemInputBinding
import com.module.backstage.databinding.BackstageItemSystemSettingsBinding
import com.quyunshuo.androidbaseframemvvm.common.listener.AdapterClickListener
import com.module.pay.common.OtherEnum
import com.quyunshuo.androidbaseframemvvm.base.utils.SpUtils
import com.quyunshuo.androidbaseframemvvm.common.util.ToastUtil
import com.quyunshuo.androidbaseframemvvm.common.util.UiUtil

class OtherAdapter(var productList: MutableList<OtherEnum>) :
    RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    private val TAG = "HomeProductAdapter"

    var SWITCH_TYPE = 1
    var INPUT_TYPE = 2


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

    private var itemListener: AdapterClickListener? = null


    private var generalParamData: MutableList<String>? = null

    inner class MyViewHolder(binding: BackstageItemSystemSettingsBinding) :
        ViewHolder(binding.root) {
        private val mBinding = binding
        fun bind(otherEnum: OtherEnum) {
            mBinding.run {

                Log.d(TAG, "MyViewHolder bind: " + otherEnum)

                tvName.text = UiUtil.getStringRes(productList[layoutPosition].nameId)
                rgSwitch.setOnCheckedChangeListener { group, checkedId ->
                    when (checkedId) {
                        R.id.rb_close -> {
                            SpUtils.putBoolean(otherEnum.mmkvName, false)
                        }

                        R.id.rb_open -> {
                            SpUtils.putBoolean(otherEnum.mmkvName, true)
                        }

                        else -> {}
                    }
                }
                var flag = SpUtils.getBoolean(otherEnum.mmkvName, otherEnum.drfault as Boolean)
                if (flag == true) {
                    rbOpen.isChecked = true
                } else {
                    rbClose.isChecked = true
                }


//                btnUpdate.setOnClickListener {
//                    itemListener?.onClickListener(it,layoutPosition,etValue.text.trim().toString())
//                }
//                val value = generalParamData?.get(layoutPosition)
//                etValue.setText(value)
            }
        }
    }


    inner class InputViewHolder(binding: BackstageItemInputBinding) : ViewHolder(binding.root) {
        private val mBinding = binding
        fun bind(otherEnum: OtherEnum) {
            mBinding.run {

                Log.d(
                    TAG,
                    "InputViewHolder bind: " + otherEnum.mmkvName + ":" + SpUtils.contains(otherEnum.mmkvName)
                )

                tvName.text = UiUtil.getStringRes(productList[layoutPosition].nameId)

                var defaultValue =
                    SpUtils.getString(otherEnum.mmkvName, otherEnum.drfault as String)
                etValue.setText(defaultValue)
                btnUpdate.setOnClickListener {
                    val value = etValue.text.trim().toString()
                    if (value == "") {
                        ToastUtil.switchToastStyleToWarn("输入为空")
                        return@setOnClickListener
                    }
                    SpUtils.putString(otherEnum.mmkvName, value)
                    ToastUtil.switchToastStyleToSuccess("更新成功:" + otherEnum.mmkvName)
                }
//                btnUpdate.setOnClickListener {
//                    itemListener?.onClickListener(it,layoutPosition,etValue.text.trim().toString())
//                }
//                val value = generalParamData?.get(layoutPosition)
//                etValue.setText(value)
            }
        }
    }

    override fun getItemViewType(position: Int): Int {
        Log.d(TAG, "getItemId: " + productList[position].type)
        return productList[position].type
    }


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        Log.d(TAG, "onCreateViewHolder: " + viewType)
        when (viewType) {
            SWITCH_TYPE -> {
                Log.d(TAG, "onCreateViewHolder: MyViewHolder:" + viewType)
                return MyViewHolder(
                    BackstageItemSystemSettingsBinding.inflate(
                        LayoutInflater.from(parent.context),
                        parent,
                        false
                    )
                )
            }

            INPUT_TYPE -> {
                Log.d(TAG, "onCreateViewHolder: InputViewHolder:" + viewType)

                return InputViewHolder(
                    BackstageItemInputBinding.inflate(
                        LayoutInflater.from(parent.context),
                        parent,
                        false
                    )
                )
            }

            else -> {
            }
        }
        return MyViewHolder(
            BackstageItemSystemSettingsBinding.inflate(
                LayoutInflater.from(parent.context),
                parent,
                false
            )
        )
    }

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

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if (holder is MyViewHolder) {
            holder.bind(productList.get(position))

        } else if (holder is InputViewHolder) {
            holder.bind(productList.get(position))
        }

        itemListener?.onClickListener(holder.itemView, position, null)


    }

    fun setData(productList: MutableList<OtherEnum>) {
        this.productList = productList
    }
}