package com.ruoyi.web.controller.test.student;

import com.google.gson.Gson;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.test.domain.*;
import com.ruoyi.test.domain.DO.AnswerDO;
import com.ruoyi.test.domain.DO.AnswerSheetDO;
import com.ruoyi.test.domain.DO.QuestionAnswerDO;
import com.ruoyi.test.domain.DO.UserDO;
import com.ruoyi.test.domain.Vo.AnswerVo;
import com.ruoyi.test.seriver.*;
import com.ruoyi.web.controller.system.SysUserController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Api(tags = "学生端主页")
@RestController
@RequestMapping("/student/homepage")
public class StudentHomePageController {

    @Autowired
    private IAnswerService iAnswerService;

    @Autowired
    private IMarkedtestService iMarkedtestService;

    @Autowired
    private IAnswerSheetDOService iAnswerSheetDOService;

    @Autowired
    private IQuestionanswerService iQuestionanswerService;

    @Autowired
    private IExamCreateService iExamCreateService;

    @Autowired
    private IStudentClassService iStudentClassService;

    @Autowired
    private ITeacherManageClassService iTeacherManageClassService;

    @Autowired
    private ISysUserService iSysUserService;

    @Autowired
    private IInstructorManageClassService iInstructorManageClassService;

    @Autowired
    private IMessageService iMessageService;

    @ApiOperation("查看消息记录")
    @GetMapping("getMessage")
    public R<List<Message>> getMessage() {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        Long id = sysUser.getUserId();
        return R.ok(iMessageService.selectById(id));
    }

    @ApiOperation("更新消息记录状态")
    @PutMapping("updateMessage")
    public R<Integer> updateMessage(@RequestBody Map<String, Long> params) {
        System.out.println("============="+params);
        Long id = params.get("id");
        System.out.println("================="+id);
        return R.ok(iMessageService.updateMessage(id));
    }

    @ApiOperation("获取用户信息")
    @GetMapping("getUser")
    public R<UserDO> getUser() {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        Long id = sysUser.getUserId();
        SysUser sysUser2 = iSysUserService.selectUserById(id);
        UserDO userDO = new UserDO();
        BeanUtils.copyProperties(sysUser2, userDO);
        StudentClass studentClass = iStudentClassService.selectById(id);
        userDO.setGrade(studentClass.getGrade());
        userDO.setClass1(studentClass.getClass1());
        InstructorManageClass instructorManageClass = iInstructorManageClassService.selectByGradeAndClass1(userDO.getGrade(),userDO.getClass1());
        if(instructorManageClass!=null){
            SysUser sysUser1 = iSysUserService.selectUserById(instructorManageClass.getId());
            userDO.setTeacherName(sysUser1.getUserName());
        }
        return R.ok(userDO);
    }

    @ApiOperation("更新用户信息")
    @PostMapping("updateUser")
    public R<Integer> updateUser(@RequestBody SysUser user) {
        return R.ok(iSysUserService.updateUser(user));
    }

    @ApiOperation("试卷")
    @GetMapping("task_paper")
    public R<AnswerSheetDO> testPaper(String subject, String grade, String name) {
        return R.ok(iAnswerSheetDOService.testPaper(subject, grade, name));
    }

    @ApiOperation("任务中心答题")
    @PostMapping("task_answer")
    public R<String> taskAnswer(@RequestBody AnswerDO answerDO){
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        Long id = sysUser.getUserId();
        List<AnswerVo> answerVos = answerDO.getList();
        Gson gson = new Gson();
        String answerVo = gson.toJson(answerVos);

        Answer answer = new Answer();
        answer.setSenderId(id);
        answer.setTestid(answerDO.getTestid());
        answer.setIdanswer(answerVo);
        answer.setTime(answerDO.getTime());

        if (iAnswerService.answer(answer) != 0) {
            return R.ok("答案提交成功！");
        }
        return R.fail("答案提交失败！");
    }

    @ApiOperation("任务中心分数")
    @GetMapping("task_score")
    public R<List<Markedtest>> taskScore(){
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        Long id = sysUser.getUserId();
        return R.ok(iMarkedtestService.selectmarkedtestListSenderId(id));
    }

    @ApiOperation("获取某一张的分数")
    @GetMapping("getScoreByTestId")
    public R<Markedtest> getScoreByTestId(Long testId) {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        Long id = sysUser.getUserId();
        return R.ok(iMarkedtestService.selectmarkedtestByTestid(id,testId));
    }

    @ApiOperation("查看学生的考试情况(最新)")
    @GetMapping("selectQuestionanswer")
    public R<QuestionAnswerDO> selectQuestionanswer(Long testid) {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        Long id = sysUser.getUserId();
        return R.ok(iQuestionanswerService.selectQuestionanswer(id,testid));
    }

    //客观题自动更改
    @ApiOperation("任务中心客观题批改")
    @GetMapping("task_answerDisplay")
    public R<String> taskAnswerDisplay(Long id) {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        Long id1 = sysUser.getUserId();
        List<Questionbank> list = iExamCreateService.selectListByPid(id);
        Answer answer = iAnswerService.selectByTestId(id1, id);
        QuestionAnswerDO questionAnswerDO = iQuestionanswerService.createTest(list, answer);
        if (iQuestionanswerService.addTest(questionAnswerDO) != 0) {
            return R.ok("该学生客观题已改完！");
        }
        return R.fail("查不到该学生的考试记录！");
    }

    @ApiOperation("任务中心批改主观题")
    @PostMapping("task_markObjectiveQuestions")
    public R<String> taskMarkObjectiveQuestions(@RequestBody QuestionAnswerDO questionAnswerDO) {
        if(iQuestionanswerService.markObjectiveQuestions(questionAnswerDO)!=0){
            return R.ok("批改完成！");
        }
        return R.fail("批改提交失败！");
    }

}
