package com.sd.cavphmi.websockets


import org.java_websocket.client.WebSocketClient
import org.java_websocket.drafts.Draft
import org.java_websocket.handshake.ServerHandshake
import java.net.Socket
import java.net.URI
import java.nio.ByteBuffer
import java.security.SecureRandom
import java.security.cert.X509Certificate
import javax.net.ssl.SSLContext
import javax.net.ssl.SSLEngine
import javax.net.ssl.SSLSocketFactory
import javax.net.ssl.TrustManager
import javax.net.ssl.X509ExtendedTrustManager

/*****网联车辆位姿**/
class VecLocWSClient : WebSocketClient {

    private val TAG = "-----VecLocWSClient"

    constructor(serverUri: URI) : super(serverUri){
        if(serverUri.toString().contains("wss://")){
            trustAllHosts()
        }
    }

    constructor(serverUri: URI?, protocolDraft: Draft?) : super(serverUri, protocolDraft)

    constructor(serverUri: URI?, httpHeaders: MutableMap<String, String>?) : super(
        serverUri,
        httpHeaders
    )

    override fun onOpen(handshakedata: ServerHandshake?) {
        send("Hello, it is me. Mario :)")
        System.out.println(TAG + "new connection opened")
    }

    override fun onMessage(message: String?) {
//        System.out.println(TAG + " : " + message)
        if (message.isNullOrEmpty())
            return
        onDataCb?.onMsg(message)
//        onSocketCb?.onMessage(message)

    }

    override fun onMessage(bytes: ByteBuffer?) {
//        System.out.println(TAG + "  received ByteBuffer")

    }

    override fun onClose(code: Int, reason: String?, remote: Boolean) {
//        System.out.println(TAG + "  closed with exit code " + code + " additional info: " + reason);

    }

    override fun onError(ex: Exception?) {
//        System.err.println(TAG + "an error occurred:" + ex)

    }

    var onDataCb: OnDataCb? = null

    interface OnDataCb {
        fun onMsg(str: String)
    }

    private fun trustAllHosts() {
        val trustAllCerts =
            arrayOf<TrustManager>(object : X509ExtendedTrustManager() {
                override fun checkClientTrusted(
                    chain: Array<out X509Certificate?>?,
                    authType: String?,
                    socket: Socket?
                ) {
                }

                override fun checkServerTrusted(
                    chain: Array<out X509Certificate?>?,
                    authType: String?,
                    socket: Socket?
                ) {
                }

                override fun checkClientTrusted(
                    chain: Array<out X509Certificate?>?,
                    authType: String?,
                    engine: SSLEngine?
                ) {
                }

                override fun checkServerTrusted(
                    chain: Array<out X509Certificate?>?,
                    authType: String?,
                    engine: SSLEngine?
                ) {
                }

                override fun checkClientTrusted(
                    chain: Array<out X509Certificate?>?,
                    authType: String?
                ) {
                }

                override fun checkServerTrusted(
                    chain: Array<out X509Certificate?>?,
                    authType: String?
                ) {
                }

                override fun getAcceptedIssuers(): Array<out X509Certificate?>? {
                    return null
                }
            })

        try {
            val ssl = SSLContext.getInstance("SSL")
            ssl.init(null, trustAllCerts, SecureRandom())

            val socketFactory: SSLSocketFactory? = ssl.getSocketFactory()
            this.setSocketFactory(socketFactory)
        } catch (e: java.lang.Exception) {
            e.printStackTrace()
        }

    }
}