package com.gh.gamecenter.home.test_v2

import com.gh.common.util.NewFlatLogUtils
import com.lightgame.utils.Utils

/**
 * @author : liujiarui
 * date : 2023/3/13
 * description :
 */
class HomeItemGameTestV2ViewHolderWatcher : OnViewHolderVisibleChangeListener<HomeItemGameTestV2ViewHolder> {
    //条目展示时长
    private var mVisibleTime = 0L

    //完整可见状态
    private var mFullVisibleState = false
    override fun onViewHolderVisibleStateChanged(
        position: Int,
        viewHolder: HomeItemGameTestV2ViewHolder,
        isVisible: Boolean,
        visiblePercent: Float
    ) {
        if (isVisible && visiblePercent == 1F && !mFullVisibleState) {
            mFullVisibleState = true
            //状态变为完整可见,记录时间戳
            mVisibleTime = System.currentTimeMillis()
            Utils.log(
                "HomeItemGameTestV2ViewHolderWatcher " +
                        "不完整可见变完整可见"
            )
        }
        if (visiblePercent < 1F && mFullVisibleState) {
            //从完整可见变为不完整可见
            mFullVisibleState = false
            calcInterval(viewHolder)
            Utils.log(
                "HomeItemGameTestV2ViewHolderWatcher " +
                        "完整可见变不完整可见"
            )
        }
    }

    private fun calcInterval(viewHolder: HomeItemGameTestV2ViewHolder) {
        //完整可见时长
        val interval = System.currentTimeMillis() - mVisibleTime
        val location = viewHolder.viewModel.location
        val blockId = viewHolder.viewModel.blockData?.link ?: ""
        val blockName = viewHolder.viewModel.blockData?.name ?: ""
        val type = viewHolder.getTimeLineAdapter()?.getType() ?: ""
        val startTime = viewHolder.getTimeLineAdapter()?.getStartTime() ?: 0
        val sequence = viewHolder.getTimeLineAdapter()?.getSequence() ?: 0
        //上报新游开测 item 完整展示时长
        NewFlatLogUtils.logGameTestV2ItemViewTime(
            location, blockId, blockName, interval, type, startTime, sequence
        )
    }
}