package com.xty.base.act

import android.text.Html
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Route
import com.xty.base.R
import com.xty.base.databinding.ActRichTextBinding
import com.xty.base.databinding.TitleDarkBarBinding
import com.xty.base.vm.RichVm
import com.xty.common.Const
import com.xty.common.arouter.ARouterUrl
import com.xty.common.arouter.RouteManager
import java.util.*

/**
 *    阅读协议加载 显示加载
 *
 *     type   1:用户协议 2:管家协议 3:隐私协议v  5：服务协议
 *
 */
@Route(path = ARouterUrl.RICH_TEXT)
class RichTextAct : BaseVmAct<RichVm>() {
    var maxInt=4
    lateinit var timer:Timer
    val binding by lazy {ActRichTextBinding.inflate(layoutInflater)}
    private var title=""
    private var type=0
    private var isAgree=true
    private var hasOther=false
    override fun setLayout()=binding.root
    override fun initData() {
        super.initData()
        isDarkBar=true
        intent.extras?.let {
            title=  it.getString("title").toString()
            type=it.getInt("type")
            isAgree=it.getBoolean("isAgree",true)
            hasOther=it.getBoolean("hasOther",false)
        }
    }
    override fun initView() {
        super.initView()

        statusBar(binding.title.mView)
        binding.title.mIvBack.setOnClickListener { finish() }
        binding.title.mTvTitle.text = title
        timer=Timer()
        binding.mAgree.isSelected=false
        binding.mAgree.isEnabled=false
        if(isAgree){
            binding.mAgree.visibility=View.VISIBLE
        }else{
            binding.mAgree.visibility=View.GONE
        }


        binding.mAgree.setOnClickListener {
            setResult(RESULT_OK)
            finish()
        }
        if(hasOther){
            binding.mOther.text="点击查看《儿童个人信息保护规则》"
            binding.mOther.setOnClickListener {
                bundle.clear()
                bundle.putString("title", "儿童个人信息保护规则")
                bundle.putInt("type", Const.AGREEMENT_9)
                bundle.putBoolean("isAgree", false)
                RouteManager.goAct(ARouterUrl.RICH_TEXT, bundle)
            }
        }

        mViewModel.getPrev(type)
    }
    override fun liveObserver() {
        mViewModel.prevLive.observe(this){
            timer.schedule(object :TimerTask(){
                override fun run() {
                    mViewModel.timeTaskLive.postValue("")
                }
            },0,1000)
            binding.mRich.text=Html.fromHtml(it.data.content)
        }

        mViewModel.timeTaskLive.observe(this){
            --maxInt
            if(maxInt==0){
                timer.cancel()
                maxInt=10
                binding.mAgree.text=getString(R.string.agree)
                binding.mAgree.isSelected=true
                binding.mAgree.isEnabled=true
            }else{
                binding.mAgree.text = "${getString(R.string.agree)}($maxInt)"
                binding.mAgree.isSelected=false
                binding.mAgree.isEnabled=false
            }

        }

    }
    override fun onDestroy() {
        super.onDestroy()
        if(timer!=null){
            timer.cancel()
        }
    }


}