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

import android.app.Activity
import android.app.AlertDialog
import android.content.Context
import android.graphics.Bitmap
import android.os.Message
import android.webkit.JsResult
import android.webkit.WebChromeClient
import android.webkit.WebView
import com.lemonhc.mplus.mcare_plus.R
import com.lemonhc.mplus.mcare_plus.activity.MainActivity

class CustomWebChromeClient(val context: Context): WebChromeClient(){
    override fun onCreateWindow(view: WebView, isDialog: Boolean, isUserGesture: Boolean, resultMsg: Message): Boolean {
        MainActivity.setWebView(context as Activity,view)
        val transport = resultMsg.obj as WebView.WebViewTransport
        transport.webView = view
        resultMsg.sendToTarget()
        return false
    }

    override fun onProgressChanged(view: WebView, progress: Int) {
        super.onProgressChanged(view, progress)
    }

    override fun onReceivedIcon(view: WebView, icon: Bitmap) {
        super.onReceivedIcon(view, icon)
    }

    override fun onReceivedTitle(view: WebView, title: String) {
        super.onReceivedTitle(view, title)
    }

    override fun getDefaultVideoPoster(): Bitmap? {
        return super.getDefaultVideoPoster()
    }

    override fun onJsAlert(view: WebView, url: String, message: String, result: JsResult): Boolean {
        AlertDialog.Builder(view.context)
                .setTitle(context.getString(R.string.info))
                .setMessage(message)
                .setPositiveButton(android.R.string.ok, { _, _ ->
                    result.confirm()
                })
                .setCancelable(false)
                .create()
                .show()
        return true
    }

}