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

import android.content.Context
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import androidx.appcompat.widget.AppCompatImageView
import androidx.appcompat.widget.AppCompatTextView
import androidx.core.view.isVisible
import com.bytedance.sdk.openadsdk.*
import com.composition.android.ad.csj.R
import com.composition.android.lib.ad.basic.AdResult
import com.composition.android.lib.ad.basic.BasicAdView
import com.composition.android.lib.ad.basic.NativeAdViewHolder

/**
 * @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?, p1: Float, p2: Float) {
                }

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

            })
            ttNativeExpressAd.render()
            (adRootView as? ViewGroup)?.let {
                it.removeAllViews()
                it.addView(ttNativeExpressAd.expressAdView)
            }
        }
    }

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