package com.dlc.exam.ui.exam

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.ViewModelProvider
import com.dlc.exam.R
import com.dlc.exam.common.CommonUtils
import com.dlc.exam.databinding.FragmentExamCompletedBinding
import com.dlc.exam.ui.certificate.CertificatePhotoDialog
import com.dlc.exam.ui.score.ExamScoreDetailActivity
import com.dlc.exam.ui.score.PaperQuViewModel
import com.rc.core.ui.fragment.RcBaseFragment
import com.rc.httpcore.vo.response.HandPaperBean

/**
 * 考试完成
 *
 * @author ReiChin_
 */
class ExamCompletedFragment : RcBaseFragment<FragmentExamCompletedBinding>() {

    companion object {
        const val FRAGMENT_TAG = "ExamCompletedFragment"

        fun newInstance() = ExamCompletedFragment()
    }

    private val viewModel by lazy {
        ViewModelProvider(requireActivity(), ViewModelProvider.NewInstanceFactory()).get(
            PaperQuViewModel::class.java
        )
    }

    override fun createViewBinding(
        inflater: LayoutInflater,
        container: ViewGroup?
    ) = FragmentExamCompletedBinding.inflate(inflater, container, false)

    override fun initViews(savedInstanceState: Bundle?) {

        viewModel.examHandPaperData.observe(viewLifecycleOwner) { data ->
            mPaperBean = data
            updateViews(data)
        }

        // 查看证书
        viewBinding.certificate.setOnClickListener {
            mPaperBean?.let {
                if (!it.userCertUrl.isNullOrEmpty()) {
                    CertificatePhotoDialog(requireContext(), it.userCertUrl).show()
                }
            }
        }

        // 回顾试卷
        viewBinding.reviewExam.setOnClickListener {
            mPaperBean?.let {
                val intent = Intent(requireContext(), ExamScoreDetailActivity::class.java)
                intent.putExtra("paperId", it.paperId)
                intent.putExtra("scopeType", it.examType)
                startActivity(intent)
                requireActivity().finish()
            }
        }
    }

    private var mPaperBean: HandPaperBean? = null

    @SuppressLint("SetTextI18n")
    private fun updateViews(data: HandPaperBean) {
        viewBinding.topicType.text = "${data.examType}成绩"
        viewBinding.examTime.text = "用时：${data.userTime}"
        if (data.passed) {
            viewBinding.examResultLayout.setBackgroundResource(R.mipmap.img_bg_hgl)
            viewBinding.examResult.text = "合格"
        } else {
            viewBinding.examResultLayout.setBackgroundResource(R.mipmap.img_bg_bhg)
            viewBinding.examResult.text = "不合格"
        }
        viewBinding.examScore.text = data.userScore
        viewBinding.unAnswered.text = "未答题数：${CommonUtils.formatEmpty(data.unAnswered)}"
        viewBinding.answered.text = "已答题数：${CommonUtils.formatEmpty(data.answered)}"
        viewBinding.errors.text = "错题数：${CommonUtils.formatEmpty(data.errors)}"
        viewBinding.certificate.visibility =
            if (data.userCertUrl.isNullOrEmpty()) View.GONE else View.VISIBLE
    }

}