package com.dlc.eboard.ui.lib

import android.os.Bundle
import com.dlc.eboard.LabApp
import com.dlc.eboard.common.CommonUtils
import com.dlc.eboard.databinding.ActivityLabDescBinding
import com.dlc.eboard.ui.common.BaseCountDownActivity
import com.dlc.eboard.ui.widget.NavViewCompat
import com.dlc.eboard.ui.widget.TitleBar
import com.rc.core.util.EscapeUnescape
import com.rc.core.util.WebViewHelper
import com.rc.httpcore.client.ApiRepository

/**
 * 实验室简介
 *
 * @author ReiChin_
 */
class LabDescActivity :
    BaseCountDownActivity<ActivityLabDescBinding>() {

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

    override val mTitleBar: TitleBar by lazy { viewBinding.titleBar }
    override val mNavView: NavViewCompat by lazy { viewBinding.navView }

    override fun initViews(savedInstanceState: Bundle?) {
        super.initViews(savedInstanceState)

        val webViewHelper = WebViewHelper(viewBinding.webView, viewBinding.progressbar)
        webViewHelper.initWebView()

        queryLabIntro()
    }

    private fun queryLabIntro() {
        if (null == LabApp.sLabConfig) {
            showToast("暂无实验室信息，即将重新获取")
            CommonUtils.restartApp(this)
            return
        }

        showLoading()
        val disposable = ApiRepository.labIntro(LabApp.sLabConfig!!.labId)
            .subscribe({ content ->
                dismissLoading()
                viewBinding.webView.loadDataWithBaseURL(
                    "file:///android_asset/",
                    EscapeUnescape.unescape(content),
                    "text/html",
                    "utf-8",
                    null
                )
            }, { throwable ->
                dismissLoading()
                showNetError(throwable)
                throwable.printStackTrace()
            })
        addDisposable(disposable)
    }

    override fun enabledBackCountDown() = true

}