package com.xuqm.base.web

import android.Manifest
import android.net.Uri
import android.webkit.*
import com.bigkoo.alertview.AlertView
import com.xuqm.base.App
import com.xuqm.base.extensions.log
import com.xuqm.base.extensions.loge
import com.xuqm.base.extensions.runWithPermission


class XWebChromeClient(val mContext: XWebViewActivity, val viewModel: XWebViewModel) :
    WebChromeClient() {

    override fun onProgressChanged(view: WebView?, newProgress: Int) {
        super.onProgressChanged(view, newProgress)
        viewModel.setProgress(newProgress)
    }

    override fun onJsAlert(
        view: WebView,
        url: String,
        message: String,
        result: JsResult
    ): Boolean {
        App.getInstance().runOnUiThread {

            AlertView(
                "", message, null, arrayOf("确定"), null, mContext,
                AlertView.Style.Alert
            ) { _, _ ->
                result.confirm()
            }.show()
        }
        return true
    }

    override fun onJsConfirm(
        view: WebView,
        url: String,
        message: String,
        result: JsResult
    ): Boolean {
        AlertView(
            "", message, null, arrayOf("确定", "取消"), null, mContext,
            AlertView.Style.Alert
        ) { _, position ->
            when (position) {
                0 -> result.confirm()
                1 -> result.cancel()
            }
        }.show()
        return true
    }

    override fun onShowFileChooser(
        webView: WebView,
        filePathCallback: ValueCallback<Array<Uri>>,
        fileChooserParams: FileChooserParams
    ): Boolean {
        var types = "*/*"
        if (fileChooserParams.acceptTypes.isNotEmpty())
            types = if (fileChooserParams.acceptTypes[0].contains("image"))
                "image/*"
            else "*/*"
        mContext.selectedFile(types, filePathCallback)
        return true
    }

    override fun onGeolocationPermissionsShowPrompt(
        origin: String?,
        callback: GeolocationPermissions.Callback
    ) {

        mContext.runWithPermission(
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.ACCESS_FINE_LOCATION,
        ) {

            AlertView(
                "定位权限",
                "当前页面需要获取定位权限",
                null,
                arrayOf("确定", "取消"),
                null,
                mContext,
                AlertView.Style.Alert
            ) { _, position ->
                when (position) {
                    0 -> callback.invoke(origin, true, true)
                    1 -> callback.invoke(origin, false, true)
                }
                super.onGeolocationPermissionsShowPrompt(origin, callback)
            }.show()

        }
    }

    override fun onGeolocationPermissionsHidePrompt() {
        "onGeolocationPermissionsHidePrompt".loge()
        super.onGeolocationPermissionsHidePrompt()
    }

    override fun onPermissionRequest(request: PermissionRequest) {
        "onPermissionRequest".loge()
        request.grant(request.resources)
    }

    override fun onPermissionRequestCanceled(request: PermissionRequest?) {
        "onPermissionRequestCanceled".loge()
        request?.loge()
        super.onPermissionRequestCanceled(request)
    }
}