package com.duben.microtribe.ad.express

import android.util.Log
import android.view.View
import android.view.ViewGroup
import android.widget.*
import com.bytedance.sdk.openadsdk.*
import com.bytedance.sdk.openadsdk.mediation.ad.MediationExpressRenderListener
import com.duben.microtribe.BuildConfig
import com.duben.microtribe.MintsApplication
import com.duben.microtribe.utils.ForegroundOrBackground
import com.duben.microtribe.utils.LogUtil
import com.duben.microtribe.utils.UIUtils

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

/**
 * 信息流
 */
class MainExpressManager {

    companion object {
        val instance: MainExpressManager by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
            MainExpressManager()
        }
    }

    private var mTTFeedAdList = arrayListOf<TTFeedAd>()

    fun loadAd(expressAdCallback: ExpressAdCallback) {
        val activity = ForegroundOrBackground.getTopActivity()

        val adNativeLoader = TTAdSdk.getAdManager().createAdNative(activity)
        val adslot = AdSlot.Builder()
            .setCodeId(BuildConfig.GROMORE_EXPRESS_CODE)
            /**
             * 注：
             *  1:单位为px
             *  2:如果是信息流自渲染广告，设置广告图片期望的图片宽高 ，不能为0
             *  2:如果是信息流模板广告，宽度设置为希望的宽度，高度设置为0(0为高度选择自适应参数)
             */
//            .setImageAcceptedSize(
//                UIUtils.getAdWidth2(MintsApplication.getContext()).toInt(),
////                UIUtils.dip2px(MintsApplication.getContext(), 192f)
//                0
//            )
            .setImageAcceptedSize(UIUtils.getScreenWidth(activity), 0)
            .setAdCount(2)//请求广告数量为1到3条 （优先采用平台配置的数量）
            .build()

        adNativeLoader.loadFeedAd(adslot, object : TTAdNative.FeedAdListener {
            override fun onError(p0: Int, p1: String?) {
                LogUtil.e(TAG, "gromore首页信息流--> 3、load feed ad error : $p0, $p1")
            }

            override fun onFeedAdLoad(ads: MutableList<TTFeedAd>?) {
                LogUtil.d(
                    TAG,
                    "gromore首页信息流--> 3、Gromore  onAdLoaded"
                )
                ads?.let {
                    if (it.isEmpty()) {
                        return
                    }

                    for (ttFeedAd in it) {
                        mTTFeedAdList.add(ttFeedAd)
                        showAd(ttFeedAd, expressAdCallback)
                    }
                }
            }

        })
    }

    private fun showAd(
        ttNativeAd: TTFeedAd?,
        expressAdCallback: ExpressAdCallback
    ) {
        ttNativeAd?.let {
            if (it.mediationManager.isExpress) {
                //--------------信息流模板广告渲染----------------
                showExpressView(it, expressAdCallback)
            } else {
                //--------------信息流自渲染广告渲染----------------
//                val view: View? = when (it.imageMode) {
//                    TTAdConstant.IMAGE_MODE_SMALL_IMG -> getSmallAdView(frameLayout)//信息流自渲染广告渲染 ：小图广告
//                    TTAdConstant.IMAGE_MODE_LARGE_IMG -> getLargeAdView(frameLayout)//信息流自渲染广告渲染 ：大图广告
//                    TTAdConstant.IMAGE_MODE_GROUP_IMG -> getGroupAdView(frameLayout)//信息流自渲染广告渲染 ：组图广告
//                    TTAdConstant.IMAGE_MODE_VIDEO -> getVideoView(frameLayout)//信息流自渲染广告渲染 ：视频广告
//                    TTAdConstant.IMAGE_MODE_VERTICAL_IMG -> getVerticalAdView(frameLayout)//信息流自渲染广告渲染 ：竖图广告
//                    TTAdConstant.IMAGE_MODE_VIDEO_VERTICAL -> getVideoView(frameLayout)//信息流自渲染广告渲染 ：竖版视频广告
//                    else -> {
//                        Log.i(TAG, "展示样式错误")
//                        null
//                    }
//                }
//                view?.let {
//                    it.layoutParams = ViewGroup.LayoutParams(
//                        ViewGroup.LayoutParams.MATCH_PARENT,
//                        ViewGroup.LayoutParams.MATCH_PARENT
//                    )
//                    frameLayout?.removeAllViews()
//                    frameLayout?.addView(it)
//                }
            }
        }
    }

    private fun showExpressView(it: TTFeedAd, expressAdCallback: ExpressAdCallback) {
        it.setExpressRenderListener(object : MediationExpressRenderListener {
            override fun onRenderSuccess(p0: View?, p1: Float, p2: Float, p3: Boolean) {
                Log.i(TAG, "onRenderSuccess")
                it.setDislikeCallback(ForegroundOrBackground.getTopActivity(),
                    object : TTAdDislike.DislikeInteractionCallback {
                        override fun onShow() {
                            Log.i(TAG, "express dislike 点击show")
                        }

                        override fun onSelected(position: Int, value: String?, enforce: Boolean) {
                            Log.i(TAG, "express 点击 $value")
                        }

                        override fun onCancel() {
                            Log.i(TAG, "express dislike 点击了取消")
                        }
                    })
                it.adView?.let { view ->
                    if (view.parent != null) {
                        (view.parent as ViewGroup).removeView(view)
                    }
                    val frameLayout = FrameLayout(MintsApplication.getContext())
                    frameLayout.addView(view)
                    expressAdCallback.renderSuccess(frameLayout)
                }
            }

            override fun onRenderFail(p0: View?, p1: String?, p2: Int) {
                LogUtil.e(TAG, "gromore首页信息流--> 5、展示广告时失败了，onRenderFail msg=${p1} code=${p2} ")
            }

            override fun onAdClick() {
                Log.i(TAG, "onAdClick")
            }

            override fun onAdShow() {
                Log.i(TAG, "onAdShow")
                Log.i(TAG, "ad mediaExtraInfo ${it.mediaExtraInfo}")
            }

        })
        it.render()//必须要调用render方法进行渲染，在onRenderSuccess中处理模板的view
    }

    /**
     * 销毁
     */
    fun destroy() {
        for (ttFeedAd in mTTFeedAdList) {
            ttFeedAd.destroy()
        }
        mTTFeedAdList.clear()
    }

//    private fun getSmallAdView(parent: ViewGroup?): View? {
//        val convertView =
//            LayoutInflater.from(ForegroundOrBackground.getTopActivity())
//                .inflate(
//                    R.layout.mediation_listitem_ad_small_pic,
//                    parent,
//                    false
//                )
//        val adViewHolder = SmallAdViewHolder()
//        adViewHolder.mTitle =
//            convertView.findViewById<View>(R.id.tv_listitem_ad_title) as TextView
//        adViewHolder.mSource =
//            convertView.findViewById<View>(R.id.tv_listitem_ad_source) as TextView
//        adViewHolder.mDescription =
//            convertView.findViewById<View>(R.id.tv_listitem_ad_desc) as TextView
//        adViewHolder.mSmallImage =
//            convertView.findViewById<View>(R.id.iv_listitem_image) as ImageView
//        adViewHolder.mIcon =
//            convertView.findViewById<View>(R.id.iv_listitem_icon) as ImageView
//        adViewHolder.mDislike =
//            convertView.findViewById<View>(R.id.iv_listitem_dislike) as ImageView
//        adViewHolder.mCreativeButton =
//            convertView.findViewById<View>(R.id.btn_listitem_creative) as Button
//
//        adViewHolder.app_info =
//            convertView.findViewById<LinearLayout>(R.id.app_info)
//        adViewHolder.app_name =
//            convertView.findViewById<TextView>(R.id.app_name)
//        adViewHolder.author_name =
//            convertView.findViewById<TextView>(R.id.author_name)
//        adViewHolder.package_size =
//            convertView.findViewById<TextView>(R.id.package_size)
//        adViewHolder.permissions_url =
//            convertView.findViewById<TextView>(R.id.permissions_url)
//        adViewHolder.permissions_content =
//            convertView.findViewById<TextView>(R.id.permissions_content)
//        adViewHolder.privacy_agreement =
//            convertView.findViewById<TextView>(R.id.privacy_agreement)
//        adViewHolder.version_name =
//            convertView.findViewById<TextView>(R.id.version_name)
//
//        val viewBinder =
//            MediationViewBinder.Builder(R.layout.mediation_listitem_ad_small_pic)
//                .titleId(R.id.tv_listitem_ad_title)
//                .sourceId(R.id.tv_listitem_ad_source).descriptionTextId(
//                    R.id.tv_listitem_ad_desc
//                )
//                .mainImageId(R.id.iv_listitem_image)
//                .logoLayoutId(R.id.tt_ad_logo)
//                .callToActionId(R.id.btn_listitem_creative)
//                .iconImageId(R.id.iv_listitem_icon).build()
//        adViewHolder.viewBinder = viewBinder
//        bindData(convertView, adViewHolder)
//        if (mTTFeedAd?.imageList?.size != 0) {
//            mTTFeedAd?.let { ttfeedad ->
//                ttfeedad.imageList[0]?.imageUrl?.let {
//                    Glide.with(ForegroundOrBackground.getTopActivity()).load(it)
//                        .into(adViewHolder.mSmallImage!!)
//                }
//            }
//        }
//        return convertView;
//    }

//    private fun getLargeAdView(parent: ViewGroup?): View {
//        val adViewHolder = LargeAdViewHolder()
//        var convertView: View =
//            LayoutInflater.from(ForegroundOrBackground.getTopActivity())
//                .inflate(
//                    R.layout.mediation_listitem_ad_large_pic,
//                    parent,
//                    false
//                )
//        adViewHolder.mTitle =
//            convertView.findViewById<View>(R.id.tv_listitem_ad_title) as TextView
//        adViewHolder.mDescription =
//            convertView.findViewById<View>(R.id.tv_listitem_ad_desc) as TextView
//        adViewHolder.mSource =
//            convertView.findViewById<View>(R.id.tv_listitem_ad_source) as TextView
//        adViewHolder.mLargeImage =
//            convertView.findViewById<View>(R.id.iv_listitem_image) as ImageView
//        adViewHolder.mIcon =
//            convertView.findViewById<View>(R.id.iv_listitem_icon) as ImageView
//        adViewHolder.mDislike =
//            convertView.findViewById<View>(R.id.iv_listitem_dislike) as ImageView
//        adViewHolder.mCreativeButton =
//            convertView.findViewById<View>(R.id.btn_listitem_creative) as Button
//        adViewHolder.mLogo =
//            convertView.findViewById<RelativeLayout>(R.id.tt_ad_logo) //logoView 建议传入GroupView类型
//
//
//        adViewHolder.app_info =
//            convertView.findViewById<LinearLayout>(R.id.app_info)
//        adViewHolder.app_name =
//            convertView.findViewById<TextView>(R.id.app_name)
//        adViewHolder.author_name =
//            convertView.findViewById<TextView>(R.id.author_name)
//        adViewHolder.package_size =
//            convertView.findViewById<TextView>(R.id.package_size)
//        adViewHolder.permissions_url =
//            convertView.findViewById<TextView>(R.id.permissions_url)
//        adViewHolder.permissions_content =
//            convertView.findViewById<TextView>(R.id.permissions_content)
//        adViewHolder.privacy_agreement =
//            convertView.findViewById<TextView>(R.id.privacy_agreement)
//        adViewHolder.version_name =
//            convertView.findViewById<TextView>(R.id.version_name)
//
//        val viewBinder =
//            MediationViewBinder.Builder(R.layout.mediation_listitem_ad_large_pic)
//                .titleId(R.id.tv_listitem_ad_title)
//                .descriptionTextId(R.id.tv_listitem_ad_desc).sourceId(
//                    R.id.tv_listitem_ad_source
//                )
//                .mainImageId(R.id.iv_listitem_image)
//                .callToActionId(R.id.btn_listitem_creative)
//                .logoLayoutId(R.id.tt_ad_logo)
//                .iconImageId(R.id.iv_listitem_icon).build()
//        adViewHolder.viewBinder = viewBinder
//        bindData(convertView, adViewHolder)
//        if (mTTFeedAd?.imageList?.size != 0) {
//            mTTFeedAd?.let { ttfeedad ->
//                ttfeedad.imageList[0]?.imageUrl?.let {
//                    Glide.with(ForegroundOrBackground.getTopActivity()).load(it)
//                        .into(adViewHolder.mLargeImage!!)
//                }
//            }
//        }
//        return convertView
//    }

//    private fun getGroupAdView(parent: ViewGroup?): View? {
//        val adViewHolder = GroupAdViewHolder()
//        var convertView: View = LayoutInflater.from(ForegroundOrBackground.getTopActivity())
//            .inflate(
//                R.layout.mediation_listitem_ad_group_pic,
//                parent,
//                false
//            )
//        adViewHolder.mTitle =
//            convertView.findViewById<View>(R.id.tv_listitem_ad_title) as TextView
//        adViewHolder.mSource =
//            convertView.findViewById<View>(R.id.tv_listitem_ad_source) as TextView
//        adViewHolder.mDescription =
//            convertView.findViewById<View>(R.id.tv_listitem_ad_desc) as TextView
//        adViewHolder.mGroupImage1 =
//            convertView.findViewById<View>(R.id.iv_listitem_image1) as ImageView
//        adViewHolder.mGroupImage2 =
//            convertView.findViewById<View>(R.id.iv_listitem_image2) as ImageView
//        adViewHolder.mGroupImage3 =
//            convertView.findViewById<View>(R.id.iv_listitem_image3) as ImageView
//        adViewHolder.mIcon =
//            convertView.findViewById<View>(R.id.iv_listitem_icon) as ImageView
//        adViewHolder.mDislike =
//            convertView.findViewById<View>(R.id.iv_listitem_dislike) as ImageView
//        adViewHolder.mCreativeButton =
//            convertView.findViewById<View>(R.id.btn_listitem_creative) as Button
//        adViewHolder.mLogo =
//            convertView.findViewById<RelativeLayout>(R.id.tt_ad_logo) //logoView 建议传入GroupView类型
//
//
//        adViewHolder.app_info =
//            convertView.findViewById<LinearLayout>(R.id.app_info)
//        adViewHolder.app_name =
//            convertView.findViewById<TextView>(R.id.app_name)
//        adViewHolder.author_name =
//            convertView.findViewById<TextView>(R.id.author_name)
//        adViewHolder.package_size =
//            convertView.findViewById<TextView>(R.id.package_size)
//        adViewHolder.permissions_url =
//            convertView.findViewById<TextView>(R.id.permissions_url)
//        adViewHolder.permissions_content =
//            convertView.findViewById<TextView>(R.id.permissions_content)
//        adViewHolder.privacy_agreement =
//            convertView.findViewById<TextView>(R.id.privacy_agreement)
//        adViewHolder.version_name =
//            convertView.findViewById<TextView>(R.id.version_name)
//
//        val viewBinder =
//            MediationViewBinder.Builder(R.layout.mediation_listitem_ad_group_pic)
//                .titleId(R.id.tv_listitem_ad_title)
//                .descriptionTextId(R.id.tv_listitem_ad_desc).sourceId(
//                    R.id.tv_listitem_ad_source
//                )
//                .mainImageId(R.id.iv_listitem_image1)
//                .logoLayoutId(R.id.tt_ad_logo).callToActionId(
//                    R.id.btn_listitem_creative
//                ).iconImageId(R.id.iv_listitem_icon)
//                .groupImage1Id(R.id.iv_listitem_image1).groupImage2Id(
//                    R.id.iv_listitem_image2
//                )
//                .groupImage3Id(R.id.iv_listitem_image3).build()
//        adViewHolder.viewBinder = viewBinder
//
//        bindData(convertView, adViewHolder)
//        if (mTTFeedAd?.imageList?.size ?: 0 >= 3) {
//            val image1: String? = mTTFeedAd?.imageList?.get(0)?.imageUrl
//            val image2: String? = mTTFeedAd?.imageList?.get(1)?.imageUrl
//            val image3: String? = mTTFeedAd?.imageList?.get(2)?.imageUrl
//            if (image1 != null) {
//                Glide.with(ForegroundOrBackground.getTopActivity()).load(image1)
//                    .into(adViewHolder.mGroupImage1!!)
//            }
//            if (image2 != null) {
//                Glide.with(ForegroundOrBackground.getTopActivity()).load(image2)
//                    .into(adViewHolder.mGroupImage2!!)
//            }
//            if (image3 != null) {
//                Glide.with(ForegroundOrBackground.getTopActivity()).load(image3)
//                    .into(adViewHolder.mGroupImage3!!)
//            }
//        }
//        return convertView
//    }

//    private fun getVideoView(parent: ViewGroup?): View? {
//        val adViewHolder = VideoAdViewHolder()
//        var convertView: View? = null
//        try {
//            convertView = LayoutInflater.from(ForegroundOrBackground.getTopActivity())
//                .inflate(
//                    R.layout.mediation_listitem_ad_large_video,
//                    parent,
//                    false
//                )
//            adViewHolder.mTitle =
//                convertView.findViewById<View>(R.id.tv_listitem_ad_title) as TextView
//            adViewHolder.mDescription =
//                convertView.findViewById<View>(R.id.tv_listitem_ad_desc) as TextView
//            adViewHolder.mSource =
//                convertView.findViewById<View>(R.id.tv_listitem_ad_source) as TextView
//            adViewHolder.videoView =
//                convertView.findViewById<View>(R.id.iv_listitem_video) as FrameLayout
//            adViewHolder.mIcon =
//                convertView.findViewById<View>(R.id.iv_listitem_icon) as ImageView
//            adViewHolder.mDislike =
//                convertView.findViewById<View>(R.id.iv_listitem_dislike) as ImageView
//            adViewHolder.mCreativeButton =
//                convertView.findViewById<View>(R.id.btn_listitem_creative) as Button
//            adViewHolder.mLogo =
//                convertView.findViewById<RelativeLayout>(R.id.tt_ad_logo) //logoView 建议传入GroupView类型
//            adViewHolder.app_info =
//                convertView.findViewById<LinearLayout>(R.id.app_info)
//            adViewHolder.app_name =
//                convertView.findViewById<TextView>(R.id.app_name)
//            adViewHolder.author_name =
//                convertView.findViewById<TextView>(R.id.author_name)
//            adViewHolder.package_size =
//                convertView.findViewById<TextView>(R.id.package_size)
//            adViewHolder.permissions_url =
//                convertView.findViewById<TextView>(R.id.permissions_url)
//            adViewHolder.permissions_content =
//                convertView.findViewById<TextView>(R.id.permissions_content)
//            adViewHolder.privacy_agreement =
//                convertView.findViewById<TextView>(R.id.privacy_agreement)
//            adViewHolder.version_name =
//                convertView.findViewById<TextView>(R.id.version_name)
//
//            val viewBinder =
//                MediationViewBinder.Builder(R.layout.mediation_listitem_ad_large_video)
//                    .titleId(R.id.tv_listitem_ad_title)
//                    .sourceId(R.id.tv_listitem_ad_source)
//                    .descriptionTextId(R.id.tv_listitem_ad_desc)
//                    .mediaViewIdId(
//                        R.id.iv_listitem_video
//                    )
//                    .callToActionId(R.id.btn_listitem_creative)
//                    .logoLayoutId(R.id.tt_ad_logo)
//                    .iconImageId(R.id.iv_listitem_icon).build()
//            adViewHolder.viewBinder = viewBinder
//
//            // 如果自己有需求进行视频播放操作，则按如下步骤进行。注意：不是所有adn都支持，因此需要进行判空操作。
////            mTTFeedAd?.csjmAdInfo?.setUseCustomVideo(true) // 注意：要在registerView之前调用
//            val videoUrl: String? = mTTFeedAd?.customVideo?.videoUrl // 注意：不是所有adn都支持，这里务必要进行判空操作
//            if (!TextUtils.isEmpty(videoUrl)) {
//                // 如果通过getVideoUrl返回的url不是空，则自己处理视频播放操作，并通过reporter上报视频状态
//                playAdVideo(adViewHolder.videoView as FrameLayout, videoUrl ?: "")
//            } else {
//                // 注意：即使该adn支持返回url，但某次偶然原因返回的videoUrl为空，则仍然会用adn自己的视频播放作为兜底
//            }
//
//            //视频广告设置播放状态回调（可选）
//            mTTFeedAd?.setVideoAdListener(object : TTFeedAd.VideoAdListener {
//                override fun onVideoLoad(ad: TTFeedAd?) {
//                    Log.i(TAG, "onVideoLoad")
//                }
//
//                override fun onVideoError(errorCode: Int, extraCode: Int) {
//                    Log.i(TAG, "onVideoError")
//                }
//
//                override fun onVideoAdStartPlay(ad: TTFeedAd?) {
//                    Log.i(TAG, "onVideoAdStartPlay")
//                }
//
//                override fun onVideoAdPaused(ad: TTFeedAd?) {
//                    Log.i(TAG, "onVideoAdPaused")
//                }
//
//                override fun onVideoAdContinuePlay(ad: TTFeedAd?) {
//                    Log.i(TAG, "onVideoAdContinuePlay")
//                }
//
//                override fun onProgressUpdate(current: Long, duration: Long) {
//                    Log.i(TAG, "onProgressUpdate")
//                }
//
//                override fun onVideoAdComplete(ad: TTFeedAd?) {
//                    Log.i(TAG, "onVideoAdComplete")
//                }
//
//            })
//            mTTFeedAd?.setDownloadListener(object : TTAppDownloadListener {
//                override fun onIdle() {
//                    Log.i(TAG, "setDownloadListener")
//                }
//
//                override fun onDownloadActive(
//                    totalBytes: Long,
//                    currBytes: Long,
//                    fileName: String?,
//                    appName: String?
//                ) {
//                    Log.i(TAG, "onDownloadActive")
//                }
//
//                override fun onDownloadPaused(
//                    totalBytes: Long,
//                    currBytes: Long,
//                    fileName: String?,
//                    appName: String?
//                ) {
//                    Log.i(TAG, "onDownloadPaused")
//                }
//
//                override fun onDownloadFailed(
//                    totalBytes: Long,
//                    currBytes: Long,
//                    fileName: String?,
//                    appName: String?
//                ) {
//                    Log.i(TAG, "onDownloadFailed")
//                }
//
//                override fun onDownloadFinished(
//                    totalBytes: Long,
//                    fileName: String?,
//                    appName: String?
//                ) {
//                    Log.i(TAG, "onDownloadFinished")
//                }
//
//                override fun onInstalled(fileName: String?, appName: String?) {
//                    Log.i(TAG, "onInstalled")
//                }
//
//            })
//            //绑定广告数据、设置交互回调
//            bindData(convertView, adViewHolder)
//        } catch (e: Exception) {
//            e.printStackTrace()
//        }
//
//        return convertView
//    }

//    private var mVideoView: VideoView? = null
//    private var customVideoReporter: TTFeedAd.CustomizeVideo? = null

//    private fun playAdVideo(videoContainer: ViewGroup, videoUrl: String) {
//        mVideoView = VideoView(MintsApplication.getContext())
//        videoContainer.addView(mVideoView)
//        customVideoReporter = mTTFeedAd?.customVideo
//        // 开发者根据自己的需求，做相应的视频控制界面，播放暂停等
//        mVideoView?.setVideoURI(Uri.parse(videoUrl))
//        mVideoView?.setOnErrorListener { mp, what, extra ->
//            if (customVideoReporter != null) {
//                customVideoReporter?.reportVideoError(
//                    mVideoView!!.currentPosition.toLong(),
//                    what,
//                    extra
//                )
//            }
//            false
//        }
//        mVideoView?.setOnCompletionListener {
//            if (customVideoReporter != null) {
//                customVideoReporter?.reportVideoFinish()
//            }
//        }
//        mVideoView?.start()
//        customVideoReporter?.reportVideoStart()
//    }

//    private fun getVerticalAdView(parent: ViewGroup?): View? {
//        val adViewHolder = VerticalAdViewHolder()
//        var convertView = LayoutInflater.from(ForegroundOrBackground.getTopActivity())
//            .inflate(
//                R.layout.mediation_listitem_ad_vertical_pic,
//                parent,
//                false
//            )
//        adViewHolder.mTitle =
//            convertView.findViewById<View>(R.id.tv_listitem_ad_title) as TextView
//        adViewHolder.mSource =
//            convertView.findViewById<View>(R.id.tv_listitem_ad_source) as TextView
//        adViewHolder.mDescription =
//            convertView?.findViewById<View>(R.id.tv_listitem_ad_desc) as TextView
//        adViewHolder.mVerticalImage =
//            convertView.findViewById<ImageView>(R.id.iv_listitem_image)
//        adViewHolder.mIcon =
//            convertView.findViewById<View>(R.id.iv_listitem_icon) as ImageView
//        adViewHolder.mDislike =
//            convertView.findViewById<View>(R.id.iv_listitem_dislike) as ImageView
//        adViewHolder.mCreativeButton =
//            convertView.findViewById<View>(R.id.btn_listitem_creative) as Button
////        adViewHolder.mLogo =
////            convertView.findViewById<RelativeLayout>(R.id.tt_ad_logo) //logoView 建议传入GroupView类型
//
//
//        adViewHolder.app_info =
//            convertView.findViewById<LinearLayout>(R.id.app_info)
//        adViewHolder.app_name =
//            convertView.findViewById<TextView>(R.id.app_name)
//        adViewHolder.author_name =
//            convertView.findViewById<TextView>(R.id.author_name)
//        adViewHolder.package_size =
//            convertView.findViewById<TextView>(R.id.package_size)
//        adViewHolder.permissions_url =
//            convertView.findViewById<TextView>(R.id.permissions_url)
//        adViewHolder.permissions_content =
//            convertView.findViewById<TextView>(R.id.permissions_content)
//        adViewHolder.privacy_agreement =
//            convertView.findViewById<TextView>(R.id.privacy_agreement)
//        adViewHolder.version_name =
//            convertView.findViewById<TextView>(R.id.version_name)
//
//        val viewBinder =
//            MediationViewBinder.Builder(R.layout.mediation_listitem_ad_vertical_pic)
//                .titleId(R.id.tv_listitem_ad_title)
//                .descriptionTextId(R.id.tv_listitem_ad_desc)
//                .mainImageId(R.id.iv_listitem_image)
//                .iconImageId(R.id.iv_listitem_icon)
//                .callToActionId(R.id.btn_listitem_creative)
//                .sourceId(R.id.tv_listitem_ad_source)
//                .logoLayoutId(R.id.tt_ad_logo) //logoView 建议传入GroupView类型
//                .build()
//        adViewHolder.viewBinder = viewBinder
//        bindData(convertView, adViewHolder)
//        if (mTTFeedAd?.imageList?.size != 0) {
//            mTTFeedAd?.let { ttfeedad ->
//                ttfeedad.imageList[0]?.imageUrl?.let {
//                    Glide.with(ForegroundOrBackground.getTopActivity()).load(it)
//                        .into(adViewHolder.mVerticalImage!!)
//                }
//            }
//        }
//        return convertView
//    }

//    private fun bindData(convertView: View, adViewHolder: AdViewHolder) {
//        mTTFeedAd?.let {
//            if (it.mediationManager?.hasDislike() == true) {
//                val ttAdDislike: TTAdDislike =
//                    it.getDislikeDialog(ForegroundOrBackground.getTopActivity())
//                adViewHolder.mDislike?.visibility = View.VISIBLE
//                adViewHolder.mDislike?.setOnClickListener(View.OnClickListener {
//                    //使用接口来展示
//                    ttAdDislike.showDislikeDialog()
//                    ttAdDislike.setDislikeInteractionCallback(object :
//                        TTAdDislike.DislikeInteractionCallback {
//                        override fun onShow() {
//                            Log.i(TAG, "dislike 点击show")
//                        }
//
//                        override fun onSelected(position: Int, value: String?, enforce: Boolean) {
//                            Log.i(TAG, "点击 $value")
//                            //用户选择不喜欢原因后，移除广告展示
////                            frameLayout?.removeAllViews()
//                        }
//
//                        override fun onCancel() {
//                            Log.i(TAG, "dislike 点击了取消")
//                        }
//
//                    })
//                })
//            } else {
//                if (adViewHolder.mDislike != null) adViewHolder.mDislike?.visibility = View.GONE
//            }
//
//            setDownLoadAppInfo(it, adViewHolder)
//
//            //可以被点击的view, 也可以把convertView放进来意味item可被点击
//            val clickViewList: MutableList<View?> = ArrayList()
//            clickViewList.add(convertView)
//            clickViewList.add(adViewHolder.mSource)
//            clickViewList.add(adViewHolder.mTitle)
//            clickViewList.add(adViewHolder.mDescription)
//            clickViewList.add(adViewHolder.mIcon)
//
//            //添加点击区域
//            if (adViewHolder is LargeAdViewHolder) {
//                clickViewList.add(adViewHolder.mLargeImage)
//            } else if (adViewHolder is SmallAdViewHolder) {
//                clickViewList.add(adViewHolder.mSmallImage)
//            } else if (adViewHolder is VerticalAdViewHolder) {
//                clickViewList.add(adViewHolder.mVerticalImage)
//            } else if (adViewHolder is VideoAdViewHolder) {
//                clickViewList.add(adViewHolder.videoView)
//            } else if (adViewHolder is GroupAdViewHolder) {
//                clickViewList.add(adViewHolder.mGroupImage1)
//                clickViewList.add(adViewHolder.mGroupImage2)
//                clickViewList.add(adViewHolder.mGroupImage3)
//            }
//
//            //触发创意广告的view（点击下载或拨打电话）
//            val creativeViewList: MutableList<View?> = ArrayList()
//            creativeViewList.add(adViewHolder.mCreativeButton)
//            //重要! 这个涉及到广告计费，必须正确调用。**** convertView必须是com.bytedance.msdk.api.format.TTNativeAdView ****
//            it.registerViewForInteraction(
//                ForegroundOrBackground.getTopActivity(),
//                convertView as ViewGroup,
//                clickViewList,
//                creativeViewList,
//                null,
//                object : TTNativeAd.AdInteractionListener {
//                    override fun onAdClicked(view: View?, ad: TTNativeAd?) {
//                        Log.i(TAG, "onAdClicked")
//                    }
//
//                    override fun onAdCreativeClick(view: View?, ad: TTNativeAd?) {
//                        Log.i(TAG, "onAdCreativeClick")
//                    }
//
//                    override fun onAdShow(ad: TTNativeAd?) {
//                        Log.i(TAG, "onAdShow")
//                        ad?.let {
////                            PrintUtil.printShowInfo(ad.mediationManager)
//                        }
//                    }
//                },
//                adViewHolder.viewBinder
//            )
//
//            adViewHolder.mTitle?.text = it.title //title为广告的简单信息提示
//
//            adViewHolder.mDescription?.text = it.description //description为广告的较长的说明
//
//            adViewHolder.mSource?.text =
//                if (TextUtils.isEmpty(it.source)) "广告来源" else it.source
//
//            val icon: TTImage? = it.icon
//            if (icon != null) {
//                Glide.with(ForegroundOrBackground.getTopActivity()).load(icon.imageUrl)
//                    .into(adViewHolder.mIcon!!)
//            }
//            val adCreativeButton = adViewHolder.mCreativeButton
//            when (it.interactionType) {
//                TTAdConstant.INTERACTION_TYPE_DOWNLOAD -> {
//                    adCreativeButton?.visibility = View.VISIBLE;
//                    adCreativeButton?.setText(it.buttonText ?: "立即下载")
//                }
//                TTAdConstant.INTERACTION_TYPE_DIAL -> {
//                    adCreativeButton?.visibility = View.VISIBLE;
//                    adCreativeButton?.setText("立即拨打");
//                }
//                TTAdConstant.INTERACTION_TYPE_LANDING_PAGE,
//                TTAdConstant.INTERACTION_TYPE_BROWSER -> {
//                    adCreativeButton?.visibility = View.VISIBLE;
//                    adCreativeButton?.setText(it.buttonText ?: "查看详情")
//                }
//                else -> {
//                    adCreativeButton?.visibility = View.GONE
//                    Log.i(TAG, "交互类型异常")
//                }
//            }
//        }
//    }

//    private fun setDownLoadAppInfo(ttNativeAd: TTFeedAd, adViewHolder: AdViewHolder) {
//        if (ttNativeAd.complianceInfo == null) {
//            adViewHolder.app_info?.visibility = View.GONE
//        } else {
//            adViewHolder.app_info?.visibility = View.VISIBLE
//            val appInfo = ttNativeAd.complianceInfo
//            adViewHolder.app_name?.text = "应用名称：" + appInfo.appName
//            adViewHolder.author_name?.text = "开发者：" + appInfo.developerName
//            adViewHolder.package_size?.text = "包大小：不支持"
//            adViewHolder.permissions_url?.text = "权限url:不支持"
//            adViewHolder.privacy_agreement?.text = "隐私url：" + appInfo.privacyUrl
//            adViewHolder.version_name?.text = "版本号：" + appInfo.appVersion
//            adViewHolder.permissions_content!!.text =
//                "权限内容:" + getPermissionsContent(appInfo.permissionsMap)
//        }
//    }
//
//    private fun getPermissionsContent(permissionsMap: Map<String, String>?): String {
//        if (permissionsMap == null) {
//            return ""
//        }
//        val stringBuffer = StringBuffer()
//        val keyList = permissionsMap.keys
//        for (s in keyList) {
//            stringBuffer.append(
//                """$s : ${permissionsMap[s]} """
//            )
//        }
//        return stringBuffer.toString()
//    }
//
//    private class VideoAdViewHolder : AdViewHolder() {
//        var videoView: FrameLayout? = null
//    }
//
//    private class LargeAdViewHolder : AdViewHolder() {
//        var mLargeImage: ImageView? = null
//    }
//
//    private class SmallAdViewHolder : AdViewHolder() {
//        var mSmallImage: ImageView? = null
//    }
//
//    private class VerticalAdViewHolder : AdViewHolder() {
//        var mVerticalImage: ImageView? = null
//    }
//
//    private class GroupAdViewHolder : AdViewHolder() {
//        var mGroupImage1: ImageView? = null
//        var mGroupImage2: ImageView? = null
//        var mGroupImage3: ImageView? = null
//    }
//
//    private class ExpressAdViewHolder {
//        var mAdContainerView: FrameLayout? = null
//    }
//
//    open private class AdViewHolder {
//        var viewBinder: MediationViewBinder? = null
//        var mIcon: ImageView? = null
//        var mDislike: ImageView? = null
//        var mCreativeButton: Button? = null
//        var mTitle: TextView? = null
//        var mDescription: TextView? = null
//        var mSource: TextView? = null
//        var mLogo: RelativeLayout? = null
//        var app_info: LinearLayout? = null
//        var app_name: TextView? = null
//        var author_name: TextView? = null
//        var package_size: TextView? = null
//        var permissions_url: TextView? = null
//        var privacy_agreement: TextView? = null
//        var version_name: TextView? = null
//        var permissions_content: TextView? = null
//    }
}