package cn.teammodel.controller.frontend;

import cn.teammodel.common.IdRequest;
import cn.teammodel.common.R;
import cn.teammodel.model.dto.Appraise.*;
import cn.teammodel.model.entity.appraise.Appraise;
import cn.teammodel.model.vo.appraise.AppraiseRecordVo;
import cn.teammodel.model.vo.appraise.StudentReportVo;
import cn.teammodel.service.EvaluationService;
import com.itextpdf.text.DocumentException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author winter
 * @create 2023-11-22 15:10
 */
@RestController
@RequestMapping("/appraise")
@Api(tags = "学生评价")
public class AppraiseController {
    @Resource
    private EvaluationService evaluationService;

    @PostMapping("getTrees")
    @ApiOperation(value = "获取评价树", notes = "获取评价树")
    public R<Appraise> getEvaluateTree(@RequestBody @Valid GetEvaluateTreeDto getEvaluateTreeDto) {
        Appraise appraise = evaluationService.getTree(getEvaluateTreeDto);
        return R.success(appraise);
    }

    @PostMapping("insertNode")
    @ApiOperation(value = "新增评价树的节点")
    public R<Appraise> insertNode(@RequestBody @Valid InsertNodeDto insertNodeDto) {
        Appraise appraise = evaluationService.insertNode(insertNodeDto);
        return R.success(appraise);
    }
    @PostMapping("updateNode")
    @ApiOperation(value = "更新评价树的节点", notes = "传递更新后的节点,而不是局部更新的值")
    public R<Appraise> updateTree(@RequestBody @Valid UpdateNodeDto updateNodeDto) {
        // fixme: 更新一二级节点应该同时更新三级的 path
        Appraise appraise = evaluationService.updateNode(updateNodeDto);
        return R.success(appraise);
    }
    @PostMapping("deleteNode")
    @ApiOperation(value = "删除评价树的节点")
    public R<Appraise> deleteNode(@RequestBody @Valid DeleteNodeDto deleteNodeDto) {
        Appraise appraise = evaluationService.deleteNode(deleteNodeDto);
        return R.success(appraise);
    }

    @PostMapping("vote")
    @ApiOperation(value = "给某个学生评价(投票)")
    public R<String> vote(@RequestBody @Valid AppraiseVoteDto appraiseVoteDto) {
        evaluationService.vote(appraiseVoteDto);
        return R.success("评价成功");
    }
    @PostMapping("recallVote")
    @ApiOperation(value = "撤回给某个学生评价(投票)")
    public R<String> recallVote(@RequestBody @Valid RecallVoteDto recallVoteDto) {
        evaluationService.recallVote(recallVoteDto);
        return R.success("撤回评价成功");
    }

    @PostMapping("findVoteRecord")
    @ApiOperation(value = "多条件查询当前登录老师的学生评价(投票)")
    public R<List<AppraiseRecordVo>> findMyVoteRecord(@Valid @RequestBody FindVoteRecordDto findVoteRecordDto, HttpServletRequest request) {
        List<AppraiseRecordVo> res = evaluationService.findVoteRecord(findVoteRecordDto,request);
        return R.success(res);
    }

    @PostMapping("studentReport")
    @ApiOperation(value = "查看学生当前的学期的实时评价报告")
    public R<StudentReportVo> studentReport(@Valid @RequestBody IdRequest idRequest) {
        StudentReportVo res = evaluationService.studentReport(idRequest);
        return R.success(res);
    }

    @PostMapping("Reports")
    @ApiOperation(value = "查看多个学生当前的学期的实时评价报告")
    public R<List<StudentReportVo>> Reports(@Valid @RequestBody ReportDto reportDto) {
        List<StudentReportVo> res = evaluationService.Reports(reportDto);
        return R.success(res);
    }

    @PostMapping("studentReportPDF")
    @ApiOperation(value = "导出学生当前的学期的实时评价报告 PDF")
    public void exportStuReportPdf(@Valid @RequestBody IdRequest idRequest, HttpServletResponse response) throws DocumentException, IOException {
        evaluationService.exportStuReportPdf(idRequest, response);
    }

}
