package com.lemonhc.mplus.mcare_plus.activity.webView

import android.app.AlertDialog
import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.net.Uri
import android.net.http.SslCertificate
import android.net.http.SslError
import android.os.Message
import android.webkit.SslErrorHandler
import android.webkit.WebView
import android.webkit.WebViewClient
import com.lemonhc.mplus.mcare_plus.R
import java.io.ByteArrayInputStream
import java.security.cert.Certificate
import java.security.cert.CertificateException
import java.security.cert.CertificateFactory


class CustomWebViewClient(val context: Context):WebViewClient(){
    override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
        when{
            url.startsWith("tel:") -> {
                val intent = Intent(Intent.ACTION_DIAL)
                intent.data = Uri.parse(url)
                context.startActivity(intent)
                return true
            }
            url.startsWith("sms:") -> {
                val intent = Intent(Intent.ACTION_SENDTO)
                intent.data = Uri.parse(url)
//                intent.type = "vnd.android-dir/mms-sms"
                context.startActivity(intent)
                return true
            }
            url.startsWith("mailto:") -> {
                val intent = Intent(Intent.ACTION_SEND)
                val address = url.replace("mailto:", "")
                intent.putExtra(Intent.EXTRA_EMAIL, arrayOf(address))
                intent.type = "message/rfc822"
                context.startActivity(Intent.createChooser(intent, "Choose Email Client"))
                return true
            }
            url.startsWith("market://") -> {
                val packageName = url.substring(url.lastIndexOf("id=") + 3, url.length)
                if (context.packageManager.getLaunchIntentForPackage(packageName) == null) {
                    val intent = Intent(Intent.ACTION_VIEW)
                    intent.data = Uri.parse(url)
                    context.startActivity(intent)
                } else {
                    val appIntent = context.packageManager.getLaunchIntentForPackage(packageName)
                    context.startActivity(appIntent)
                }
                return true
            }
        }

        return false
    }

    override fun onFormResubmission(view: WebView, dontResend: Message, resend: Message) {
        resend.sendToTarget()
    }

    override fun onReceivedSslError(view: WebView, handler: SslErrorHandler, sslError: SslError) {
        val message = StringBuffer()
        when (sslError.primaryError) {
            SslError.SSL_EXPIRED -> message.append(context.getString(R.string.ssl_expired))
            SslError.SSL_IDMISMATCH -> message.append(context.getString(R.string.ssl_idmismatch))
            SslError.SSL_NOTYETVALID -> message.append(context.getString(R.string.ssl_notyetvalid))
            SslError.SSL_UNTRUSTED -> message.append(context.getString(R.string.ssl_untrusted))
            else -> message.append(context.getString(R.string.ssl_cert_error))
        }

        val builder = AlertDialog.Builder(context)
        builder.setTitle(context.getString(R.string.error))
        builder.setMessage(message.toString())
        builder.setPositiveButton(android.R.string.ok) { dialog, which -> handler.proceed() }
        builder.setNegativeButton(android.R.string.cancel) { dialog, which -> handler.cancel() }
        builder.create().show()
    }

    override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
        super.onPageStarted(view, url, favicon)
    }

    override fun onPageFinished(view: WebView?, url: String?) {
        super.onPageFinished(view, url)
    }

}