package com.dahe.gldriver.base

import android.app.Application
import android.content.Context
import com.dahe.gldriver.BuildConfig
import com.dahe.gldriver.base.App
import com.dahe.mylibrary.CommonBaseLibrary
import com.dahe.gldriver.net.Api
import java.lang.Exception
import java.security.SecureRandom
import java.security.cert.X509Certificate
import javax.net.ssl.HttpsURLConnection
import javax.net.ssl.SSLContext
import javax.net.ssl.TrustManager
import javax.net.ssl.X509TrustManager

class App : Application() {
    override fun onCreate() {
        super.onCreate()
        app = this
        CommonBaseLibrary.getInstance().init(this, Api.BASE_URL)
        if (BuildConfig.DEBUG) {
//            CrashHandler.getInstance().init(this);
//            CrashHandler2.getInstance().init(getApplicationContext());
        }
        //        CrashReport.initCrashReport(getApplicationContext(), AppConfig.BUGLY_APP_ID, false);


//        SophixManager.getInstance().queryAndLoadNewPatch();

//        OssServiceUtil.getInstance().init();

//                PendingIntent pendingIntent =
//                PendingIntent.getActivity(this, 0, new Intent(this, HomeActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
//        Cactus.getInstance()
//                .isDebug(true)
//                //可选，设置通知栏点击事件
//                .setPendingIntent(pendingIntent)
////                .setBackgroundMusicEnabled(true)//可选，退到后台是否可以播放音乐
////                .setMusicId(R.raw.main) //可选，设置音乐
////                .setPendingIntent(pendingIntent)//可选，设置通知栏点击事件
////                .addCallback(new CactusCallback())//可选，运行时回调
//                .setCrashRestartUIEnabled(true) //可选，设置奔溃可以重启，google原生rom android 10以下可以正常重启
//                .setCrashRestartUIEnabled(true)//可选，设置奔溃可以重启，google原生rom android 10以下可以正常重启
//                .setTitle("大河好运")
//                .hideNotificationAfterO(true)
//                .setSmallIcon(R.drawable.right_icon)
//                .addBackgroundCallback(new CactusBackgroundCallback() {
//                    @Override
//                    public void onBackground(boolean b) {
//                        if (b){
////                            getBillLocation();
//                        }
//                    }
//                })
//                .register(this);
    }

    override fun attachBaseContext(base: Context) {
        super.attachBaseContext(base)
    }

    //忽略https的证书校验
    fun handleSSLHandshake() {
        try {
            val trustAllCerts = arrayOf<TrustManager>(object : X509TrustManager {
                override fun getAcceptedIssuers(): Array<X509Certificate?> {
                    return arrayOfNulls(0)
                }

                override fun checkClientTrusted(certs: Array<X509Certificate>, authType: String) {}
                override fun checkServerTrusted(certs: Array<X509Certificate>, authType: String) {}
            })
            val sc = SSLContext.getInstance("TLS")
            // trustAllCerts信任所有的证书
            sc.init(null, trustAllCerts, SecureRandom())
            HttpsURLConnection.setDefaultSSLSocketFactory(sc.socketFactory)
            HttpsURLConnection.setDefaultHostnameVerifier { hostname, session -> true }
        } catch (ignored: Exception) {
        }
    }

    companion object {
        var app: App? = null
    }
}