package com.cretin.webservices.service.api.exam

import com.cretin.entity.exam.ExamItem1
import com.cretin.entity.exam.ExamItem2
import com.cretin.entity.exam.ExamItem3
import com.cretin.entity.exam.ExamItem4
import com.cretin.webcore.exception.WholeException
import com.cretin.webcore.utils.GsonGet
import com.cretin.webcore.utils.OkHttpUtils
import com.cretin.webdb.service.ITbExamAnswerService
import com.google.gson.JsonSyntaxException
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service

@Service
class ExamService {

    fun submitTestAnswer(cookie: String, examParam: ExamItem4) {
        try {
            val examUrl =
                "https://tlearning-sdbg.tcl.com/api/activityRegister/submitTestAnswer.shtml?ts=${System.currentTimeMillis()}"
            OkHttpUtils.builder().url(examUrl).addHeader("Cookie", cookie).addHeader(
                "User-Agent",
                "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36"
            ).addBody(examParam).postJson().sync()
        } catch (e: Throwable) {
            if(e is JsonSyntaxException){
                throw WholeException("Cookie过期，请重新获取")
            }
            throw WholeException("自动考试失败")
        }
    }

    fun generateAnswerTest(cookie: String, activityRegisterId: String, activityModuleId: String): ExamItem3 {
        val url =
            "https://tlearning-sdbg.tcl.com/api/activityRegister/generateAnswerTest.shtml?ts=${System.currentTimeMillis()}&activity_register_id=${activityRegisterId}&activity_module_id=$activityModuleId"
        val exam = OkHttpUtils.builder().url(url).addHeader("Cookie", cookie).addHeader(
            "User-Agent",
            "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36"
        ).get().sync()
        return GsonGet.getGson().fromJson<ExamItem3>(exam, ExamItem3::class.java)
    }

    fun getActivityList(cookie: String, parent_class_id: String): ExamItem1 {
        try {
            val classListUrl =
                "https://tlearning-sdbg.tcl.com/api/activityMgt/queryActivityList.shtml?ts=${System.currentTimeMillis()}&parent_class_id=$parent_class_id"
            val result = OkHttpUtils.builder().url(classListUrl).addHeader("Cookie", cookie).addHeader(
                "User-Agent",
                "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36"
            ).get().sync()
            return GsonGet.getGson().fromJson<ExamItem1>(result, ExamItem1::class.java).apply {
                if(this.code != 0){
                    throw WholeException("Cookie过期，请重新获取")
                }
            }
        } catch (e: Throwable) {
            if(e is JsonSyntaxException){
                throw WholeException("Cookie过期，请重新获取")
            }
            throw WholeException("资源不存在")
        }
    }

    fun queryLearnerActivityModule(cookie: String, parent_class_id: String, activity_id: String): ExamItem2 {
        try {
            val courseListUrl =
                "https://tlearning-sdbg.tcl.com/api/activityRegister/queryLearnerActivityModule.shtml?ts=${System.currentTimeMillis()}&parent_class_id=$parent_class_id&activity_id=$activity_id"

            val result = OkHttpUtils.builder().url(courseListUrl).addHeader("Cookie", cookie).addHeader(
                "User-Agent",
                "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36"
            ).get().sync()
            println(result)
            return GsonGet.getGson().fromJson<ExamItem2>(result, ExamItem2::class.java).apply {
                if(this.code != 0){
                    throw WholeException("Cookie过期，请重新获取")
                }
            }
        } catch (e: Throwable) {
            if(e is JsonSyntaxException){
                throw WholeException("Cookie过期，请重新获取")
            }
            throw WholeException("资源不存在")
        }
    }

    fun isExamCourse(dicModType: String): Boolean {
        return dicModType == "144"
    }
}