package com.cretin.webapi.controller.exam

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper
import com.cretin.webcore.exception.WholeException
import com.cretin.webcore.utils.GsonGet
import com.cretin.webcore.utils.OkHttpUtils
import com.cretin.entity.common.Resp
import com.cretin.entity.exam.ExamItem1
import com.cretin.entity.exam.ExamItem2
import com.cretin.entity.exam.ExamItem4
import com.cretin.webdb.entity.TbExamAnswer
import com.cretin.webdb.service.ITbExamAnswerService
import com.cretin.webservices.service.api.exam.ExamService
import org.apache.http.util.TextUtils
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.*
import java.util.Random
import javax.servlet.http.HttpServletRequest

@RestController
@RequestMapping("/exam")
@ResponseBody
class ExamController {

    @Autowired
    private var tbExamAnswerService: ITbExamAnswerService? = null

    @Autowired
    private var examService: ExamService? = null

    @GetMapping("/do_exam")
    fun doExam(
        @RequestParam(value = "cookie") cookie: String,
        @RequestParam(value = "parent_class_id") parent_class_id: String,
        @RequestParam(value = "activity_module_id") activity_module_id: String,
        @RequestParam(value = "activity_register_id") activity_register_id: String,
        httpServletRequest: HttpServletRequest
    ): Resp<*> {
        try {
            val tbExamAnswer = tbExamAnswerService?.getOne(QueryWrapper<TbExamAnswer>().apply {
                eq("activity_module_id", activity_module_id)
            }, false) ?: throw WholeException("管理员未配置此考试答案，请联系管理员")
            val examParam =
                GsonGet.getGson().fromJson<ExamItem4>(tbExamAnswer.answerContent, ExamItem4::class.java)
            examParam.answer_cost_seconds = Random().nextInt(1000) + 600
            examParam.activity_module_id = activity_module_id
            examParam.activity_register_id = activity_register_id
            //模拟考试
            examService?.submitTestAnswer(cookie, examParam)
            return Resp.createSuccess()
        } catch (e: Throwable) {
            if (e is WholeException) {
                throw e
            }
            throw WholeException("操作失败")
        }
    }

    @GetMapping("/list")
    fun examList(
        @RequestParam(value = "cookie") cookie: String,
        @RequestParam(value = "parent_class_id") parent_class_id: String,
        httpServletRequest: HttpServletRequest
    ): Resp<*> {
        if (TextUtils.isEmpty(cookie)) {
            throw WholeException("cookie不能为空")
        }
        if (TextUtils.isEmpty(parent_class_id)) {
            throw WholeException("班级id不能为空")
        }
        val activityList = examService?.getActivityList(cookie, parent_class_id) ?: throw WholeException("数据获取失败")
        val list = mutableListOf<MutableMap<String, Any>>()
        if (activityList.code == 0) {
            activityList.data?.filter { it.activityRegister != null }?.forEachIndexed { index, it ->
                val courseList = mutableListOf<MutableMap<String, Any>>()
                val activityId = it.id
                Thread.sleep(300)
                val learnerActivityModule = examService?.queryLearnerActivityModule(cookie, parent_class_id, it.id)
                if (learnerActivityModule?.code == 0) {
                    var tempIndex = 0
                    learnerActivityModule.data?.forEach {

                        if (it.child_activitymodule != null && it.child_activitymodule.isNotEmpty()) {
                            //有多层
                            it.child_activitymodule.forEach {
                                val examUrl = "https://tlearning-sdbg.tcl.com/static/admin/#/course/play/${it.id}?t=144&from=course&is=true&class_id=$parent_class_id&rid=${it.activity_register_id}&aid=${it.activity_id}"
                                courseList.add(
                                    mutableMapOf(
                                        "id" to (index + 1) * 100 + (tempIndex++),
                                        "name" to it.mod_name,
                                        "title_flag" to false,
                                        "activity_register_id" to it.activity_register_id,
                                        "activity_module_id" to it.id,
                                        "flag" to (it.registerModule?.learn_status == "已学完"),
                                        "flag_status" to (it.registerModule?.learn_status ?: ""),
                                        "exam_flag" to (examService?.isExamCourse(it.dic_mod_type) ?: false),
                                        "exam_result_url" to examUrl
                                    )
                                )
                            }
                        } else {
                            //有一层
                            courseList.add(
                                mutableMapOf(
                                    "id" to (index + 1) * 100 + (tempIndex++),
                                    "name" to it.mod_name,
                                    "title_flag" to false,
                                    "activity_register_id" to it.activity_register_id,
                                    "activity_module_id" to it.id,
                                    "flag" to (it?.registerModule?.learn_status == "已学完"),
                                    "flag_status" to (it?.registerModule?.learn_status ?: ""),
                                    "exam_flag" to (examService?.isExamCourse(it.dic_mod_type) ?: false),
                                )
                            )
                        }
                    }
                }
                list.add(
                    mutableMapOf<String, Any>(
                        "id" to index,
                        "title_flag" to true,
                        "flag" to (it.activityRegister?.learn_status == "已学完"),
                        "flag_status" to (it.activityRegister?.learn_status ?: ""),
                        "activity_id" to activityId,
                        "name" to it.activity_name,
                        "course_list" to courseList,
                    )
                )
            }
        }
        return Resp(list)
    }
}
