package com.gem.media.ad;

//import static com.anythink.network.applovin.ApplovinATConst.DEBUGGER_CONFIG.Applovin_NETWORK;
//import static com.anythink.network.pangle.PangleATConst.DEBUGGER_CONFIG.Pangle_NETWORK;
//import static com.anythink.network.vungle.VungleATConst.DEBUGGER_CONFIG.Vungle_NETWORK;


//import static com.anythink.network.pangle.PangleATConst.DEBUGGER_CONFIG.Pangle_NETWORK;

import android.text.TextUtils;


//import com.anythink.core.api.ATDebuggerConfig;
import com.ads.cal.picturetranslate.PictureApplication;
import com.anythink.core.api.ATDebuggerConfig;
import com.anythink.core.api.ATSDK;
import com.anythink.core.api.ATSDKInitListener;
import com.gem.media.StatsStaticHelper;
import com.gem.media.splash.base.utils.ALog;
import com.gem.media.splash.base.utils.PROP;

import java.util.HashMap;

public class CpAdData {
    private static boolean isInit = false;

    private static final class CpAdDataHolder {
        static final CpAdData adManager = new CpAdData();
    }

    public static CpAdData getInstance() {
        return CpAdDataHolder.adManager;
    }

    private static HashMap<String, AdData> adDataHashMap = new HashMap<>();

    public static final String CP_TOPON = "topon";

    public void setCpData(String cp, AdData adData) {
        if (null == adDataHashMap) {
            adDataHashMap = new HashMap<>();
        }
        adDataHashMap.put(cp, adData);
    }

    public AdData getAdData(String cpId) {
        if (adDataHashMap == null) {
            adDataHashMap = new HashMap<>();
            return null;
        }
        return adDataHashMap.get(cpId);
    }

    public static String initTopOnAdSdk(String adAppId_channelId, String appKey) {
        if (!TextUtils.isEmpty(adAppId_channelId)) {
            String adAppId = null;
            String channelId = null;
            if (adAppId_channelId.contains("_")) {
                String[] s = adAppId_channelId.split("_");
                if (s.length > 0) {
                    adAppId = s[0];
                }
                if (s.length > 1) {
                    channelId = s[1];
                }
            } else {
                adAppId = adAppId_channelId;
            }
            initTopOnAdSdk(adAppId, appKey, channelId);
            return adAppId;
        }
        return "";
    }

    public static boolean adInitSuccess = false;
    public static void initTopOnAdSdk(String adAppId, String appKey, String channelId) {
        if (TextUtils.isEmpty(channelId)) {
            channelId = "20560708";
        }
        init(adAppId, appKey, channelId);
    }

    private static void init(String adAppId, String appKey, String channelId) {
        if (!TextUtils.isEmpty(adAppId) && !TextUtils.isEmpty(appKey) && !TextUtils.isEmpty(channelId)) {
            if (!isInit) {
                ALog.w("initTopOnAdSdk", "initTopOnAdSdk start adAppId = " + adAppId + " channelId = " + channelId);
//                ATSDK.setNetworkLogDebug(PROP.isLogEnabled());
                //5c72931-a53c-4449-b17b-022706322d67
//                ATSDK.setDebuggerConfig(PictureApplication.getApplication(), "748d98dc-455b-4aee-b85a-4f3db6c1b7dd", new ATDebuggerConfig.Builder(Pangle_NETWORK).build());

                ATSDK.setChannel(channelId);
                //初始化SDK
                ATSDK.init(PictureApplication.getApplication(), adAppId, appKey, null, new ATSDKInitListener() {
                    @Override
                    public void onSuccess() {
                        isInit = true;
                        adInitSuccess = true;
                        ALog.w("initTopOnAdSdk", "initTopOnAdSdk onSuccess");
                        StatsStaticHelper.onEvent("m_init_s");
                    }

                    @Override
                    public void onFail(String s) {
                        isInit = false;
                        adInitSuccess = false;
                        StatsStaticHelper.onEvent("m_init_f");
                    }
                });
                isInit = true;
            }
        }
    }

}
