package com.gem.media.ad;


import android.content.Context;
import android.text.TextUtils;
import android.util.Log;


import com.ads.cal.picturetranslate.PictureApplication;
import com.adsurge.adn.ads.AdSurgeAdError;
import com.adsurge.adn.ads.AdSurgePrivacyConfiguration;
import com.adsurge.adn.managers.AdSurgeAdSdk;
import com.adsurge.adn.managers.AdSurgeAdSdkInitConfig;
import com.adsurge.adn.managers.OnStartListener;
import com.gem.media.InitA;
import com.gem.media.StatsStaticHelper;
import com.gem.media.splash.base.utils.ALog;

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_ADMOB = "admob";
    public static final String CP_ADSURGE = "adsurge";


    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);
    }

    private static final String T_ = "_";

    public static void init(String adAppId_channelId, String cpId) {
        if (!TextUtils.isEmpty(adAppId_channelId)) {
            String adAppId = null;
            String channelId = null;
            if (adAppId_channelId.contains(T_)) {
                String[] s = adAppId_channelId.split(T_);
                if (s.length > 0) {
                    adAppId = s[0];
                }
                if (s.length > 1) {
                    channelId = s[1];
                }
            }
            if (TextUtils.isEmpty(channelId)) {
                channelId = InitA.DEFAULT_APP_ID;
            }
            InitA.getInstance().initMediaApp(channelId);

            if (CpAdData.CP_ADSURGE.equals(cpId)) {
                if (TextUtils.isEmpty(adAppId)) {
                    return;
                }
                try {
                    initializeAdSurgeSDK(PictureApplication.getApplication(), adAppId);
                } catch (Exception e) {
                    try {
                        StatsStaticHelper.onEvent("ads_fe",Log.getStackTraceString(e));
                    } catch (Exception e2) {
                        //
                    }
                }
            }
        }
    }

    private static void initializeAdSurgeSDK(Context context, String appId) {
        if (isInit) {
            return;
        }
        isInit = true;
        // Create SDK initialization config using Builder pattern
        AdSurgeAdSdkInitConfig config = new AdSurgeAdSdkInitConfig.Builder().
                setContext(context).setAppId(appId).build();
        AdSurgePrivacyConfiguration.setUserConsent(true);

        // Initialize the SDK with config and callback
        AdSurgeAdSdk.getInstance().
                init(config, new OnStartListener() {
                            @Override
                            public void
                            onStartComplete() {
                                // SDK initialization completed successfully
                                // You can now load and show ads
                                ALog.d("AdSurge_SDK", "SDK initialized successfully");
                                isInit = true;
                            }

                            @Override
                            public void
                            onStartFailed(AdSurgeAdError error) {
                                // Handle initialization failure
                                ALog.e("AdSurge_SDK", "SDK initialization failed: " + error.getErrorMsg());
                                handleInitializationError(error);
                                StatsStaticHelper.onEvent("ads_f",error.getErrorMsg());
                                isInit = false;
                            }
                        }
                );
    }

    private static void handleInitializationError(AdSurgeAdError error) {
        switch (error.getErrorCode()) {
            // Handle network issues
            case AdSurgeAdError.ERROR_CODE_NETWORK_ERROR:
                break;
            // Handle internal errors (e.g., invalid APPID)
            case AdSurgeAdError.ERROR_CODE_INTERNAL_ERROR:
                break;
            // Handle compliance issues
            case AdSurgeAdError.ERROR_CODE_COMPLIANCE:
                break;
            // Handle other errors
            default:
                break;
        }
    }
}
