package com.zj365.health.adapter

import android.util.Log
import android.view.ViewGroup
import android.widget.RadioButton
import android.widget.RadioGroup
import androidx.core.content.ContextCompat
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.xty.base.adapter.BaseAdapter
import com.zj365.health.R
import com.xty.network.model.SelectedAnswerBean
import com.xty.network.model.TongueQuestionBean

class TongueQuestAdapter(): BaseAdapter<TongueQuestionBean>(R.layout.item_quest_tongue)  {

    private val selectedAnswers: MutableMap<Int, SelectedAnswerBean> = mutableMapOf()
    var isShowAnswer = false

    fun getSelectedAnswers() = selectedAnswers

    fun setSelectedAnswer(answers :List<SelectedAnswerBean>){
        selectedAnswers.clear() // 清空现有的 selectedAnswers 映射
        isShowAnswer = true
        for (answer in answers) {
            selectedAnswers[answer.questionIndex] = answer
        }
        Log.e("mTag",selectedAnswers.toString())
    }

    override fun convert(holder: BaseViewHolder, item: TongueQuestionBean) {
        holder.setText(R.id.mNum,"${holder.layoutPosition+1}、")
            .setText(R.id.nQuestName,item.questionContent)
//                var beanArray: List<QuestBean.Answer> =Gson().fromJson(replace, object :  TypeToken<List<QuestBean.Answer>>(){}.type)
        holder.getView<RadioGroup>(R.id.mRadio).clearCheck()
        holder.getView<RadioGroup>(R.id.mRadio).removeAllViews()
        item.answerOptionsArray.forEachIndexed { i, answer ->
            var  tempButton =   RadioButton(context);//this就是你的activity
            var  lp =  RadioGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            lp.setMargins(12, 5, 0, 5);// 设置RadioButton边距 (int left, int top, int right, int bottom)
            // tempButton.setBackgroundResource(R.drawable.shape_corners_delivered_14dp_bg);    // 设置RadioButton的背景图片
            tempButton.setPadding(12, 5, 12, 5);                // 设置文字距离按钮四周的距离
            tempButton.setTextColor(ContextCompat.getColor(context,R.color.col_455));//字体颜色
            tempButton.buttonDrawable = ContextCompat.getDrawable(context,R.drawable.selector_check_medical);//取消前面的圆点
            tempButton.text = answer
            tempButton.id=i
            tempButton.tag=answer
            tempButton.isChecked=false
            if (isShowAnswer){
                tempButton.isEnabled = false
                if (selectedAnswers[holder.layoutPosition + 1]?.answerCode.equals(tempButton.tag as String)){
                    tempButton.isChecked=true
                }
            }
            holder.getView<RadioGroup>(R.id.mRadio).addView(tempButton, lp);
        }
        holder.getView<RadioGroup>(R.id.mRadio).setOnCheckedChangeListener { group, checkedId ->
            val selectedButton = group.findViewById<RadioButton>(checkedId)
            val selectedAnswerTag = selectedButton?.tag as String?

            // 保存用户选择的答案
            selectedAnswers[holder.layoutPosition] = SelectedAnswerBean(holder.layoutPosition+1, selectedAnswerTag ?: "")
        }


    }

}