package com.gh.gamecenter.forum.home

import android.app.Activity
import com.gh.gamecenter.common.entity.AdditionalParamsEntity
import com.gh.gamecenter.common.utils.SensorsBridge
import com.gh.gamecenter.common.utils.ShareUtils
import com.gh.gamecenter.common.utils.isPublishEnv
import com.gh.gamecenter.feature.entity.ForumVideoEntity
import com.gh.gamecenter.login.user.UserManager
import com.shuyu.gsyvideoplayer.utils.OrientationUtils

class AnswerArticleVideoViewEventHelper(
    private val entity: ForumVideoEntity,
    private val orientationUtils: OrientationUtils
) : ArticleItemVideoView.OnArticleItemVideoViewEventListener {
    override fun onTrackVideoStartPlaying() {
        SensorsBridge.trackVideoStartPlaying(
            articleId = entity.id,
            bbsId = entity.bbs?.id ?: "",
            bbsType = entity.bbs?.typeChinese ?: "综合论坛",
            customerType = entity.user.auth?.text ?: "",
            videoId = entity.id,
            playType = "视频贴",
            gameForumType = entity.bbs?.game?.categoryChinese ?: "",
            activityTag = entity.tagActivityName,
            articleType = entity.typeChinese
        )
    }

    override fun onTrackVideoEndPlaying(maxPlayedProgress: Int) {
        SensorsBridge.trackVideoEndPlaying(
            articleId = entity.id,
            bbsId = entity.bbs?.id ?: "",
            bbsType = entity.bbs?.typeChinese ?: "综合论坛",
            customerType = entity.user.auth?.text ?: "",
            videoId = entity.id,
            playType = "视频贴",
            gameForumType = entity.bbs?.game?.categoryChinese ?: "",
            activityTag = entity.tagActivityName,
            articleType = entity.typeChinese,
            result = if (maxPlayedProgress >= 95) "是" else "否"
        )
    }

    override fun onShare(videoView: ArticleItemVideoView) {
        val shareIcon = entity.poster
        val shareUrl = if (isPublishEnv()) {
            "https://m.ghzs666.com/video/${entity.id}"
        } else {
            "https://resource.ghzs.com/page/video_play/video/video.html?video=${entity.id}"
        }
        val additionalParams = AdditionalParamsEntity().apply {
            contentType = "视频帖"
            contentId = entity.id ?: ""
            bbsId = entity.bbs?.id ?: ""
            bbsType = entity.bbs?.typeChinese ?: "综合论坛"
            customerType = entity.user.auth?.text ?: ""
            activityTagName = entity.tagActivityName ?: ""
            gameForumType = entity.bbs?.game?.categoryChinese ?: ""
            refUserId = UserManager.getInstance().userId
        }
        val activity = (videoView.context as? Activity) ?: return
        ShareUtils.getInstance(videoView.context).showShareWindowsCallback(activity,
            videoView,
            shareUrl,
            shareIcon,
            entity.title,
            entity.des,
            ShareUtils.ShareEntrance.video,
            entity.id,
            additionalParams,
            object : ShareUtils.ShareCallBack {
                override fun onSuccess(label: String) = Unit

                override fun onCancel() = Unit
            })
    }

    override fun onMutedChanged(isMuted: Boolean) {
        entity.videoIsMuted = isMuted
    }

    override fun onStartWindowFullscreen() {
        orientationUtils.resolveByClick()
    }
}