/*
 *    Copyright 2018-2019 adview.com, Inc.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.adview.mobile.javademo;

import android.app.Application;
import android.util.Log;

import com.adview.sdk.AdViewConfig;
import com.adview.sdk.AdViewSDK;
import com.adview.sdk.AdviewMobile;
import com.adview.sdk.ExternalUserId;
import com.adview.sdk.TargetingParams;
import com.adview.sdk.api.data.InitializationStatus;
import com.adview.mobile.javademo.utils.Settings;
import com.applovin.sdk.AppLovinSdk;
import com.applovin.sdk.AppLovinSdkConfiguration;
import com.google.android.gms.ads.MobileAds;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CustomApplication extends Application {

    private static final String TAG = "AdviewCustomApplication";

    @Override
    public void onCreate() {
        super.onCreate();

        initAdviewSDK();
        initAdMob();
        initApplovinMax();

        Settings.init(this);
    }

    private void initAdviewSDK() {
        String accountID = "54649565";
        /* 1. 3rd id config */
        AdViewConfig config = new AdViewConfig.Builder()
                .accountId(accountID) // account ID
                .enabledeh(true)        // 3rd id enable (DEH)
                .build();
        // 2. initialize adview SDK
        AdViewSDK.initialize(this, config);
        // 3. set user params, email ,phone
        AdViewSDK.deh.setHasheddeh(
                "test-java-android-7@adview.com",
                "13800138006"
        );

        //official account
        AdviewMobile.setServerAccountId(accountID);

        AdviewMobile.initializeSdk(getApplicationContext(), status -> {
            if (status == InitializationStatus.SUCCEEDED) {
                Log.d(TAG, "SDK initialized successfully!");
            } else {
                Log.e(TAG, "SDK initialization error: " + status + "\n" + status.getDescription());
            }
        });

        //sample for external uids
        //setupExternalUserIds("YOUR_UID2_TOKEN_HERE", "YOUR_ID5_ID_HERE", 2, "YOUR_LIVERAMP_ID_HERE");

    }

    //external uids sample codes
    /*
    private void setupExternalUserIds(String uid2Token, String id5Id, Integer id5LinkType, String liveRampId) {
        List<ExternalUserId> externalUserIds = new ArrayList<>();

        if (uid2Token != null && !uid2Token.isEmpty()) {
            externalUserIds.add(new ExternalUserId("uidapi.com", List.of(new ExternalUserId.UniqueId(uid2Token, 3))));
        }

        if (id5Id != null && !id5Id.isEmpty()) {
            ExternalUserId.UniqueId id5UniqueId = new ExternalUserId.UniqueId(id5Id, 3);
            if (id5LinkType != null) {
                Map<String, Object> extMap = new HashMap<>();
                extMap.put("linkType", id5LinkType);
                id5UniqueId.setExt(extMap);
            }
            externalUserIds.add(new ExternalUserId("id5-sync.com", List.of(id5UniqueId)));
        }

        if (liveRampId != null && !liveRampId.isEmpty()) {
            externalUserIds.add(new ExternalUserId("liveramp.com", List.of(new ExternalUserId.UniqueId(liveRampId, 3))));
        }

        if (!externalUserIds.isEmpty()) {
            TargetingParams.setExternalUserIds(externalUserIds);
            Log.d(TAG, "External IDs set");
        } else {
            TargetingParams.setExternalUserIds(null);
            Log.d(TAG, "No External IDs provided, eids cleared.");
        }
    }
     */

    private void initAdMob() {
        MobileAds.initialize(this, initializationStatus -> {
            Log.d("MobileAds", "Initialization complete.");
        });
    }

    private void initApplovinMax() {
        AppLovinSdk.getInstance(this).setMediationProvider("max");
        //note: if no sdk Key，SDK initialization will fail
        AppLovinSdk.getInstance(this).initializeSdk(config -> {
            Log.d("MaxAds", " ===== Initialization max complete ====");
        });

        AppLovinSdk.getInstance(this).getSettings().setVerboseLogging(false);
    }

}
