package com.dlc.eboard.ui.common

import android.os.Bundle
import com.dlc.eboard.databinding.ActivityHtmlFullScreenBinding
import com.dlc.eboard.ui.widget.NavViewCompat
import com.dlc.eboard.ui.widget.TitleBar
import com.rc.core.ui.ActivityCollector
import com.rc.core.util.EscapeUnescape
import com.rc.core.util.VideoFullScreenWebChromeClient
import com.rc.core.util.WebViewHelper

/**
 * Html全屏
 *
 * @author ReiChin_
 */
class HtmlFullScreenActivity :
    BaseCountDownActivity<ActivityHtmlFullScreenBinding>() {

    override fun createViewBinding() = ActivityHtmlFullScreenBinding.inflate(layoutInflater)

    override val mTitleBar: TitleBar? = null
    override val mNavView: NavViewCompat? = null

    private var htmlContent: String? = null
    private var name: String? = null
    private var onEboard: Boolean = false

    override fun initViews(savedInstanceState: Bundle?) {
        htmlContent = intent.getStringExtra("html_content")
        name = intent.getStringExtra("name")
        onEboard = intent.getBooleanExtra("onEboard", false)

        viewBinding.homeLayout.setOnClickListener {
            if (onEboard) {
                logoutCountDownFinish()
            } else {
                ActivityCollector.removeActivity(2)
            }
        }
        viewBinding.backLayout.setOnClickListener { finish() }

        val webViewHelper = WebViewHelper(viewBinding.webView, viewBinding.progressbar)
        webViewHelper.initWebView(webChromeClientProxy = VideoFullScreenWebChromeClient(viewBinding.videoFullScreen))
    }

    override fun initData() {
        viewBinding.titleText.text = name

        if (!htmlContent.isNullOrEmpty()) {
            val html = StringBuilder()
            html.append("<HTML><HEAD><LINK href=\"simple_table.css\" type=\"text/css\" rel=\"stylesheet\"/></HEAD><body>")
            html.append(EscapeUnescape.unescape(htmlContent))
            html.append("</body></HTML>")

            viewBinding.webView.loadDataWithBaseURL(
                "file:///android_asset/",
                html.toString(),
                "text/html",
                "utf-8",
                null
            )
        }
    }

    override fun enabledBackCountDown() = true

    override fun refreshBackCountDownView(countDown: Int) {
        viewBinding.countDown.text = "(${countDown}s)"
    }

}