package com.duben.microtribe.ad.splash

import android.app.Activity
import android.util.Log
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import com.bytedance.sdk.openadsdk.*
import com.bytedance.sdk.openadsdk.mediation.MediationConstant
import com.bytedance.sdk.openadsdk.mediation.ad.MediationAdSlot
import com.bytedance.sdk.openadsdk.mediation.ad.MediationSplashRequestInfo
import com.duben.microtribe.BuildConfig
import com.duben.microtribe.MintsApplication
import com.duben.microtribe.ad.AdStatusListener
import com.duben.microtribe.utils.GromorePrintUtil
import com.duben.microtribe.utils.LogUtil
import com.duben.microtribe.utils.UIUtils
import java.lang.ref.WeakReference

/**
 * 应用内-网赚开屏
 */
object SplashManager {

    // 兜底配置
    val pangleSplashBottom = object :
        MediationSplashRequestInfo(
            MediationConstant.ADN_PANGLE, "888403155",
            BuildConfig.GROMORE_APP_ID, ""
        ) {} //ok

    private val TAG = SplashManager::class.java.simpleName

    private var mTTSplashAd: TTSplashAd? = null

    private const val AD_TIME_OUT = 5000 //开屏广告加载超时时间
    private var isLoadSuccess = 0 //0-加载失败/未加载 1-加载中 2-加载成功
    private var isShowAd = false // 是否显示广告
    private var adStatusListener: AdStatusListener? = null

    /**
     * 预加载开屏广告
     */
    fun preLoadAd(activity: Activity) {
        val weakActivity = WeakReference<Activity>(activity)
        LogUtil.d(TAG, "gromore 开屏广告-> 1、进入预加载")
        if (isLoadSuccess == 1) {

            return
        }
        val adNativeLoader = TTAdSdk.getAdManager().createAdNative(weakActivity.get())
        val adslot = AdSlot.Builder()
            .setCodeId(BuildConfig.GROMORE_SPLASH_CODE)
            .setImageAcceptedSize(
                UIUtils.getScreenWidth(MintsApplication.getContext()),
                UIUtils.getScreenHeight(MintsApplication.getContext())
            )
            .setMediationAdSlot(
                MediationAdSlot.Builder()
//                    .setMediationSplashRequestInfo(pangleSplashBottom)
                    .build()
            )
            .build()

        adNativeLoader.loadSplashAd(adslot, object : TTAdNative.SplashAdListener {
            override fun onError(p0: Int, p1: String?) {
                LogUtil.e(
                    TAG,
                    "gromore 开屏广告--> 3、Gromore  onSplashAdLoadFail  adErrorCode=${p0}  " + p1
                )
                isLoadSuccess = 0
            }

            override fun onTimeout() {
                LogUtil.d(TAG, "gromore 开屏广告--> 3、Gromore  onAdLoadTimeout")
                isLoadSuccess = 0
            }

            override fun onSplashAdLoad(p0: TTSplashAd?) {
                LogUtil.d(TAG, "gromore 开屏广告--> 3、Gromore  onSplashAdLoadSuccess")
                isLoadSuccess = 2
                mTTSplashAd = p0
            }

        })
    }

    /**
     * 展示开屏广告
     */
    fun showAd(activity: Activity, view: FrameLayout, adStatusListener: AdStatusListener?) {
        this.adStatusListener = adStatusListener
        isShowAd = false
        if (mTTSplashAd != null && isLoadSuccess == 2) {
            LogUtil.d(TAG, "gromore 开屏广告--> 准备调用show")
            mTTSplashAd?.let {
                it.setSplashInteractionListener(adInteractionListener)
//                it.setSplashClickEyeListener(object: ISplashClickEyeListener {
//                    override fun onSplashClickEyeAnimationStart() {
//                    }
//
//                    override fun onSplashClickEyeAnimationFinish() {
//                    }
//
//                    override fun isSupportSplashClickEye(isSupport: Boolean): Boolean {
//                        var dp = it.splashClickEyeSizeToDp
//
//                        // 点睛相关处理
//                        val minWindowSizeFromSdk: IntArray = it.getSplashClickEyeSizeToDp()
//                        val flContent=it.splashView
//                        val params: ViewGroup.LayoutParams = flContent.getLayoutParams() as ViewGroup.LayoutParams
//                        params.height = UIUtils.dp2px(activity, minWindowSizeFromSdk[1].toFloat())
//                        params.width = UIUtils.dp2px(activity, minWindowSizeFromSdk[0].toFloat())
//                        flContent.setLayoutParams(params)
//                        flContent.setBackgroundResource(R.color.common_half_alpha)
//                        flContent.translationX = 200f
//                        flContent.translationY = 200f
//                        it.splashClickEyeAnimationFinish()
//                        return false
//                    }
//                })
//                it.setSplashCardListener(object : ISplashCardListener {
//                    override fun onSplashEyeReady() {
//                        it.splashClickEyeAnimationFinish()
//                    }
//
//                    override fun onSplashClickEyeClose() {
//                        adStatusListener?.adClose(null)
//                    }
//
//                    override fun setSupportSplashClickEye(isSupport: Boolean) {
//                    }
//
//                    override fun getActivity(): Activity {
//                        return  activity
//                    }
//
//                })
                view.addView(it.splashView)
            }
        } else {
            LogUtil.d(
                TAG,
                "gromore 开屏广告--> 准备调用show 失败 mTTSplashAd=${mTTSplashAd} isLoadSuccess=${isLoadSuccess}"
            )
            adStatusListener?.adFail()
        }
        // 状态重置
        isLoadSuccess = 0
    }

    /**
     * 广告是否加载成功
     */
    fun isSuccessStatus() = isLoadSuccess == 2

    val adInteractionListener = object : TTSplashAd.AdInteractionListener {
        override fun onAdClicked(p0: View?, p1: Int) {
            LogUtil.d(TAG, "gromore 开屏广告--> 7、onAdClicked ")
        }

        override fun onAdShow(p0: View?, p1: Int) {
            isShowAd = true
            LogUtil.d(TAG, "gromore 开屏广告--> 6、onAdShow ")
        }

        override fun onAdSkip() {
            LogUtil.d(TAG, "gromore 开屏广告--> 7、onAdSkip ")
            adStatusListener?.adClose(null)
        }

        override fun onAdTimeOver() {
            LogUtil.d(TAG, "gromore 开屏广告--> 7、onAdDismiss ")
            adStatusListener?.adClose(null)
        }
    }

    /**
     * 是否展示广告
     */
    fun getAdStatus() = isShowAd

}