package com.ccwangluo.accelerator.ui.web

import android.os.Bundle
import android.webkit.JavascriptInterface
import androidx.core.content.ContextCompat.checkSelfPermission
import com.ccwangluo.accelerator.utils.LoginUtils
import com.ccwangluo.accelerator.utils.SysUtils
import com.ccwangluo.cc_quickly.utils.XToastUtils
import com.just.agentweb.core.AgentWeb
import com.karumi.dexter.Dexter
import com.karumi.dexter.MultiplePermissionsReport
import com.karumi.dexter.PermissionToken
import com.karumi.dexter.listener.PermissionRequest
import com.karumi.dexter.listener.multi.MultiplePermissionsListener
import com.xuexiang.xpage.base.XPageFragment


/**
 * js注入的方法
 */
class AndroidInterface(val xPageFragment: XPageFragment, val agentWeb: AgentWeb) {

    @JavascriptInterface
    fun callAndroid(msg: String) {
        XToastUtils.toast("这是Js调用Android的方法，内容:$msg")
    }

    @JavascriptInterface
    fun close() {
        xPageFragment.popToBack()
    }

    @JavascriptInterface
    fun openNative(pageName: String) {
        xPageFragment.openPage(pageName)
    }

    @JavascriptInterface
    fun openWebview(url: String) {
        val params = Bundle()
        params.putString(CommonWebViewFragment.WEBVIEW_URL_KEY, url)
        xPageFragment.openPage(CommonWebViewFragment::class.java, params)
    }

    @JavascriptInterface
    fun login(callback: String) {
        LoginUtils.login(xPageFragment, {
            if (it) {
                agentWeb.getJsAccessEntrace()
                    .quickCallJs(callback, LoginUtils.token)
            }
        })
    }


    /**
     * 检查权限
     */
    @JavascriptInterface
    fun getPermissionState(key: String, callback: String) {
        xPageFragment.context?.let {
            val hasPermission: Boolean = if (checkSelfPermission(it, key) > 0) true else false
            agentWeb.getJsAccessEntrace()
                .quickCallJs(callback, key, hasPermission.toString())
        }
    }

    /**
     * 申请权限
     */
    @JavascriptInterface
    fun requirePermissionState(key: String, callback: String) {
        xPageFragment.context?.let {
            //获取权限
            Dexter.withContext(it)
                .withPermissions(key)
                .withListener(object : MultiplePermissionsListener {
                    override fun onPermissionsChecked(p0: MultiplePermissionsReport?) {
                        p0?.let {
                            it.grantedPermissionResponses.forEach {
                                agentWeb.getJsAccessEntrace()
                                    .quickCallJs(callback, key, "true")
                            }
                            it.deniedPermissionResponses.forEach {
                                agentWeb.getJsAccessEntrace()
                                    .quickCallJs(callback, key, "false")
                            }
                        }
                    }

                    override fun onPermissionRationaleShouldBeShown(
                        p0: MutableList<PermissionRequest>?,
                        p1: PermissionToken?
                    ) {

                    }

                }).check()
        }
    }

    /**
     * 检查安装包
     */
    @JavascriptInterface
    fun getApkState(packageName: String, callback: String) {
        xPageFragment.context?.let {
            val packageInstall = SysUtils.isPackageInstall(it, packageName)
            agentWeb.getJsAccessEntrace()
                .quickCallJs(callback, packageName, packageInstall.toString())
        }
    }

}