package com.yzx.escreen.widget

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.yzx.escreen.R
import com.yzx.escreen.adapter.VisitorDialogStepAdapter
import com.yzx.escreen.model.ViewImage
import com.yzx.escreen.model.Visitor
import com.yzx.escreen.utils.setSrc
import com.yzx.escreen.utils.toTime
import org.greenrobot.eventbus.EventBus
import org.jetbrains.anko.find
import org.jetbrains.anko.textColor

/**
 * 展示访客信息
 */
class VisitorDialog(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0,
    defStyleRes: Int = 0
) :
    ConstraintLayout(context, attrs, defStyleAttr, defStyleRes) {

    val adapter: VisitorDialogStepAdapter by lazy {
        VisitorDialogStepAdapter(mutableListOf())
    }
    var onCloseClick: (view: View) -> Unit = {}

    init {
        val view = LayoutInflater.from(context).inflate(R.layout.dialog_visitor, null)
        val layoutParams = LayoutParams(0, LayoutParams.WRAP_CONTENT)
        layoutParams.matchConstraintPercentWidth = 0.8f
        layoutParams.topToTop = LayoutParams.PARENT_ID
        layoutParams.startToStart = LayoutParams.PARENT_ID
        layoutParams.endToEnd = LayoutParams.PARENT_ID
        layoutParams.bottomToBottom = LayoutParams.PARENT_ID
        view.layoutParams = layoutParams
        addView(view)
        background = ContextCompat.getDrawable(context, R.color.dialog_bg)
        setOnClickListener { } //阻止事件冒泡
    }

    @SuppressLint("SetTextI18n", "Range")
    fun setData(detail: Visitor) {
        //处理弹窗显示
        find<TextView>(R.id.userName).text = "${detail.user_name}     ${detail.user_phone}"
        find<TextView>(R.id.id_card_num).text = "${detail.card_no}"
        if (detail.face_key?.isNotEmpty() == true) {
            val img = find<ImageView>(R.id.face_img)
            img.setSrc(detail.face_identity, detail.face_key)
            img.visibility = View.VISIBLE
            find<View>(R.id.face_empty).visibility = View.GONE
            find<View>(R.id.face_layout).visibility = View.VISIBLE
        } else {
            find<View>(R.id.face_empty).visibility = View.VISIBLE
            find<View>(R.id.face_img).visibility = View.GONE
            find<View>(R.id.face_layout).visibility = View.GONE
        }
        find<ImageView>(R.id.face_img).setOnClickListener {
            EventBus.getDefault().post(ViewImage(detail.id, detail.face_identity, detail?.face_key?:""))
        }
//        when {
//            detail.health_code_status >= 0 -> {
//                healthCodeStatusLayout.visibility = VISIBLE
//                health_empty.visibility = GONE
//                health_img.visibility = GONE
//                when (detail.health_code_status) {
//                    0 -> {
//                        healthCodeStatus.text = "状态：绿码"
//                        healthCodeStatus.textColor = Color.parseColor("#5DAD64")
//                    }
//                    1 -> {
//                        healthCodeStatus.text = "状态：黄码"
//                        healthCodeStatus.textColor = Color.parseColor("#FCCF13")
//                    }
//                    10 -> {
//                        healthCodeStatus.text = "状态：红码"
//                        healthCodeStatus.textColor = Color.parseColor("#FC0416")
//                    }
//                }
//            }
//            detail.health_code_key.isNotEmpty() -> {
//                healthCodeStatusLayout.visibility = GONE
//                val img = find<ImageView>(R.id.health_img)
//                img.setSrc(detail.health_code_identity, detail.health_code_key)
//                img.visibility = View.VISIBLE
//                find<View>(R.id.health_empty).visibility = View.GONE
//            }
//            else -> {
//                healthCodeStatusLayout.visibility = GONE
//                find<View>(R.id.health_empty).visibility = View.VISIBLE
//                find<View>(R.id.health_img).visibility = View.GONE
//            }
//        }
//        find<ImageView>(R.id.health_img).setOnClickListener {
//            EventBus.getDefault()
//                .post(ViewImage(detail.id, detail.health_code_identity, detail.health_code_key))
//        }
//        if (detail.trip_code_key.isNotEmpty()) {
//            val img = find<ImageView>(R.id.trip_img)
//            img.setSrc(detail.trip_code_identity, detail.trip_code_key)
//            img.visibility = View.VISIBLE
//            find<View>(R.id.trip_empty).visibility = View.GONE
//        } else {
//            find<View>(R.id.trip_empty).visibility = View.VISIBLE
//            find<View>(R.id.trip_img).visibility = View.GONE
//        }
//        if(detail.nuclein_file_key.isNullOrBlank()){
//            nuclein_img.visibility = View.GONE
//            nuclein_empty.visibility = View.VISIBLE
//        }else{
//            nuclein_img.visibility = View.VISIBLE
//            nuclein_empty.visibility = View.GONE
//            nuclein_img.setSrc(detail.nuclein_file_identity, detail.nuclein_file_key)
//        }
//        if(detail.nuclein_time>0){
//            nucleinStatusLayout.visibility = VISIBLE
//            nucleinStatus.text = detail.getNucleinStatusObj().nuclein_name
//            nucleinStatus.setTextColor(Color.parseColor(detail.getNucleinStatusObj().nuclein_color))
//        }else{
//            nucleinStatusLayout.visibility = GONE
//        }
//        find<ImageView>(R.id.trip_img).setOnClickListener {
//            EventBus.getDefault()
//                .post(ViewImage(detail.id, detail.trip_code_identity, detail.trip_code_key))
//        }
//        find<ImageView>(R.id.nuclein_img).setOnClickListener {
//            EventBus.getDefault()
//                .post(ViewImage(detail.id, detail.nuclein_file_identity, detail.nuclein_file_key))
//        }
        find<TextView>(R.id.reception_user).text = "${detail.reception_user}"
        find<TextView>(R.id.visit_reason).text = "${detail.visit_reason}"
        find<TextView>(R.id.is_drive).text = when (detail.is_drive) {
            1 -> "是"
            0 -> "否"
            else -> "--"
        }
        if (detail.is_drive == 1) {
            find<View>(R.id.carLayout).visibility = View.VISIBLE
            find<View>(R.id.doorLayout).visibility = View.GONE
            find<TextView>(R.id.license_plate).text = "${detail.license_plate}"
            find<TextView>(R.id.car_in_time).text =
                "${detail.car_in_time.toTime()} - ${detail.car_out_time.toTime()}"
        } else {
            find<View>(R.id.carLayout).visibility = View.GONE
            find<View>(R.id.doorLayout).visibility = View.VISIBLE
            find<TextView>(R.id.audit_status_str).text = when (detail.audit_status) {
                0 -> "待审核"
                1 -> "可通行"
                2 -> "审核拒绝"
                -1 -> "已失效"
                else -> "--"
            }
            find<TextView>(R.id.audit_status_str).textColor = when (detail.audit_status) {
                0 -> Color.parseColor("#333333")
                1 -> Color.parseColor("#3c7ef6")
                2 -> Color.parseColor("#ff4040")
                -1 -> Color.parseColor("#999999")
                else -> Color.parseColor("#333333")
            }
            find<TextView>(R.id.start_time).text =
                "${detail.start_time.toTime()} - ${detail.end_time.toTime()}"
        }

        find<ImageView>(R.id.statusImg).setSrc(detail.getStatusImg())

        find<RecyclerView>(R.id.stepRecycler)
        val recyclerView = find<RecyclerView>(R.id.stepRecycler)
        recyclerView.layoutManager =
            LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
        recyclerView.adapter = adapter
        adapter.setList(detail.getStep())

        find<View>(R.id.closeBtn).setOnClickListener {
            onCloseClick(this)
        }
    }
}