package com.zj365.health.adapter

import android.text.Editable
import android.text.TextWatcher
import android.widget.EditText
import android.widget.TextView
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.googlecode.mp4parser.authoring.Edit
import com.xty.base.adapter.BaseAdapter
import com.zj365.health.R
import com.xty.network.model.MedicBean

/**
 * isShow  是否仅作展示
 * isCheckNone 是否点了无
 */
class IllAdapter(
    var isShow: Boolean = false,
    var isCheckNone: Boolean = false,
    var isCheck: Boolean = true,
) :
    BaseAdapter<MedicBean>(R.layout.item_ill) {

    override fun convert(holder: BaseViewHolder, item: MedicBean) {

        val input = holder.getView<EditText>(R.id.mInput)
        val name = holder.getView<TextView>(R.id.mTvName)
        name.isEnabled = isCheck
        input.isEnabled = isCheck
        if (!isShow) {
            if (isCheckNone) {//如果选择无疾病，将所有疾病选择未选择
                data[holder.layoutPosition].isSelect = item.dictName == "无"
                input.setText("")
                data[holder.layoutPosition].inputStr = ""
            } else {
                if (item.dictName == "无") {//如果选择有疾病，将无改为未选择
                    data[holder.layoutPosition].isSelect = false
                }
            }
        }

        name.isSelected = item.isSelect
        holder.setText(R.id.mTvName, item.dictName)
        input.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(
                s: CharSequence?,
                start: Int,
                count: Int,
                after: Int
            ) {
            }

            override fun onTextChanged(
                s: CharSequence?,
                start: Int,
                before: Int,
                count: Int
            ) {
            }

            override fun afterTextChanged(s: Editable?) {
                if (s == null || s.toString().isEmpty()) {
                    item.inputStr = ""
                    item.isSelect = false
                    name.isSelected = false
                } else {
                    item.inputStr = s.toString()
                    item.isSelect = true
                    name.isSelected = true

                    if (isCheckNone){
                        data.forEach {
                            if (it.dictName == "无"){
                                isCheckNone = false
                                it.isSelect = false
                                notifyItemChanged(getItemPosition(it))
                            }
                        }
                    }
                }
            }
        })
        when (item.dictName) {

            "肿瘤" -> {
                holder.setGone(R.id.mInput, false)
                    .setText(R.id.mInput, item.inputStr)
                input.hint = "请输入肿瘤名称"
                name.isEnabled = false
            }

            "其他" -> {
                holder.setGone(R.id.mInput, false)
                    .setText(R.id.mInput, item.inputStr)
                input.hint = "请输入名称"
                name.isEnabled = false
            }

            else -> {
                holder.setGone(R.id.mInput, true)
                input.hint = ""
                name.isEnabled = true
            }
        }
    }


}