package xn.xxp.home.lab_info

import android.os.Bundle
import core.ui.activity.BaseCountDownActivity
import core.util.EscapeUnescape
import core.util.WebViewHelper
import http.client.ApiRepository
import xn.xxp.databinding.ActivityLabDescBinding
import xn.xxp.room.RoomTool
import xn.xxp.utils.Tool
import xn.xxp.widget.ITitleBar
import xn.xxp.widget.NavViewCompat

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

    override fun createViewBinding(): ActivityLabDescBinding {
        return ActivityLabDescBinding.inflate(layoutInflater)
    }


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

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

        queryLabIntro()
    }

    override fun getMTitleBar(): ITitleBar {
        return binding.titleBar
    }

    override fun getMNavView(): NavViewCompat {
        return binding.navView
    }

    private fun queryLabIntro() {
        val labConfig = RoomTool.getInstance().labConfigDao().labConfig
        if (null == labConfig) {
            showToast("暂无实验室信息，即将重新获取")
            Tool.INSTANCE.reStartApp("暂无实验室信息，即将重新获取")
            return
        }
        showLoading("加载中...")
        val disposable = ApiRepository.labIntro(labConfig.labId.toString())
            .subscribe({ content ->
                dismissLoading()
                binding.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

}