package com.adealink.weparty.commonui.widget.banner.transformer

import android.util.Log
import android.view.View

/**
 * 确保缩放以 item 的中心为轴，居中缩放
 * Created by XiaoDongLin.
 * Date: 2025/1/3
 */
class CenterScaleInTransformer(val mMinScale: Float) : BasePageTransformer() {
//    private var mMinScale = DEFAULT_MIN_SCALE

    override fun transformPage(view: View, position: Float) {
        val pageWidth = view.width
        val pageHeight = view.height

        Log.d("TAG", "transformPage: pageWidth:$pageWidth")

        // 设置缩放中心为页面的中点
        view.pivotY = (pageHeight / 2).toFloat()
        view.pivotX = (pageWidth / 2).toFloat()
        if (position < -1) { // [-Infinity,-1)
            // This page is way off-screen to the left.
            view.scaleX = mMinScale
            view.scaleY = mMinScale
        } else if (position <= 1) { // [-1,1]
            // Modify the default slide transition to shrink the page as well
            if (position < 0) //1-2:1[0,-1] ;2-1:1[-1,0]
            {
                val scaleFactor = (1 + position) * (1 - mMinScale) + mMinScale
                view.scaleX = scaleFactor
                view.scaleY = scaleFactor
            } else  //1-2:2[1,0] ;2-1:2[0,1]
            {
                val scaleFactor = (1 - position) * (1 - mMinScale) + mMinScale
                view.scaleX = scaleFactor
                view.scaleY = scaleFactor
            }
        } else { // (1,+Infinity]
            view.scaleX = mMinScale
            view.scaleY = mMinScale
        }
    }

    companion object {
        private const val DEFAULT_MIN_SCALE = 0.85f
    }
}
