package com.zj365.health.fragment.tongue

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.xty.base.fragment.BaseVmFrag
import com.xty.common.util.CommonToastUtils
import com.zj365.health.act.tonguediagnosis.TongueDetailActivity
import com.zj365.health.adapter.TongueQuestAdapter
import com.zj365.health.databinding.FragmentTongueDiagnosisInfoBinding
import com.zj365.health.vm.TongueDiagnosisVm
import com.xty.network.model.SelectedAnswerBean
import com.xty.network.model.TongueQuestionBean
import com.xty.network.model.CancerGoodsBean
import com.xty.network.utils.JsonParser

private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"

class TongueDiagnosisInfoFragment : BaseVmFrag<TongueDiagnosisVm>() {

    val binding by lazy { FragmentTongueDiagnosisInfoBinding.inflate(layoutInflater) }
    override fun setViewModel() = TongueDiagnosisVm()


    override fun setLayout() = binding.root

    lateinit var bean: CancerGoodsBean

    val mAdapter  by lazy{ TongueQuestAdapter() }

    companion object {
        var orderNoKey: String = "orderNoKey"
        fun newInstance(bean: CancerGoodsBean): Fragment {
            val args = Bundle()
            args.putSerializable(orderNoKey, bean)
            val fragment = TongueDiagnosisInfoFragment()
            fragment.arguments = args
            return fragment
        }
    }

    override fun initData() {
        super.initData()
    }

    private val tongueQuestionBeans: List<TongueQuestionBean>?
        get() {
            var questions = bean.inquiryQuestion?.let { JsonParser.parseTongueQuestionBean(it) }
            return questions
        }

    override fun initView() {
        super.initView()
        initMyView()
    }

    fun initMyView(){
        bean = (activity as TongueDetailActivity).bean
        when (bean.status) {
//            2 -> result="审核失败"
//            3 -> result="待问诊"
//            6 -> result="失败"
            5 -> {
                //已完成 回答了显示回答页面  没回答显示提交按钮
                if (bean.inquiryQuestion.isNullOrEmpty()) {
                    binding.clSuccess.visibility = View.VISIBLE
                    binding.clIng.visibility = View.GONE
                    binding.clQuestion.visibility = View.GONE
                    binding.tvSubmit.visibility = View.GONE
                } else {
                    binding.clSuccess.visibility = View.GONE
                    binding.clIng.visibility = View.GONE
                    binding.clQuestion.visibility = View.VISIBLE
                    binding.tvSubmit.visibility = View.GONE
                }
            }
            2,6,4 -> {
                //审核失败，失败
                binding.clSuccess.visibility = View.GONE
                binding.clIng.visibility = View.VISIBLE
                binding.clQuestion.visibility = View.GONE
                binding.tvSubmit.visibility = View.GONE
            }
            else -> {
                //待问诊  回答了显示回答页面  没回答显示提交按钮
                binding.clSuccess.visibility = View.GONE
                binding.clIng.visibility = View.GONE
                binding.clQuestion.visibility = View.VISIBLE
                binding.tvSubmit.visibility = View.VISIBLE
            }
        }
        binding.recyclerViewQuestion.layoutManager= LinearLayoutManager(this.requireContext())
        binding.recyclerViewQuestion.adapter=mAdapter

        if (bean.inquiryQuestion?.isNotEmpty() == true) {
            if (bean.inquiryAnswer?.isNotEmpty() == true) {
                val selectedAnswerList: List<SelectedAnswerBean> = JsonParser.parseTongueQuestionAnswerBean(
                    bean.inquiryAnswer!!
                )
                mAdapter.setSelectedAnswer(selectedAnswerList.sortedBy { it.questionIndex })
                binding.tvSubmit.visibility = View.GONE
            }
            mAdapter.setNewInstance(tongueQuestionBeans as MutableList<TongueQuestionBean>?)
        }

        binding.tvSubmit.setOnClickListener {
            if (getSelectedAnswersList().size == tongueQuestionBeans?.size) {
                //提交答案
                mViewModel.tongueInquiry(bean.orderNo!!,getSelectedAnswersList().sortedBy { it.questionIndex })
            } else {
                CommonToastUtils.showToast("请作答所有问题再提交")
            }
        }
    }

    fun getSelectedAnswersList(): ArrayList<SelectedAnswerBean> {
        return ArrayList(mAdapter.getSelectedAnswers().values)
    }

    override fun observer() {
        mViewModel.questionInfo.observe(this){
            (activity as TongueDetailActivity).getOrderDetailInfo()
        }
    }
}