package com.qmth.distributed.print.api.mark;

import com.qmth.boot.api.annotation.Aac;
import com.qmth.boot.api.constant.ApiConstant;
import com.qmth.teachcloud.common.entity.SysUser;
import com.qmth.teachcloud.common.enums.mark.MarkMode;
import com.qmth.teachcloud.common.service.SysUserService;
import com.qmth.teachcloud.common.util.Result;
import com.qmth.teachcloud.common.util.ResultUtil;
import com.qmth.teachcloud.common.util.ServletUtil;
import com.qmth.teachcloud.mark.entity.MarkUserGroup;
import com.qmth.teachcloud.mark.params.MarkResult;
import com.qmth.teachcloud.mark.service.MarkService;
import com.qmth.teachcloud.mark.service.MarkUserGroupService;
import com.qmth.teachcloud.mark.utils.TaskLock;
import com.qmth.teachcloud.mark.utils.TaskLockUtil;
import io.lettuce.core.GeoArgs.Sort;
import io.swagger.annotations.Api;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 评卷端接口 前端控制器
 * </p>
 *
 * @author xf
 * @since 2023-09-22
 */
@Api(tags = "评卷-评卷接口")
@RestController
@RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + "/mark")
public class MarkController extends BaseController {

    @Autowired
    private MarkService markService;
    @Resource
    private MarkUserGroupService markUserGroupService;
    @Autowired
    private SysUserService userService;

    @PostMapping(value = "/")
    @Aac(auth = false)
    public Object getTaskLock(@RequestParam(required = false) Long examId,
                              @RequestParam(required = false) String paperNumber,
                              @RequestParam(required = false) Integer groupNumber) {
        Map<String, TaskLock> formalTaskMap = TaskLockUtil.getFormalTaskMap();
        if (examId != null && StringUtils.isNotBlank(paperNumber) && groupNumber != null) {
            TaskLock taskLock = TaskLockUtil.getFormalTask(examId + "_" + paperNumber + "_" + groupNumber);
            return taskLock.list();
        }
        Map<String, List<Map<String, Object>>> map = new HashMap<>();
        for (Map.Entry<String, TaskLock> entry : formalTaskMap.entrySet()) {
            map.put(entry.getKey(), entry.getValue().list());
        }
        return map;
    }

    @RequestMapping(value = "/getSetting", method = RequestMethod.POST)
    public Result getSetting(@RequestParam Long examId, @RequestParam String paperNumber,
                             @RequestParam Integer groupNumber) {
        SysUser user = (SysUser) ServletUtil.getRequestUser();
        return ResultUtil.ok(markService.getSetting(user, examId, paperNumber, groupNumber));
    }

    @RequestMapping(value = "/getStatus", method = RequestMethod.POST)
    public Result status(@RequestParam Long examId, @RequestParam String paperNumber,
                         @RequestParam Integer groupNumber) {
        validMarkPaperForMark(examId, paperNumber);
        SysUser user = (SysUser) ServletUtil.getRequestUser();
        return ResultUtil.ok(markService.getStatus(user.getId(), examId, paperNumber, groupNumber));
    }

    @RequestMapping(value = "/getGroup", method = RequestMethod.POST)
    @ResponseBody
    public Result getGroup(@RequestParam Long examId, @RequestParam String paperNumber) {
        SysUser user = (SysUser) ServletUtil.getRequestUser();
        return ResultUtil.ok(markService.getGroup(user.getId(), examId, paperNumber));
    }

    @RequestMapping(value = "/getTask", method = RequestMethod.POST)
    public Result getTask(@RequestParam Long examId, @RequestParam String paperNumber,
                          @RequestParam Integer groupNumber) {
        SysUser user = (SysUser) ServletUtil.getRequestUser();
        return ResultUtil.ok(markService.getTask(user.getId(), examId, paperNumber, groupNumber));
    }

    @RequestMapping(value = "/saveTask", method = RequestMethod.POST)
    public Result saveTask(HttpServletRequest request, @RequestParam Long examId, @RequestParam String paperNumber,
                           @RequestParam Integer groupNumber, @RequestBody MarkResult markResult) {
        validMarkPaperForMark(examId, paperNumber);
        SysUser user = (SysUser) ServletUtil.getRequestUser();
        return ResultUtil.ok(markService.saveTask(examId, paperNumber, groupNumber, user.getId(), markResult));
    }

    @RequestMapping(value = "/getHistory", method = RequestMethod.POST)
    public Result getHistory(@RequestParam int pageNumber, @RequestParam int pageSize, @RequestParam String order, @RequestParam String sort,
                             @RequestParam Long examId, @RequestParam String paperNumber,
                             @RequestParam Integer groupNumber, @RequestParam(required = false) String secretNumber, @RequestParam(required = false) Double markerScore) {
        SysUser user = (SysUser) ServletUtil.getRequestUser();
        return ResultUtil.ok(markService.getHistory(user.getId(), pageNumber, pageSize, "DESC".equalsIgnoreCase(sort) ? Sort.desc : Sort.asc, order, examId, paperNumber, groupNumber, secretNumber, markerScore));
    }

    @RequestMapping(value = "/updateSetting", method = RequestMethod.POST)
    public Result updateSetting(@RequestParam Long examId,
                                @RequestParam String paperNumber,
                                @RequestParam Integer groupNumber,
                                @RequestParam String uiSetting,
                                @RequestParam MarkMode mode) {
        SysUser user = (SysUser) ServletUtil.getRequestUser();
        userService.updateUiSetting(user.getId(), StringEscapeUtils.unescapeHtml(StringUtils.trimToNull(uiSetting)));
        markUserGroupService.updateMode(examId, paperNumber, groupNumber, user.getId(), mode);
        return ResultUtil.ok(true);
    }

    @RequestMapping(value = "/clear", method = RequestMethod.POST)
    public Result clear(@RequestParam Long examId, @RequestParam String paperNumber,
                        @RequestParam Integer groupNumber) {
        SysUser user = (SysUser) ServletUtil.getRequestUser();
        markService.clear(user.getId(), examId, paperNumber, groupNumber);
        return ResultUtil.ok(true);
    }
}
