package com.convenient.android.common.extension

import android.app.Activity
import android.content.Context
import android.content.ContextWrapper
import android.view.View
import android.view.ViewGroup
import androidx.annotation.DrawableRes
import androidx.core.content.ContextCompat
import androidx.lifecycle.findViewTreeLifecycleOwner
import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

/**
 * @classname:ViewExtensions
 * @author: LiuXiaoLong
 * @date: 2022/7/27
 * description: view相关扩展
 */


/****** 获取Drawable Start ******/
fun Context.drawableByResId(@DrawableRes id: Int) = ContextCompat.getDrawable(this, id)
/****** 获取Drawable  End ******/

fun Context.setViewsAlpha(alpha : Float, vararg views: View){
    for (view in views) {
        view.alpha = alpha
    }
}

/****** View 点击事件添加扩展 Start ******/
fun Context.setViewsClick(listener: View.OnClickListener, vararg views: View) {
    views.forEach {
        it.setOnClickListener(listener)
    }
}

fun <T : View> Context.setViewsClickWithTrigger(block: (T) -> Unit, vararg views: View) {
    views.forEach {
        it.clickWithTrigger { v ->
            block(v as T)
        }
    }
}

fun <T : View> Context.setViewsClickAlphaWithTrigger(block: (T) -> Unit, vararg views: View) {
    views.forEach {
        it.clickAlphaWithTrigger { v ->
            block(v as T)
        }
    }
}

fun <T : View> T.clickWithTrigger(block: (T) -> Unit) {
    setOnClickListener {
        if (clickEnable()) {
            block(it as T)
        }
    }
}

@JvmOverloads
fun <T : View> T.clickAlphaWithTrigger(time: Long = 500, block: (T) -> Unit) {
    triggerDelay = time
    setOnClickListener {
        try {
            it.findViewTreeLifecycleOwner()?.lifecycleScope?.launch {
                if (clickEnable()) {
                    it.alpha = 0.6f
                    delay(150)
                    it.alpha = 1.0f
                    block(it as T)
                }
            }?:block(it as T)
        } catch (e: Exception) {

        }
    }
}

fun <T : View> T.longClickAlphaWithTrigger(time: Long = 600, block: (T) -> Unit) {
    triggerDelay = time
    setOnLongClickListener {
        try {
            it.findViewTreeLifecycleOwner()?.lifecycleScope?.launch {
                if (clickEnable()) {
                    it.alpha = 0.6f
                    delay(150)
                    it.alpha = 1.0f
                    block(it as T)
                }
            }?:block(it as T)
        } catch (e: Exception) {

        }
        true
    }
}
/****** View 点击事件添加扩展 End ******/

/****** 防重点击相关扩展属性 Start ******/
private var <T : View> T.triggerLastTime: Long
    get() = if (getTag(1123460103) != null) (getTag(1123460103) as Long) else 0
    set(value) {
        setTag(1123460103, value)
    }

private var <T : View> T.triggerDelay: Long
    get() = if (getTag(1123461123) != null) (getTag(1123461123) as Long) else -1
    set(value) {
        setTag(1123461123, value)
    }

fun <T : View> T.withTrigger(delay: Long = 600): T {
    triggerDelay = delay
    return this
}

private fun <T : View> T.clickEnable(): Boolean {
    var flag = false
    val currentClickTime = System.currentTimeMillis()
    if (currentClickTime - triggerLastTime > triggerDelay) {
        flag = true
    }
    triggerLastTime = currentClickTime
    return flag
}
/****** 防重点击相关扩展属性 End ******/


fun View.getActivityFromView() : Activity?{
    var cont = context
    while (cont is ContextWrapper){
        if (cont is Activity){
            return cont as Activity
        }
        cont = cont.baseContext
    }
    return null
}

fun View.removeParentView(){
    if (this.parent is ViewGroup) {
        val parent = parent as ViewGroup
        parent.removeView(this)
    }
}