package com.composition.android.ad.csj.load.view

import android.content.Context
import android.view.View
import android.view.ViewGroup
import com.bytedance.sdk.openadsdk.*
import com.composition.android.lib.ad.AdManager
import com.composition.android.lib.ad.basic.AdResult
import com.composition.android.lib.ad.basic.BasicAdView
import com.composition.android.lib.ad.basic.NativeAdViewHolder
import com.composition.android.lib.ad.util.adLogE
import com.convenient.android.common.extension.getScreenWidth

/**
 * @classname:
 * @author: LiuXiaoLong
 * @date: 2022/9/13
 * description:
 */
class CSJNativeAdView(context: Context?) : BasicAdView<View>(context) {

    private var viewHolder: NativeAdViewHolder? = null

    fun setViewHolder(viewHolder: NativeAdViewHolder) {
        this.viewHolder = viewHolder
    }

    override fun adContentView(): View? {

        if (adResult == null || viewHolder == null) {
            return null
        }
        populateNativeAdView(context, adResult = adResult!!, viewHolder = viewHolder!!, null)
        val ttNativeExpressAd = adResult?.adObject as TTNativeExpressAd
        return ttNativeExpressAd.expressAdView
    }

    override fun onResume() {
    }

    override fun onPause() {

    }

    override fun onDestroy() {
        val ttNativeExpressAd = adResult?.adObject as? TTNativeExpressAd
        ttNativeExpressAd?.destroy()
    }

    companion object {

        fun populateNativeAdView(context: Context, adResult: AdResult.Success, viewHolder: NativeAdViewHolder, adRootView : View?) {
            val ttNativeExpressAd = adResult.adObject as TTNativeExpressAd
            viewHolder.activity?.let {
                ttNativeExpressAd.setDislikeCallback(it, object : TTAdDislike.DislikeInteractionCallback {
                    override fun onShow() {

                    }

                    override fun onSelected(p0: Int, p1: String?, p2: Boolean) {
                        viewHolder.adListener?.onAdDisLike()
                    }

                    override fun onCancel() {
                    }

                })
            }

            ttNativeExpressAd.setExpressInteractionListener(object : TTNativeExpressAd.AdInteractionListener {
                override fun onAdClicked(p0: View?, p1: Int) {
                    viewHolder.adListener?.onAdClick()
                }

                override fun onAdShow(p0: View?, p1: Int) {
                    viewHolder.adListener?.onAdShow(adResult.adBean)

                }

                override fun onRenderFail(p0: View?, p1: String?, p2: Int) {
                    viewHolder.adListener?.onAdShowFail("穿山甲原生模板广告渲染失败")
                }

                override fun onRenderSuccess(p0: View?, width: Float, height: Float) {
                    adLogE(AdManager.TAG, "穿山甲原生广告渲染成功width:${width},height:${height}")
                    (adRootView as? ViewGroup)?.let {
                        val sWidth = context.getScreenWidth()
                        val sHeight = (sWidth * height / width).toInt()
                        it.removeAllViews()
                        it.addView(ttNativeExpressAd.expressAdView, sWidth,sHeight)
                    }
                }

                override fun onAdDismiss() {
                    viewHolder.adListener?.onAdClose()
                }
            })
            ttNativeExpressAd.render()

        }
    }

    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        adResult?.adObject = null
    }
}