package com.bjzc.bhzhgd.activity



import android.content.Context
import android.content.pm.ApplicationInfo
import android.graphics.Color
import android.os.Bundle
import android.text.TextUtils
import android.util.Log
import android.view.KeyEvent
import android.view.View
import android.view.ViewGroup
import android.webkit.WebSettings
import android.webkit.WebView
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.widget.Toolbar
import com.bjzc.bhzhgd.R
import com.bjzc.bhzhgd.base.BaseAgentWebActivity
import com.bjzc.bhzhgd.contans.Contans
import com.bjzc.bhzhgd.utils.GsonUtils
import com.bjzc.bhzhgd.utils.HttpListener
import com.bjzc.bhzhgd.utils.NoProgresshttpUtils
import com.bjzc.bhzhgd.utils.NohttpUtils
import com.bjzc.bhzhgd.utils.RSAUtils
import com.bjzc.bhzhgd.utils.SPUtils
import com.bjzc.bhzhgd.utils.UiUtlis
import com.yolanda.nohttp.NoHttp
import com.yolanda.nohttp.RequestMethod
import com.yolanda.nohttp.rest.Response
import java.net.URLEncoder


class WorkTableActivity : BaseAgentWebActivity() {
    private val urlToken = "api/app/login/singleLogin?account="
    private lateinit var startUrl:String

    private lateinit var type:String

    private lateinit  var mToolbar: Toolbar
    private lateinit var mTitleTextView: TextView

    private lateinit var context: Context

    private lateinit var token: String


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_work_table)

        context = this;
        type = "工作台"

        val tokenTemp = SPUtils.get(this, Contans.LOGIN_TOKEN, "") as String
        if (!TextUtils.isEmpty(tokenTemp)) {
            token = RSAUtils.decryptBASE64StrLocal(tokenTemp)
        }else{
            token = ""
        }

        showLog("token:")
        showLog(token)

        mToolbar = this.findViewById(R.id.toolbar)
        mToolbar.setTitleTextColor(Color.WHITE)
        mToolbar.title = ""
        mToolbar.setNavigationIcon(R.mipmap.ac_back_icon)
        mTitleTextView = this.findViewById(R.id.toolbar_title)
        mTitleTextView.text = type
        this.setSupportActionBar(mToolbar)
        if (supportActionBar != null) {
            supportActionBar!!.setDisplayHomeAsUpEnabled(true)
        }
        mToolbar.setNavigationOnClickListener { finish() }


        //支持缩放
        mAgentWeb.webCreator.webView.settings.builtInZoomControls = true
        mAgentWeb.webCreator.webView.settings.displayZoomControls = false
        mAgentWeb.webCreator.webView.settings.setSupportZoom(true)
        mAgentWeb.webCreator.webView.settings.cacheMode = WebSettings.LOAD_NO_CACHE

        loadSisPage(token)

    }



    private fun loadSisPage(token: String) {



        var startUrl = Contans.IP + Contans.WORK_TABLE_URL + URLEncoder.encode(token,"UTF-8")
        showLog(startUrl)

        //startUrl = "https://www.hao123.com";
        mAgentWeb.urlLoader.loadUrl(startUrl)
    }



    override fun getAgentWebParent(): ViewGroup {
        return findViewById<View>(R.id.container) as ViewGroup
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (mAgentWeb != null && mAgentWeb.handleKeyEvent(keyCode, event)) {
            return true
        }

        return super.onKeyDown(keyCode, event)
    }

    override fun getIndicatorColor(): Int {
        return Color.parseColor("#ff0000")
    }

    override fun setTitle(view: WebView?, title: String?) {
        super.setTitle(view, title)
        if (!TextUtils.isEmpty(title)) {
            if (mTitleTextView != null) {
                mTitleTextView.text = type
            }
        }
    }

    override fun getIndicatorHeight(): Int {
        return 3
    }

    override fun getUrl(): String? {
        return null
        //return "https://www.baidu.com/";
    }

    private fun showLog(logText: String) {
        if (isApkInDebug(context)) {
            if (TextUtils.isEmpty(logText)) {
                Log.i("app", "logText is null")
            } else {
                Log.i("app", logText)
            }
        }
    }

    /**
     * 判断当前应用是否是debug状态
     */
    fun isApkInDebug(context: Context): Boolean {
        try {
            val info = context.applicationInfo
            return (info.flags and ApplicationInfo.FLAG_DEBUGGABLE) != 0
        } catch (e: Exception) {
            return false
        }
    }

    fun showToast(msg: String?) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show()
    }

    fun showToast(strId: Int) {
        Toast.makeText(this, strId, Toast.LENGTH_SHORT).show()
    }
}





//import android.os.Bundle
//import androidx.activity.enableEdgeToEdge
//import androidx.appcompat.app.AppCompatActivity
//import androidx.core.view.ViewCompat
//import androidx.core.view.WindowInsetsCompat
//import com.bjzc.bhzhgd.R
//
//class WorkTableActivity : AppCompatActivity() {
//    override fun onCreate(savedInstanceState: Bundle?) {
//        super.onCreate(savedInstanceState)
//        enableEdgeToEdge()
//        setContentView(R.layout.activity_work_table)
//        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
//            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
//            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
//            insets
//        }
//    }
//}