package com.zj365.mime.adapter

import android.annotation.SuppressLint
import android.text.TextUtils
import android.view.View
import android.widget.TextView
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.xty.base.adapter.BaseAdapter
import com.xty.common.SizeUtil
import com.zj365.mime.R
import com.xty.network.model.LnvitationMessageListBean

class LnvitationMessageAdapter : BaseAdapter<LnvitationMessageListBean>(
    R.layout.item_lnvitation_message_list
) {

    private val textColor by lazy {
        arrayOf(
            R.color.col_9BD,
            R.color.col_9Bd,
            R.color.red_9c
        )
    }
    private val bg by lazy {
        arrayOf(
            R.drawable.shape_xya_status_yellow,
            R.drawable.shape_xya_status,
            R.drawable.shape_status_gray
        )
    }
    private fun getDataType(dataType: Int): String {
        val showName = when (dataType) {
            1 -> {
                "佩戴用户"
            }

            2 -> {
                "已解绑"
            }

            else -> {
                "注册用户"
            }
        }
        return showName
    }

    @SuppressLint("SetTextI18n")
    override fun convert(holder: BaseViewHolder, item: LnvitationMessageListBean) {
        val colorIndex = if (!TextUtils.isEmpty(item.deviceId)) {
            1
        } else {
            if (TextUtils.isEmpty(item.actTime)) {
                0
            } else {
                2
            }
        }
        val sex = when(item.sex) {
            "1" -> {
                "男"
            }
            "2" -> {
                "女"
            }
            else -> {
                ""
            }
        }
        //名称
        val name = holder.getView<TextView>(R.id.tvName)
        //性别与年龄
        val sexAndAge = holder.getView<TextView>(R.id.tvSexAndAge)
        //佩戴状态
        val status = holder.getView<TextView>(R.id.tvStatus)
        //电话号码
        val phone = holder.getView<TextView>(R.id.tvPhone)
        //注册时间
        val registerTime = holder.getView<TextView>(R.id.tvItemRegisterTime)
        //邀请时间
        val activateTime = holder.getView<TextView>(R.id.tvItemActivateTime)

        name.text = if (!TextUtils.isEmpty(item.name)) item.name else ""

        sexAndAge.text = if (!TextUtils.isEmpty(sex)) {
            "$sex  |  ${item.age}岁"
        } else {
            "${item.age}岁"
        }

        status.apply {
            setBackgroundResource(bg[colorIndex])
            setTextColor(resources.getColor(textColor[colorIndex]))
            setPadding(
                SizeUtil.dp2px(context, 10f),
                SizeUtil.dp2px(context, 5f),
                SizeUtil.dp2px(context, 10f),
                SizeUtil.dp2px(context, 5f)
            )
            text = getDataType(colorIndex)
        }

        if (!TextUtils.isEmpty(item.phone)) {
            phone.visibility = View.VISIBLE
            phone.text = "电话号码：${item.phone}"
        } else {
            phone.visibility = View.GONE
        }

        if (!TextUtils.isEmpty(item.createTime)) {
            registerTime.visibility = View.VISIBLE
            registerTime.text = "注册时间：${item.createTime}"
        } else {
            registerTime.visibility = View.GONE
        }

        if (!TextUtils.isEmpty(item.actTime)) {
            activateTime.visibility = View.VISIBLE
            activateTime.text = "激活时间：${item.actTime}"
        } else {
            activateTime.visibility = View.GONE
        }
    }
}