package com.xunao.effectdemo.activity

import android.app.Activity
import android.os.Bundle
import android.webkit.WebSettings
import android.webkit.WebViewClient
import android.content.Intent
import android.net.Uri
import android.webkit.WebView
import com.xunao.effectdemo.R
import com.xunao.effectdemo.net.ApiHttpClient
import com.xunao.effectdemo.net.ApiUrl
import com.xunao.effectdemo.net.CSMHttpCallback

/**
 * author : 程中强
 * e-mail : 740479946@qq.com
 * date : 2022/8/2917:40
 * desc :
 * version: 1.0
 */
class WebActivity : Activity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_web)
        val webview = findViewById<WebView>(R.id.webview)
        val webSettings = webview.settings
        webSettings.domStorageEnabled = true
        webSettings.javaScriptEnabled = true
        webSettings.blockNetworkImage = false
        webSettings.builtInZoomControls = true
        // 覆盖WebView默认使用第三方或系统默认浏览器打开网页的行为，使网页用WebView打开
        webview.webViewClient = object : WebViewClient() {
            override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
//                view.loadUrl(url);
//                return true;
                return if (url.startsWith("http:") || url.startsWith("https:") || url.startsWith("ftp")) {
                    view.loadUrl(url)
                    true
                } else if (url.startsWith("scheme://")) {
                    // 使用浏览器打开
                    val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
                    startActivity(intent)
                    true
                } else {
                    false
                }
            }
        }
        webview.loadUrl("http://waibao.kidcastle.org/smallgame/tiaoyitiao/zjreplay.html")
    }

    fun login() {
        ApiHttpClient.get(ApiUrl.login, object : CSMHttpCallback() {
            override fun onSuccess(jsonStr: String) {}
            override fun onFail(msg: String) {}
        })
    }
}