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

import android.app.Activity
import android.content.Intent
import android.graphics.Bitmap
import android.net.Uri
import android.net.http.SslError
import android.util.Log
import android.view.View
import android.webkit.SslErrorHandler
import android.webkit.WebView
import android.webkit.WebViewClient
import android.widget.ImageButton
import android.widget.Toast

/**
 * 웹 메시지 처리
 */
class MCareWebExtraPopupViewClient(val activity: Activity): WebViewClient(){
    override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
        Log.e("##########", "shouldOverrideUrlLoading() - URL: $url , WEBVIEW: $view")
        // http는 웹뷰에게 맡기고
        if (url.startsWith("https://") || url.startsWith("http://")) {
            return false
        }

        // 전화를 연결해주기 위함
        if (url.startsWith(WebView.SCHEME_TEL)) {
            val intent = Intent(Intent.ACTION_DIAL, Uri.parse(url))
            intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
            this.activity.startActivity(intent);
            return true
        }

        // 메일연결
        if (url.startsWith(WebView.SCHEME_MAILTO)) {
            val parsedUri: Uri = Uri.parse(url)
            val intent = Intent(Intent.ACTION_SENDTO, parsedUri)
            this.activity.startActivity(intent)
            return true
        }
        return false
    }

    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)
    }

}