package com.zj365.health.act

import android.content.Intent
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.xty.base.act.BaseListAct
import com.xty.common.arouter.ARouterUrl
import com.xty.common.arouter.RouteManager
import com.xty.common.recycleUtils.RecycleItem
import com.xty.common.util.CommonToastUtils
import com.zj365.health.adapter.AnswerAdapter
import com.zj365.health.databinding.ActSubmitReportBinding
import com.zj365.health.vm.RiskAssessVm
import com.xty.network.model.AnswerBean
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

/**
 * 已填问卷列表
 */
@Route(path = ARouterUrl.SUBMIT_REPORT)
class SubmitReportListAct: BaseListAct<RiskAssessVm>() {

    val mAdapter by lazy{ AnswerAdapter() }
    val binding by  lazy{ActSubmitReportBinding.inflate(layoutInflater)}
    val QUEST_REQUEST=10000
    var isEdit=false
    var userId=""
    override fun setLayout()=binding.root
    override fun initData() {
        super.initData()
        userId=intent.extras!!.getString("id").toString()
    }
    override fun initView() {
        super.initView()
        statusBar(binding.title.mView)
        binding.title.mTvTitle.text="历史评估"
        binding.title.mIvBack.setOnClickListener { finish() }


//        binding.title.mTvRight.apply {
//            visibility= View.VISIBLE
//            text="开始评估"
//            setOnClickListener {
//                mViewModel.getQuestTime()
//            }
//        }
    }
    override fun liveObserver() {
        mViewModel.answerLive.observe(this){
            if(isEdit){
                if(mAdapter.data.size==it.data.size){
                    CommonToastUtils.showToast("报告正在生成中，请耐心等待1-3分钟")
                }
            }
            if (it.data.size==0){
                binding.scroll.visibility = View.VISIBLE
                binding.mRefresh.visibility = View.GONE
            }else{
                binding.mRefresh.visibility = View.VISIBLE
                binding.scroll.visibility = View.GONE
            }
            setDate(mAdapter,it.data)
            isEdit=false
        }
        mViewModel.timeOut.observe(this){
            RouteManager.goActRequest(ARouterUrl.QUEST_LIST,this,QUEST_REQUEST)
//            if(it.data){
//                RouteManager.goActRequest(ARouterUrl.QUEST_LIST,this,QUEST_REQUEST)
//            }else{
//                ToastUtils.show(it.msg)
//                //RouteManager.goAct(ARouterUrl.RISK_ASSESS)
//            }
        }
    }

    override fun initAdapter() {
        binding.mRecycle.addItemDecoration(RecycleItem(this,40))
        binding.mRecycle.adapter=mAdapter
        binding.mRecycle.layoutManager=LinearLayoutManager(this)
        mAdapter.setOnItemClickListener { adapter, view, position ->
            var bean =adapter.getItem(position) as AnswerBean
            bundle.clear()
            bundle.putSerializable("bean",bean)
            bundle.putString("id",userId)
            RouteManager.goAct(ARouterUrl.RISK_ASSESS,bundle)
        }
        setRecycleRefresh(binding.mRecycle,binding.mRefresh)
    }

    override fun loadData() {
        mViewModel.getAnswerList(userId)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if(resultCode== RESULT_OK&&requestCode==QUEST_REQUEST){
            mainScope.launch {
                loadingView.show()
                delay(3000)
                loadingView.dismiss()
                isEdit=true
                mViewModel.getAnswerList(userId)
            }

        }

    }
}