package com.xty.common

import android.content.Context
import android.graphics.Bitmap
import android.graphics.Canvas
import android.view.View
import android.widget.ImageView
import androidx.core.content.ContextCompat
import com.bumptech.glide.Glide
import com.bumptech.glide.load.MultiTransformation
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import com.xty.common.weight.VisceraView

/**
 * view 绑定
 */
fun ImageView.setImage(context: Context, string: String?) {
    Glide.with(context).load(string)
        .placeholder(ContextCompat.getDrawable(context, R.mipmap.ic_default_round))
        .error(R.mipmap.ic_default_round).into(this)
}

fun ImageView.setRoundImage(context: Context, string: String?) {
    Glide.with(context).load(string)
        .transform(MultiTransformation(CenterCrop(), RoundedCorners(18)))
        .placeholder(ContextCompat.getDrawable(context, R.mipmap.ic_default_round))
        .error(R.mipmap.ic_default_round).into(this)
}

/**
 * 欢迎界面图片绑定 绑定
 */
fun ImageView.setImageWelcome(context: Context, string: String?) {
    Glide.with(context).load(string).error(R.mipmap.ic_welcome_logo).into(this)
}

fun getImageUrl(url: String?): String {
    return if (url.isNullOrEmpty()) "" else if (url.startsWith("http")) url else BuildConfig.image_prefix + url
}

fun getImageUrl(url: String?, prefixUrl: String = ""): String {
    return if (url.isNullOrEmpty()) "" else if (url.startsWith("http")) url else if (prefixUrl.isNullOrEmpty()) BuildConfig.image_prefix + url else prefixUrl + url
}

/**
 * view 绑定
 */
fun ImageView.setImageUser(context: Context, string: String?) {
    Glide.with(context).load(string).placeholder(
        ContextCompat.getDrawable(
            context,
            R.mipmap.icon_default_avator
        )
    ).error(R.mipmap.icon_default_avator).into(this)
}

/**
 * view 绑定
 */
fun ImageView.setImageUser(context: Context, string: String?, sex: String) {
    val userHeadImg = if (sex == "1") {
        R.mipmap.icon_default_avator
    } else {
        R.mipmap.icon_default_avator
    }
    Glide.with(context).load(string).placeholder(
        ContextCompat.getDrawable(
            context,
            userHeadImg
        )
    ).error(userHeadImg).into(this)
}

fun ImageView.setBigImage(context: Context, string: String?) {
    Glide.with(context).load(string).placeholder(R.mipmap.ic_default_round).dontAnimate()
        .into(this);
}

/**
 * 获取视频的第一帧图片
 * frame 单位是微秒
 */
fun ImageView.getThumbVideo(context: Context, path: String?) {
    Glide.with(context).setDefaultRequestOptions(RequestOptions().frame(1000000).centerCrop())
        .load(path).into(this)
}

/**
 * 设置ImageView的器官状态
 *
 */
fun ImageView.setOrganStatus(
    context: Context,
    status: Int,
    icon: Array<Int>,
    isClick: Boolean = true,
    click: (view: View) -> Unit
) {
    if (status > 4) {
        return
    }
    var draw = ContextCompat.getDrawable(context, icon[status])
    setImageDrawable(draw)
    if (isClick) {
        setOnClickListener {
            isSelected = true
            click.invoke(it)

        }
    }

}

/**
 * 设置ImageView的器官状态
 *
 */
fun VisceraView.setOrganStatus(
    context: Context,
    status: Int,
    icon: Array<Int>,
    isClick: Boolean = true,
    click: (view: View) -> Unit
) {
    setInfo(status, icon)
    if (isClick) {
        setOnClickListener {
            isSelected = true
            click.invoke(it)

        }
    }

}


/**
 * view截取
 */
fun View.ScreenThis(y: Int, child: View, startH: Int): Bitmap {
    var bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
    var canvas = Canvas(bitmap)
    child.draw(canvas)
    child.layout(0, 0, width, child.height)
    return bitmap

}

/**
 * view截取
 * startH  截取的起始高度
 */
fun View.Screen(startH: Int = 0): Bitmap {
    var bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
    var canvas = Canvas(bitmap)
    this.draw(canvas)
    this.layout(0, 0 + startH, width, height + startH)
    return bitmap
}


/**
 * 2s点击事件
 */
fun View.doubleClick(click: () -> Unit) {
    var time = 0L
    setOnClickListener {
        if (time == 0L) {
            click.invoke()
            time = System.currentTimeMillis()
        } else if (System.currentTimeMillis() - time > 2000) {
            click.invoke()
            time = System.currentTimeMillis()
        }

    }
}



