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 androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.content.ContextCompat
import com.yzx.escreen.R
import com.yzx.escreen.model.ViewImage
import com.yzx.escreen.utils.setSrc
import kotlinx.android.synthetic.main.dialog_view_img.view.*
import org.jetbrains.anko.find

/**
 * 展示大图
 */
class ViewImageDialog(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0,
    defStyleRes: Int = 0
) :
    ConstraintLayout(context, attrs, defStyleAttr, defStyleRes) {

    var onCloseClick: (view: View) -> Unit = {}
    init {
        val view = LayoutInflater.from(context).inflate(R.layout.dialog_view_img, null)
        val layoutParams = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)
        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(viewImg: ViewImage) {
//        photo_view.setSrc(viewImg.identity,viewImg.key)
//        photo_view.setImageResource(R.mipmap.empty)
        val img = find<ImageView>(R.id.photo_view)
        img.setSrc(viewImg.identity,viewImg.key)
//        img.setSrc("https://fileb.oa.qbjjyyun.net/edufile/imageView?uniqueKey=125a4c591c91fbb9f186da8e54cd31aa")
        photo_view.setOnClickListener {
            onCloseClick(this)
        }
    }
}