package com.yzx.escreen.widget

import android.annotation.SuppressLint
import android.content.Context
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.HomeDialogStepAdapter
import com.yzx.escreen.model.StuLeave
import com.yzx.escreen.model.ViewImage
import com.yzx.escreen.utils.durationStr
import com.yzx.escreen.utils.setSrc
import com.yzx.escreen.utils.toTime
import kotlinx.android.synthetic.main.dialog_leave.view.*
import org.greenrobot.eventbus.EventBus
import org.jetbrains.anko.find

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

    val adapter: HomeDialogStepAdapter by lazy {
        HomeDialogStepAdapter(mutableListOf())
    }
    var onCloseClick: (view: View) -> Unit = {}
    var onSureClick: (view: View, item: StuLeave) -> Unit = { view: View, stuLeave: StuLeave -> }

    init {
        val view = LayoutInflater.from(context).inflate(R.layout.dialog_leave, 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: StuLeave) {
        leave_student_name.text = detail.leave_student_name
        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
        } 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.leave_request_type_name
        find<TextView>(R.id.leave_duration_str).text =
            (detail.end_time - detail.start_time).durationStr()
        find<TextView>(R.id.add_time).text =
            "${detail.start_time.toTime()} - ${detail.end_time.toTime()}"
        find<TextView>(R.id.leave_request_reason).text = detail.leave_request_reason
        find<ImageView>(R.id.statusImg).setSrc(
            when (detail.status) {
                1 -> "https://oa-edu-1259243469.cos.ap-chengdu.myqcloud.com/public/mini/stutakeleave_wait.png"
                2 -> "https://oa-edu-1259243469.cos.ap-chengdu.myqcloud.com/public/mini/stutakeleave_pass.png"
                3 -> "https://oa-edu-1259243469.cos.ap-chengdu.myqcloud.com/public/mini/stutakeleave_nopass.png"
                4 -> "https://oa-edu-1259243469.cos.ap-chengdu.myqcloud.com/public/mini/cancellation.png"
                5 -> "https://oa-edu-1259243469.cos.ap-chengdu.myqcloud.com/public/mini/cancellation_overtime.png"
                else -> ""
            }
        )

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

        recyclerView.adapter = adapter
        adapter.setList(detail.getStep())
        recyclerView.scrollToPosition(adapter.itemCount - 1)
        find<View>(R.id.closeBtn).setOnClickListener {
            onCloseClick(this)
        }
        find<View>(R.id.sureBtn).setOnClickListener {
            onSureClick(this, detail)
        }
        find<View>(R.id.sureBtn).visibility = if (detail.is_hide == 0) {
            View.VISIBLE
        } else {
            View.GONE
        }
    }
}