package com.dlc.exam.ui.learn.test

import android.annotation.SuppressLint
import android.app.Activity
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.FragmentClassTestCompletedBinding
import com.dlc.exam.ui.score.PaperQuViewModel
import com.rc.core.ui.fragment.RcBaseFragment
import com.rc.httpcore.vo.response.HandPractiseBean

/**
 * info
 *
 * @author ReiChin_
 */
class ClassTestCompletedFragment : RcBaseFragment<FragmentClassTestCompletedBinding>() {

    companion object {
        const val FRAGMENT_TAG = "ClassTestCompletedFragment"
        fun newInstance() = ClassTestCompletedFragment()
    }

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

    private var mPaperBean: HandPractiseBean? = null

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

    @SuppressLint("SetTextI18n")
    override fun initViews(savedInstanceState: Bundle?) {
        viewModel.handPractiseData.observe(viewLifecycleOwner) { data ->
            mPaperBean = data
            if (data.passed) {
                viewBinding.examResultLayout.setBackgroundResource(R.mipmap.img_bg_hgl)
                viewBinding.rightInfo.visibility = View.VISIBLE
                viewBinding.errorInfo.visibility = View.GONE
                viewBinding.points.text = CommonUtils.formatEmpty(data.points)
                viewBinding.reMockTest.text = "回顾考核"
            } else {
                viewBinding.examResultLayout.setBackgroundResource(R.mipmap.img_bg_bhg)
                viewBinding.rightInfo.visibility = View.GONE
                viewBinding.errorInfo.visibility = View.VISIBLE
                viewBinding.reMockTest.text = "重新考核"
            }
            viewBinding.accuracy.text = data.accuracy
            viewBinding.unAnswered.text = "未答题数：${CommonUtils.formatEmpty(data.unAnswered)}"
            viewBinding.answered.text = "已答题数：${CommonUtils.formatEmpty(data.answered)}"
            viewBinding.errors.text = "错题数：${CommonUtils.formatEmpty(data.errors)}"
        }

        viewBinding.reMockTest.setOnClickListener {
            // 重新考核/回顾考核
            mPaperBean?.let { data ->
                if (data.passed) {
                    // 回顾考核
                    val intent = Intent(requireContext(), ReviewClassTestActivity::class.java)
                    // 错误数
                    val errors = CommonUtils.stringParseInt(data.errors)
                    val answered = CommonUtils.stringParseInt(data.answered)
                    val unAnswered = CommonUtils.stringParseInt(data.unAnswered)
                    intent.putExtra("errors", errors)
                    intent.putExtra("rights", answered - errors)
                    intent.putExtra("progress", "${answered}/${answered + unAnswered}")
                    intent.putExtra("paperId", data.paperId)
                    startActivity(intent)
                    requireActivity().setResult(Activity.RESULT_OK)
                    requireActivity().finish()
                } else {
                    // 重新考核
                    viewModel.setRetest(true)
                }
            }
        }
    }

}