package com.zj365.health.adapter

import android.text.Html
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.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.xty.base.adapter.BaseAdapter
import com.zj365.health.R
import com.xty.network.model.QuestBean
import com.xty.network.model.QuestDetailBean

class QuestDetailAdapter:BaseAdapter<QuestDetailBean.Answers>(R.layout.item_quest_detail) {
    val selPosition by lazy{ mutableMapOf("A" to 0,"B" to 1,"C" to 2,"D" to 3,"E" to 4,"F" to 5,"G" to 6)}
    override fun convert(holder: BaseViewHolder, item: QuestDetailBean.Answers) {
        holder.setText(R.id.mNum,"${holder.layoutPosition}")
            .setText(R.id.nQuestName,item.question)
            .setText(R.id.mContent,if(item.analysis.isEmpty()) "无" else Html.fromHtml(item.analysis))
        val replace = item.desc_answer.replace("\\", "")
        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).isEnabled=false
        holder.getView<RadioGroup>(R.id.mRadio).removeAllViews()
        if(beanArray[0].label.isEmpty()){
            val split = item.optiont.split(",")
            holder.setText(R.id.mText,"身高${split[0]}，体重${split[1]}")
            holder.setGone(R.id.mText,false).setGone(R.id.mRadio,true)
        }else{
            holder.setGone(R.id.mText,true).setGone(R.id.mRadio,false)
            beanArray.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 = null;//取消前面的圆点
                tempButton.background=ContextCompat.getDrawable(context,R.drawable.selector_detail)
                tempButton.text = "${answer.value} ${answer.label}"
                tempButton.id=i
                tempButton.tag=answer
                tempButton.isChecked=selPosition[item.optiont]==i  //是否相等
                tempButton.isEnabled=false
                holder.getView<RadioGroup>(R.id.mRadio).addView(tempButton, lp)
            }
        }
    }
}