package com.mints.goodmoney.manager;

import android.content.Context;
import android.view.View;
import android.widget.FrameLayout;

import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.TTAdConstant;
import com.bytedance.sdk.openadsdk.TTAdManager;
import com.bytedance.sdk.openadsdk.TTAdNative;
import com.bytedance.sdk.openadsdk.TTAppDownloadListener;
import com.bytedance.sdk.openadsdk.TTNativeExpressAd;
import com.mints.goodmoney.MintsApplication;

import java.util.List;

/**
 * 穿山甲banner
 */
public class TTNativeExpressManager {

    private TTAdNative mTTAdNative;
    private TTNativeExpressAd mTTAd;

    private TTNativeExpressManagerListener tTNativeExpressManagerListener;

    public void setTTNativeExpressManagerListener(TTNativeExpressManagerListener tTNativeExpressManagerListener) {
        this.tTNativeExpressManagerListener = tTNativeExpressManagerListener;
    }

    public interface TTNativeExpressManagerListener {
        void setTtExpressView(FrameLayout view);
    }

    /**
     * 穿山甲权限初始化
     */
    public TTAdNative initTTAd(boolean permission) {
        Context context= MintsApplication.getContext();
        //step1:初始化sdk
        TTAdManager ttAdManager = TtCsjAdManager.get();
        //step2:(可选，强烈建议在合适的时机调用):申请部分权限，如read_phone_state,防止获取不了imei时候，下载类广告没有填充的问题。
        if (permission) {
            TtCsjAdManager.get().requestPermissionIfNecessary(context);
        }
        //step3:创建TTAdNative对象,用于调用广告请求接口
        mTTAdNative = ttAdManager.createAdNative(context);
        return mTTAdNative;
    }

    /**
     * 获取穿山甲banner
     *
     * @param frameLayout
     * @param viewWidth
     * @param viewHeight
     */
    public void getNativeExpressInit(String codeId, FrameLayout frameLayout, float viewWidth, float viewHeight) {
         this.getTtNativeExpress( codeId, frameLayout, viewWidth, viewHeight);
    }


    /**
     * 获取穿山甲banner
     *
     * @param viewWidth
     * @param viewHeight
     */
    private void getTtNativeExpress(String codeId, FrameLayout view, float viewWidth, float viewHeight) {
        Context context= MintsApplication.getContext();
        //step4:创建广告请求参数AdSlot,具体参数含义参考文档
        AdSlot adSlot = new AdSlot.Builder()
                .setCodeId(codeId) //广告位id
                .setSupportDeepLink(true)
                .setAdCount(1) //请求广告数量为1到3条
                .setExpressViewAcceptedSize(viewWidth, viewHeight) //期望模板广告view的size,单位dp
                .setImageAcceptedSize(640, 320)//这个参数设置即可，不影响模板广告的size
                .build();
        //step5:请求广告，对请求回调的广告作渲染处理
        mTTAdNative.loadNativeExpressAd(adSlot, new TTAdNative.NativeExpressAdListener() {
            @Override
            public void onError(int code, String message) {

                if (view != null) {
                    view.removeAllViews();
                }

                if (tTNativeExpressManagerListener != null) {
                    tTNativeExpressManagerListener.setTtExpressView(null);
                }
            }

            @Override
            public void onNativeExpressAdLoad(List<TTNativeExpressAd> ads) {
                if (ads == null || ads.size() == 0) {
                    return;
                }
                mTTAd = ads.get(0);
                mTTAd.render();
                bindAdListener(mTTAd, view);


            }
        });
    }

    /**
     * 广告销毁
     */
    public void onTTAdDestroy() {
        if (mTTAd != null) {
            mTTAd.destroy();
        }
    }

    /**
     * banner广告事件
     *
     * @param ad
     * @param frameLayout
     */
    private void bindAdListener(TTNativeExpressAd ad, FrameLayout frameLayout) {
        ad.setExpressInteractionListener(new TTNativeExpressAd.ExpressAdInteractionListener() {
            @Override
            public void onAdClicked(View view, int type) {
            }

            @Override
            public void onAdShow(View view, int type) {
            }

            @Override
            public void onRenderFail(View view, String msg, int code) {
            }

            @Override
            public void onRenderSuccess(View view, float width, float height) {
                if (frameLayout != null) {
                    frameLayout.removeAllViews();
                    frameLayout.addView(view);

                    if (tTNativeExpressManagerListener != null) {
                        tTNativeExpressManagerListener.setTtExpressView(frameLayout);
                    }
                }
            }


        });
        //dislike设置
        if (ad.getInteractionType() != TTAdConstant.INTERACTION_TYPE_DOWNLOAD) {
            return;
        }
    }
}
