package com.remax.visualnovel.extension

import android.animation.ObjectAnimator
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import android.view.ViewGroup.LayoutParams
import android.view.ViewGroup.MarginLayoutParams
import android.view.animation.AccelerateDecelerateInterpolator
import android.widget.EditText
import androidx.core.view.updateLayoutParams
import com.remax.visualnovel.utils.KeyboardUtils
import com.google.android.material.appbar.AppBarLayout

/**
 * Created by HJW on 2021/4/1
 */

/**
 * 标题arrow旋转动画
 */
fun View.navRotationOpen(isOpen: Boolean) {
    val end: Float = if (isOpen) 180f else 0.toFloat()
    val start: Float = if (isOpen) 0f else 180.toFloat()
    ObjectAnimator.ofFloat(this, "rotation", start, end).setDuration(200).start()
}

/**
 * 在editText上显示键盘
 */
fun View.showKeyboard() {
    (this as? EditText)?.run {
        setSelection(text.toString().length)
    }
    KeyboardUtils.showSoftInput(this)
}

fun View.setMargin(marginStart: Int? = null, topMargin: Int? = null, marginEnd: Int? = null, bottomMargin: Int? = null) {
    (layoutParams as? MarginLayoutParams)?.let {
        updateLayoutParams<MarginLayoutParams> {
            marginStart?.run(this::setMarginStart)
            marginEnd?.run(this::setMarginEnd)
            if (topMargin != null) this.topMargin = topMargin
            if (bottomMargin != null) this.bottomMargin = bottomMargin
        }
    }
}

fun View.setSize(width: Int? = null, height: Int? = null): Pair<Int?, Int?> {
    if (layoutParams == null) {
        layoutParams = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)
    }
    updateLayoutParams<LayoutParams> {
        width?.let {
            this.width = it
        }
        height?.let {
            this.height = it
        }
    }
    return Pair(width, height)
}

fun View.setAllParentClipChildren() {
    if (parent is ViewGroup && parent !is AppBarLayout) {
        (parent as? ViewGroup)?.run {
            clipToPadding = false
            clipChildren = false
            setAllParentClipChildren()
        }
    }
}

fun View.inRangeOfView(ev: MotionEvent): Boolean {
    val location = IntArray(2)
    this.getLocationOnScreen(location)
    val x = location[0]
    val y = location[1]
    return !(ev.x < x || ev.x > x + this.width || ev.y < y || ev.y > y + this.height)
}

fun View?.isShouldHideKeyboard(event: MotionEvent): Boolean {
    if (this != null) {  //判断得到的焦点控件是否包含EditText
        val l = intArrayOf(0, 0)
        this.getLocationInWindow(l)
        val left = l[0]
        //得到输入框在屏幕中上下左右的位置
        val top = l[1]
        val bottom = top + this.height
        val right = left + this.width
        return !(event.x > left && event.x < right && event.y > top && event.y < bottom)
    }
    // 如果焦点不是EditText则忽略
    return false
}


fun View.translationYObjectAnimator(
    translationY: Float,
    duration: Long,
    onEnd: (() -> Unit)? = null
) {
    val animatorY = ObjectAnimator.ofFloat(this, "translationY", translationY)

    animatorY.apply {
        this.duration = duration
        interpolator = AccelerateDecelerateInterpolator()
        addListener(object : android.animation.AnimatorListenerAdapter() {
            override fun onAnimationEnd(animation: android.animation.Animator) {
                onEnd?.invoke()
            }
        })
    }
    animatorY.start()
}

