package com.zj365.health.adapter

import android.util.DisplayMetrics
import android.util.TypedValue
import android.view.ViewGroup
import android.widget.EditText
import android.widget.RadioButton
import android.widget.RadioGroup
import androidx.core.content.ContextCompat
import androidx.core.widget.addTextChangedListener
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.xty.network.model.QuestBean
import com.zj365.health.R

class QuestAdapter(): BaseMultiItemQuickAdapter<QuestBean.Question, BaseViewHolder>()  {

    //`type_id` int(11) DEFAULT NULL COMMENT '题型(1.判断，2.单选，3.多选，4.问答)',
    init {
        addItemType(2, R.layout.item_quest_type_2)
        addItemType(4, R.layout.item_quest_type_4)
    }

    override fun convert(holder: BaseViewHolder, item: QuestBean.Question) {
        when(item.itemType){
            2->{
                holder.setText(R.id.mNum,"${holder.layoutPosition+1}/${data.size}")
                    .setText(R.id.nQuestName,item.question)
                val replace = item.descAnswer.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).removeAllViews()
                beanArray.forEachIndexed { i, answer ->
                    var  tempButton =   RadioButton(context);//this就是你的activity
                    var  lp =  RadioGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                    lp.setMargins( dp2px(12f), dp2px(8f), 0,  dp2px(8f));// 设置RadioButton边距 (int left, int top, int right, int bottom)
                   // tempButton.setBackgroundResource(R.drawable.shape_corners_delivered_14dp_bg);    // 设置RadioButton的背景图片
                    tempButton.setPadding(dp2px(12f), 0, dp2px(12f), 0);                // 设置文字距离按钮四周的距离
                    tempButton.setTextColor(ContextCompat.getColor(context,R.color.col_455));//字体颜色
                    tempButton.buttonDrawable = ContextCompat.getDrawable(context,R.drawable.selector_check_medical);//取消前面的圆点
                    tempButton.text = "${answer.value} ${answer.label}"
                    tempButton.textSize = 14f
                    tempButton.id=i
                    tempButton.tag=answer
                    tempButton.isChecked=item.selectPosition==i
                    holder.getView<RadioGroup>(R.id.mRadio).addView(tempButton, lp);
                }
                holder.getView<RadioGroup>(R.id.mRadio).setOnCheckedChangeListener {   group, checkedId ->
                    val childCount = group.childCount
                    for(i in 0 until childCount){
                        if(group.getChildAt(i).id==checkedId){
                           var bean= group.getChildAt(i).tag as QuestBean.Answer
                            item.selectOption=bean.value
                            item.selectPosition=i
                        }
                    }
                }

            }
            4->{
                holder.setText(R.id.mNum,"${holder.layoutPosition+1}/${data.size}")
                    .setText(R.id.mQuestName,item.question)
                holder.getView<EditText>(R.id.mHeight).addTextChangedListener {
                    item.height=it.toString().trim()

                }
                holder.getView<EditText>(R.id.mWeight).addTextChangedListener {
                    item.weight=it.toString().trim()
                }
            }

        }

    }

    private fun dp2px(dp: Float): Int {
        val displayMetrics: DisplayMetrics = context.resources.displayMetrics
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, displayMetrics).toInt()
    }
}