package com.adealink.weparty.commonui.viewpager

import android.view.View
import androidx.viewpager.widget.ViewPager
import kotlin.math.abs

/**
 * https://blog.csdn.net/wangxiaocheng16/article/details/95472436
 */
class ScaleAlphaTransformer(val scale: Float, val alpha: Float? = null) : ViewPager.PageTransformer {

    override fun transformPage(page: View, position: Float) {
        page.pivotY = page.height.toFloat() / 2
        if (position < -1) {
            page.pivotX = page.width.toFloat()
        } else if (position < 0) {
            page.pivotX = page.width.toFloat() * (0.5f - 0.5f * position)
        } else if (position <= 1) {
            page.pivotX = page.width.toFloat() * (0.5f - 0.5f * position)
        } else {
            page.pivotX = 0f
        }
        if (position <= 1) {
            val scaleFactor = scale + (1 - abs(position)) * (1 - scale)
            page.scaleX = scaleFactor
            page.scaleY = scaleFactor

            alpha?.let { it ->
                val alphaFactor = it + (1 - abs(position)) * (1 - it)
                page.alpha = alphaFactor
            }
        } else {
            page.scaleX = scale
            page.scaleY = scale
            alpha?.let { it ->
                page.alpha = it
            }
        }
    }
}