package com.gh.ad

import android.view.View
import android.view.ViewGroup
import android.view.ViewStub
import androidx.fragment.app.Fragment
import com.gh.gamecenter.common.constant.Constants
import com.gh.gamecenter.common.provider.ILaunchAd
import com.gh.gamecenter.common.utils.SensorsBridge
import com.gh.gamecenter.common.utils.goneIf
import com.gh.gamecenter.core.utils.DisplayUtils
import com.gh.gamecenter.core.utils.SPUtils
import com.gh.gamecenter.entity.AdConfig

@com.therouter.inject.ServiceProvider
class LaunchAdImpl : ILaunchAd {

    override fun requestAd(fragment: Fragment, container: ViewGroup, maskView: View, topViewStub: ViewStub, bottomViewStub: ViewStub, adClickAction: () -> Unit): View {
        if (AdDelegateHelper.shouldShowHelperLaunchAd()) {
            val launchAd = AdDelegateHelper.vGameLaunchAd
            val showThirdPartyAd = launchAd?.displayRule?.adSource == AdDelegateHelper.AD_TYPE_SDK
            val thirdPartyAd = launchAd?.thirdPartyAd
            if (showThirdPartyAd && thirdPartyAd != null) {
                val onAdShowAction = {
                    SensorsBridge.trackEvent("ThirdPartyAdShow",
                        "ad_source", thirdPartyAd.sourceName,
                        "ad_id", thirdPartyAd.slotId,
                        "ad_format", launchAd.typeChinese,
                        "ad_placement", AD_PLACEMENT,
                        "ad_space_id", launchAd.id,
                        "ad_space_name", launchAd.name
                    )
                }
                val onAdClickAction = {
                    SensorsBridge.trackEvent("ThirdPartyAdClick",
                        "ad_source", thirdPartyAd.sourceName,
                        "ad_id", thirdPartyAd.slotId,
                        "ad_format", launchAd.typeChinese,
                        "ad_placement", AD_PLACEMENT,
                        "ad_space_id", launchAd.id,
                        "ad_space_name", launchAd.name
                    )
                    adClickAction.invoke()
                }

                if (launchAd.type == AdConfig.TYPE_BANNER) {
                    requestBannerAd(fragment, container, maskView, thirdPartyAd, onAdShowAction, onAdClickAction)
                    return topViewStub.inflate()
                } else if (launchAd.type == AdConfig.TYPE_INTERSTITIAL) {
                    requestFullScreenAd(fragment, thirdPartyAd, onAdShowAction, onAdClickAction)
                    return bottomViewStub.inflate()
                }
            }
        }
        return bottomViewStub.inflate()
    }

    private fun requestBannerAd(
        fragment: Fragment,
        container: ViewGroup,
        maskView: View,
        thirdPartyAd: AdConfig.ThirdPartyAd,
        onAdShowAction: () -> Unit,
        onAdClickAction: () -> Unit,
    ) {
        AdDelegateHelper.requestThirdPartyBannerAd(
            fragment,
            container,
            thirdPartyAd,
            DisplayUtils.getScreenWidthInDp(fragment.requireActivity()),
            onAdShowAction,
            onAdClickAction
        ) { isSuccess ->
            maskView.goneIf(!isSuccess)
            if (isSuccess) {
                SPUtils.setLong(Constants.SP_LAST_HELPER_LAUNCH_AD_SHOW_TIME, System.currentTimeMillis())
            }
        }
    }

    private fun requestFullScreenAd(
        fragment: Fragment,
        thirdPartyAd: AdConfig.ThirdPartyAd,
        onAdShowAction: () -> Unit,
        onAdClickAction: () -> Unit
    ) {
        AdDelegateHelper.requestFullScreenAd(
            fragment,
            thirdPartyAd,
            onAdShowAction,
            onAdClickAction
        ) { isSuccess ->
            if (isSuccess) {
                SPUtils.setLong(Constants.SP_LAST_HELPER_LAUNCH_AD_SHOW_TIME, System.currentTimeMillis())
            }
        }
    }

    companion object {
        private const val AD_PLACEMENT = "畅玩启动"
    }
}