package com.yzx.ebrand.adapter

import android.annotation.SuppressLint
import android.graphics.Color
import android.view.View
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.yzx.ebrand.R
import com.yzx.ebrand.model.Visitor
import com.yzx.ebrand.utils.setSrc
import com.yzx.ebrand.utils.toTime
import kotlinx.android.synthetic.main.layout_visitor_table.view.*
import org.jetbrains.anko.textColor

class VisitorListAdapter(list: MutableList<Visitor>) :
    BaseQuickAdapter<Visitor, BaseViewHolder>(R.layout.layout_visitor_table, list) {

    init {
        addChildClickViewIds(R.id.faceEmpty,R.id.nucleinEmpty,R.id.tripCodeEmpty,R.id.healthCodeEmpty)
    }

    @SuppressLint("SetTextI18n", "Range")
    override fun convert(holder: BaseViewHolder, item: Visitor) {
        holder.itemView.userName.text = item.user_name
        if (item.face_key.isNotEmpty()) {
            holder.itemView.faceImg.visibility = View.VISIBLE
            holder.itemView.faceEmpty.visibility = View.GONE
            holder.itemView.faceImg.setSrc(item.face_identity, item.face_key)
        } else {
            holder.itemView.faceEmpty.visibility = View.VISIBLE
            holder.itemView.faceImg.visibility = View.GONE
        }
        when {
            item.health_code_status >= 0 -> {
                holder.itemView.healthCodeStatusLayout.visibility = View.VISIBLE
                holder.itemView.healthCodeImg.visibility = View.GONE
                holder.itemView.healthCodeEmpty.visibility = View.GONE
                when (item.health_code_status) {
                    0 -> {
                        holder.itemView.healthCodeStatus.text = "状态：绿码"
                        holder.itemView.healthCodeStatus.textColor = Color.parseColor("#5DAD64")
                    }
                    1 -> {
                        holder.itemView.healthCodeStatus.text = "状态：黄码"
                        holder.itemView.healthCodeStatus.textColor = Color.parseColor("#FCCF13")
                    }
                    10 -> {
                        holder.itemView.healthCodeStatus.text = "状态：红码"
                        holder.itemView.healthCodeStatus.textColor = Color.parseColor("#FC0416")
                    }
                }
            }
            item.health_code_key.isNotEmpty() -> {
                holder.itemView.healthCodeStatusLayout.visibility = View.GONE
                holder.itemView.healthCodeImg.visibility = View.VISIBLE
                holder.itemView.healthCodeEmpty.visibility = View.GONE
                holder.itemView.healthCodeImg.setSrc(
                    item.health_code_identity,
                    item.health_code_key
                )

            }
            else -> {
                holder.itemView.healthCodeStatusLayout.visibility = View.GONE
                holder.itemView.healthCodeEmpty.visibility = View.VISIBLE
                holder.itemView.healthCodeImg.visibility = View.GONE
            }
        }
        if(item.nuclein_file_key.isNullOrBlank()){
            holder.itemView.nucleinEmpty.visibility = View.VISIBLE
            holder.itemView.nucleinImg.visibility = View.GONE
        }else{
            holder.itemView.nucleinImg.visibility = View.VISIBLE
            holder.itemView.nucleinEmpty.visibility = View.GONE
            holder.itemView.nucleinImg.setSrc(item.nuclein_file_identity, item.nuclein_file_key)
        }
        if(item.nuclein_time>0){
            holder.itemView.nucleinStatusLayout.visibility = View.VISIBLE
            holder.itemView.nucleinStatus.text = item.getNucleinStatusObj().nuclein_name
            holder.itemView.nucleinStatus.setTextColor(Color.parseColor(item.getNucleinStatusObj().nuclein_color))
        }else{
            holder.itemView.nucleinStatusLayout.visibility = View.GONE
        }
        if (item.trip_code_key.isNullOrBlank()) {
            holder.itemView.tripCodeEmpty.visibility = View.VISIBLE
            holder.itemView.tripCodeImg.visibility = View.GONE
        } else {
            holder.itemView.tripCodeImg.visibility = View.VISIBLE
            holder.itemView.tripCodeEmpty.visibility = View.GONE
            holder.itemView.tripCodeImg.setSrc(item.trip_code_identity, item.trip_code_key)
        }
        holder.itemView.isDrive.text = if (item.is_drive == 1) {
            "是"
        } else {
            "否"
        }
        holder.itemView.receptionUser.text = item.reception_user
        holder.itemView.statusName.text = when (item.audit_status) {
            0 -> "待审核"
            1 -> "可通行"
            2 -> "审核拒绝"
            -1 -> "已失效"
            else -> "--"
        }
        holder.itemView.statusName.textColor = when (item.audit_status) {
            0 -> Color.parseColor("#333333")
            1 -> Color.parseColor("#3c7ef6")
            2 -> Color.parseColor("#ff4040")
            -1 -> Color.parseColor("#999999")
            else -> Color.parseColor("#333333")
        }

        holder.itemView.applyTime.text = item.add_time.toTime()

        holder.itemView.bottomLine.visibility = if (holder.adapterPosition == data.size - 1) {
            View.GONE
        } else {
            View.VISIBLE
        }
    }
}