package com.applovin.enterprise.apps.demoapp;

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;

import com.adjust.sdk.Adjust;
import com.adjust.sdk.AdjustConfig;
import com.adjust.sdk.LogLevel;
import com.applovin.sdk.AppLovinMediationProvider;
import com.applovin.sdk.AppLovinSdk;
import com.applovin.sdk.AppLovinSdkSettings;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class GlobalApplication
        extends Application
{
    @Override
    public void onCreate()
    {
        super.onCreate();

        // Initialize the AppLovin SDK
        AppLovinSdk.getInstance( this ).setMediationProvider( AppLovinMediationProvider.MAX );
        AppLovinSdk.getInstance( this ).initializeSdk( config -> {
            // AppLovin SDK is initialized, start loading ads now or later if ad gate is reached

            // Initialize Adjust SDK
            AdjustConfig adjustConfig = new AdjustConfig( getApplicationContext(), "{YourAppToken}", AdjustConfig.ENVIRONMENT_SANDBOX );
            Adjust.onCreate( adjustConfig );

            registerActivityLifecycleCallbacks( new AdjustLifecycleCallbacks() );
        } );
        //wilder 20230202 add debug log output
        AppLovinSdkSettings settings = AppLovinSdk.getInstance(this).getSettings();
        settings.setVerboseLogging(true); //debug logout
        //Interstitial Ad & reward ad , disable Sequential Caching
        //settings.setExtraParameter( "disable_b2b_ad_unit_ids", "YOUR_AD_UNIT_ID1, YOUR_AD_UNIT_ID2" );
        //end wilder
    }

    private static final class AdjustLifecycleCallbacks
            implements ActivityLifecycleCallbacks
    {
        @Override
        public void onActivityCreated(@NonNull final Activity activity, @Nullable final Bundle bundle) {}

        @Override
        public void onActivityStarted(@NonNull final Activity activity) {}

        @Override
        public void onActivityResumed(Activity activity)
        {
            Adjust.onResume();
        }

        @Override
        public void onActivityPaused(Activity activity)
        {
            Adjust.onPause();
        }

        @Override
        public void onActivityStopped(@NonNull final Activity activity) {}

        @Override
        public void onActivitySaveInstanceState(@NonNull final Activity activity, @NonNull final Bundle bundle) {}

        @Override
        public void onActivityDestroyed(@NonNull final Activity activity) {}
    }
}
