package com.yzx.ebrand.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.ebrand.R
import com.yzx.ebrand.adapter.ChangeDialogStepAdapter
import com.yzx.ebrand.model.StuChange
import com.yzx.ebrand.model.ViewImage
import com.yzx.ebrand.utils.setSrc
import com.yzx.ebrand.utils.toDay
import org.greenrobot.eventbus.EventBus
import org.jetbrains.anko.find
import org.jetbrains.anko.textColor

/**
 * 展示学生请假
 */
class ChangeDialog(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0,
    defStyleRes: Int = 0
) :
    ConstraintLayout(context, attrs, defStyleAttr, defStyleRes) {

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

    init {
        val view = LayoutInflater.from(context).inflate(R.layout.dialog_change, 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")
    fun setData(detail: StuChange) {
        //处理弹窗显示
        find<TextView>(R.id.leave_student_name).text = detail.status_change_student_name
        if (detail.face_key.isNotEmpty()) {
            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
        } else {
            find<View>(R.id.face_empty).visibility = View.VISIBLE
            find<View>(R.id.face_img).visibility = View.GONE
        }
        find<ImageView>(R.id.face_img).setOnClickListener {
            EventBus.getDefault().post(ViewImage(detail.id,detail.face_identity,detail.face_key))
        }
        find<TextView>(R.id.class_name).text = "${detail.grade_name}${detail.class_name}"
        find<TextView>(R.id.leave_request_type_name).text = detail.status_change_type_name
        find<TextView>(R.id.statusName).text = when (detail.status) {
            1 -> "待处理"
            2 -> "已确认"
            3 -> "已拒绝"
            else -> "--"
        }
        find<TextView>(R.id.statusName).textColor = when (detail.status) {
            1 -> Color.parseColor("#333333")
            2 -> Color.parseColor("#3c7ef6")
            3 -> Color.parseColor("#ff4040")
            else -> Color.parseColor("#333333")
        }
        if (detail.getSubStatusName().isNotEmpty()) {
            find<TextView>(R.id.subStatusName).text = "（${detail.getSubStatusName()}）"
            find<TextView>(R.id.subStatusName).textColor = when (detail.sub_status) {
                0, 2 -> Color.parseColor("#ff4040")
                1, 3 -> Color.parseColor("#07c160")
                else -> Color.parseColor("#333333")
            }
        } else {
            find<TextView>(R.id.subStatusName).text = ""
        }
        var time = "--"
        if (detail.start_time > 0) {
            time = detail.start_time.toDay()
        }
        if (detail.end_time > 0) {
            time = "$time -- ${detail.end_time.toDay()}"
        }
        find<TextView>(R.id.add_time).text = time
        if(detail.status_change_reason.isNotEmpty()){
            find<View>(R.id.remarkLayout).visibility = View.VISIBLE
            find<TextView>(R.id.leave_request_reason).text = detail.status_change_reason
        }else{
            find<View>(R.id.remarkLayout).visibility = View.GONE
        }
        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)
        }
    }
}