package com.zhangxin.magicbox

import android.app.Application
import android.text.TextUtils
import com.mob.MobSDK
import com.paypal.checkout.PayPalCheckout
import com.paypal.checkout.config.CheckoutConfig
import com.paypal.checkout.config.Environment
import com.paypal.checkout.config.SettingsConfig
import com.paypal.checkout.createorder.CurrencyCode
import com.paypal.checkout.createorder.UserAction
import com.umeng.commonsdk.UMConfigure
import com.zxhl.cms.AppContext
import com.zxhl.cms.common.ApplicationAsLibrary
import com.zxhl.cms.common.Config
import com.zxhl.cms.common.Constant
import com.zxhl.cms.net.SettingPreference
import com.zxhl.cms.utils.EventUtils.onEvent
import com.zxhl.cms.utils.MiitHelper
import com.zxhl.cms.utils.TTAdManagerHolder
import com.zxhl.cms.utils.Utils
import java.util.*

/**
 * Created by gaoleichao on 2018/12/26
 */
class Appli : Application() {
    private val mChildApplicationList = ArrayList<ApplicationAsLibrary>()

    override fun onCreate() {
        super.onCreate()
        AppContext.init(applicationContext)
        UMConfigure.setLogEnabled(true)
        UMConfigure.init(this, UMConfigure.DEVICE_TYPE_PHONE, "")
        Utils.checkDebugMode(this)
        Config.init(this)
        getOaid()
        initByteDance()
        MobSDK.submitPolicyGrantResult(true, null)
        MobSDK.init(this)
        initPayPal()
    }

    private fun initPayPal() {
        val config = CheckoutConfig(
            application = this,
            clientId = Constant.PAY_PAL_CLIENT_ID,
            environment = Environment.SANDBOX,//正式上线后，改为Environment.LIVE
            returnUrl = "com.zhangxin.magicbox://paypalpay",//创建应用时填的RETURN_URL
            currencyCode = CurrencyCode.USD,//货币种类：CNY-人民币；HKD-港币；TWD-新台币；USD-美元...
            userAction = UserAction.PAY_NOW,
            settingsConfig = SettingsConfig(
                loggingEnabled = true,
                shouldFailEligibility = false
            )
        )
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
            PayPalCheckout.setConfig(config)
        }
    }

    private fun getOaid() {
        MiitHelper(appIdsUpdater).getDeviceIds(AppContext.mAppContext)
    }

    private val appIdsUpdater: MiitHelper.AppIdsUpdater = object : MiitHelper.AppIdsUpdater {
        override fun OnIdsAvalid(ids: String) {
            if (!TextUtils.isEmpty(ids)) {
                SettingPreference.setOaid(ids)
                onEvent("tab_imp", "获取到oaid$ids")
            }
        }
    }

    private fun initByteDance() {
//        TTAdSdk.init(
//            this,
//            TTAdConfig.Builder()
//                .appId("5229735") //  .appId("520728")
//                .useTextureView(true) //使用TextureView控件播放视频,默认为SurfaceView,当有SurfaceView冲突的场景，可以使用TextureView
//                .appName("幸运魔盒")
//                .titleBarTheme(TTAdConstant.TITLE_BAR_THEME_DARK)
//                .allowShowNotify(true) //是否允许sdk展示通知栏提示AnalyticsProvider
//                .allowShowPageWhenScreenLock(true) //是否在锁屏场景支持展示广告落地页
//                .debug(true) //测试阶段打开，可以通过日志排查问题，上线时去除该调用
//                .directDownloadNetworkType(
//                    TTAdConstant.NETWORK_STATE_WIFI,
//                    TTAdConstant.NETWORK_STATE_3G
//                ) //允许直接下载的网络状态集合
//                .supportMultiProcess(false) //是否支持多进程，true支持
//                .build()
//        )
        TTAdManagerHolder.init(this)
    }

    override fun onTrimMemory(level: Int) {
        super.onTrimMemory(level)
//        for (app in mChildApplicationList) {
//            app.onTrimMemoryAsLibrary(this, level)
//        }
    }

    override fun onLowMemory() {
        super.onLowMemory()
//        for (app in mChildApplicationList) {
//            app.onLowMemoryAsLibrary(this)
//        }
    }

}