package com.xty.server.act

import com.alibaba.android.arouter.facade.annotation.Route
import com.xty.base.act.BaseVmAct
import com.xty.common.WebViewOrigin
import com.xty.common.arouter.ARouterUrl
import com.xty.network.model.ScienceBean
import com.xty.server.databinding.ActScienceDetailBinding
import com.xty.server.vm.ScienceDetailVm
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

/**
 * 科普详情
 */
@Route(path = ARouterUrl.SCIENCE_DETAIL)
class ScienceDetailAct:BaseVmAct<ScienceDetailVm>() {
    val binding by lazy{ActScienceDetailBinding.inflate(layoutInflater)}
    var bean: ScienceBean?=null
    var job: Job?=null

    var id:String? = null
    override fun setLayout()=binding.root
    override fun initData() {
        super.initData()
        isDarkBar=true
        intent.extras?.let {
            if(it.containsKey("bean")){
                bean=intent.extras!!.getSerializable("bean") as ScienceBean

            }else if(it.containsKey("id")){
                id = it.getString("id")
            }
        }

    }

    override fun initView() {
        super.initView()
        statusBar(binding.title.mView)
        binding.title.mIvBack.setOnClickListener { finish() }
        binding.title.mTvTitle.text = "科普详情"
        bean?.let {
            binding.mTvSubTitle.text =  bean?.name
            binding.mTvOrgName.text = bean?.orgName
            binding.mTvReleaseTime.text = bean?.time
            WebViewOrigin().initWebSettings(this,binding.mRich)
            binding.mRich.loadDataWithBaseURL(null,bean!!.content,"text/html", "utf-8", null);

        }

        id?.let {
            mViewModel.getDetailForId(it)
        }
    }

    private fun startDelay() {
        job= mainScope.launch {
            delay(30*1000)
            mViewModel.startSetScore()
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        job?.let{
            job!!.cancel()
        }
    }

    override fun liveObserver() {

        mViewModel.detailLiveData.observe(this){
            it.data.let {
                binding.mTvSubTitle.text =  it.name
                binding.mTvOrgName.text = it.orgName
                binding.mTvReleaseTime.text = it.time
                WebViewOrigin().initWebSettings(this,binding.mRich)
                binding.mRich.loadDataWithBaseURL(null,it.content,"text/html", "utf-8", null);
            }
        }
    }
}