/*
 *    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.adviewkotlindemo

import android.app.Application
import android.util.Log
import com.adview.mobile.adviewkotlindemo.utils.Settings
import com.adview.sdk.AdviewMobile
import com.adview.sdk.ExternalUserId
import com.adview.sdk.ExternalUserId.UniqueId
import com.adview.sdk.TargetingParams
import com.adview.sdk.api.data.InitializationStatus
import com.applovin.sdk.AppLovinSdk
import com.google.android.gms.ads.MobileAds
import java.util.List


class CustomApplication : Application() {

    companion object {
        private const val TAG = "AdviewCustomApplication"
    }

    override fun onCreate() {
        super.onCreate()

        initAdviewSDK()
        initAdMob()
        initApplovinMax()

        Settings.init(this)
    }


    private fun initAdviewSDK() {
        //account id
        AdviewMobile.setServerAccountId("54649565")

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

        // sample codes: setup External User IDs (UID2.0, ID5, LiveRamp)
        // sample: ID5 linkType =  2 (Hard Link)
//        setupExternalUserIds(
//            uid2Token = "YOUR_UID2_TOKEN_HERE",
//            id5Id = "YOUR_ID5_ID_HERE",
//            id5LinkType = 2, // 2 - Hard Link
//            liveRampId = "YOUR_LIVERAMP_ID_HERE"
//        )

    }

    /* sample codes for multi ids (UID2, ID5, LiveRamp)
    private fun setupExternalUserIds(
        uid2Token: String? = null,
        id5Id: String? = null,
        id5LinkType: Int? = null,
        liveRampId: String? = null
    ) {
        val externalUserIds = mutableListOf<ExternalUserId>()
//        base OpenRTB, atype as following:
//        1 (Proprietary / Exchange ID): means private ID or exchange ID.
//        2 (Session ID): means session ID.
//        3 (Device ID): device id , IMEI or other device ID. but usually not used. most case in device.ifa.

        // 1. UID2.0 - source: uidapi.com
        if (!uid2Token.isNullOrBlank()) {
            externalUserIds.add(ExternalUserId("uidapi.com", listOf(UniqueId(uid2Token, 3))))
        }

        // 2. ID5 - source: id5-sync.com
        if (!id5Id.isNullOrBlank()) {
            val id5UniqueId = UniqueId(id5Id, 1)
            // ID5  linkType
            if (id5LinkType != null) {
                val extMap = mutableMapOf<String, Any>()
                extMap["linkType"] = id5LinkType
                id5UniqueId.setExt(extMap)
            }
            externalUserIds.add(ExternalUserId("id5-sync.com", listOf(id5UniqueId)))
        }

        // 3. LiveRamp (RampID) - source: liveramp.com
        if (!liveRampId.isNullOrBlank()) {
            externalUserIds.add(ExternalUserId("liveramp.com", listOf(UniqueId(liveRampId, 3))))
        }

        // set to user ids
        if (externalUserIds.isNotEmpty()) {
            TargetingParams.setExternalUserIds(externalUserIds)
            Log.d(TAG, "External IDs set: ${externalUserIds.joinToString { it.source }}")
        } else {
            TargetingParams.setExternalUserIds(null)
            Log.d(TAG, "No External IDs provided, eids cleared.")
        }
    }
     */


    private fun initAdMob() {
        MobileAds.initialize(this) {
            Log.d("MobileAds", "Initialization complete.")
        }
    }

    private fun initApplovinMax() {
        AppLovinSdk.getInstance(this).mediationProvider = "max"
        AppLovinSdk.getInstance(this).initializeSdk { }
        // more debug can set debug to true
        AppLovinSdk.getInstance(this).settings.setVerboseLogging(false)
    }

}
