package com.remax.visualnovel.extension

import android.view.View
import androidx.core.widget.NestedScrollView
import com.remax.visualnovel.app.delegate.navBgAlpha
import com.remax.visualnovel.app.delegate.titleTextAlpha
import com.remax.visualnovel.utils.KeyboardUtils
import com.google.android.material.appbar.AppBarLayout
import com.remax.visualnovel.configs.NovelApplication

/**
 * Created by HJW on 2023/2/13
 */

/**
 * 当布局中有AppBarLayout时，滑动渐隐公共toolbar的标题
 * @param titleView 滑动到title。渐变完成
 * @param titleGroup 当页面撑满整个状态栏时，给toolbarLayout最小高度，当toolbarLayout中的元素比较多时，渐变只到titleTextView的底部，才传这个进来
 * @param includeBgAlpha 是否渐隐toolbar的背景色
 * @param isFromToolbar toolbar是否在页面上方，默认toolbar和contentLayout重叠在一起，即是false
 *
 */
fun AppBarLayout.addScrollerAlpha(
    titleView: View,
    titleGroup: View? = null,
    includeBgAlpha: Boolean = false,
    isFromToolbar: Boolean = false,
    textAlpha: Boolean = true,
    alphaChanged: ((alpha: Float, verticalOffset: Int) -> Unit)? = null
) {
    context.findBaseActivity()?.apply {
        if (includeBgAlpha) {
            // 当主页面填满整个屏幕时，给一个最小高度，和标题栏的高度一样，滑动的时候显示即正常
            (titleGroup ?: titleView).minimumHeight = getNavHeight()
        }
        addOnOffsetChangedListener { _, verticalOffset ->
            updateToolbar {
                val alpha = if (isFromToolbar)
                    calculateScrollerAlphaFromToolbar(verticalOffset, titleView.bottom)
                else
                    calculateScrollerAlpha(verticalOffset, titleView.bottom)

                alphaChanged?.invoke(alpha, verticalOffset)
                if (textAlpha) {
                    titleTextAlpha = alpha
                }
                if (includeBgAlpha) {
                    navBgAlpha = alpha
                }
            }
        }
    }
}

/**
 * 同上，这是NestedScrollView的渐隐扩展方法
 * */
fun NestedScrollView.addScrollerAlpha(
    titleView: View,
    includeBgAlpha: Boolean = false,
    isFromToolbar: Boolean = false,
    textAlpha: Boolean = true,
    scrollChange: ((alpha: Float, scrollY: Int) -> Unit)? = null
) {
    context.findBaseActivity()?.apply {
        if (textAlpha) {
            updateToolbar {
                titleTextAlpha = 0f
            }
        }
        setOnScrollChangeListener { _, _, scrollY, _, _ ->
            if (KeyboardUtils.isSoftInputVisible(this)) {
                KeyboardUtils.hideSoftInput(this)
            }
            val alpha = if (isFromToolbar) calculateScrollerAlphaFromToolbar(
                scrollY,
                titleView.bottom
            ) else calculateScrollerAlpha(
                scrollY,
                titleView.bottom
            )
            scrollChange?.invoke(alpha, scrollY)
            updateToolbar {
                if (textAlpha) {
                    titleTextAlpha = alpha
                }
                if (includeBgAlpha) {
                    navBgAlpha = alpha
                }
            }
        }
    }
}

/**
 * 滑动时关闭键盘
 */
fun NestedScrollView.scrollAndHideKeyboard() {
    setOnScrollChangeListener { _, _, _, _, _ ->
        NovelApplication.getCurrentActivity()?.let { act ->
            if (KeyboardUtils.isSoftInputVisible(act)) {
                KeyboardUtils.hideSoftInput(act)
            }
        }
    }
}
