package cn.com.qmth.stmms.monitor;

import cn.com.qmth.stmms.biz.exam.model.ExamStudent;
import cn.com.qmth.stmms.biz.exam.service.ExamStudentService;
import cn.com.qmth.stmms.biz.file.service.FileService;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;

/**
 * 兼容老版本的文件下载接口
 */
@Controller("fileDownloadController")
@RequestMapping("/file")
public class FileDownloadController {

    private static Logger log = LoggerFactory.getLogger(FileDownloadController.class);

    @Autowired
    private FileService fileService;

    @Autowired
    private ExamStudentService studentService;

    @RequestMapping("/ft-sheet/{examId}-{campusId}/{subjectCode}/{examNumber}-{index}.jpg")
    public void getSheet(HttpServletResponse response, @PathVariable Integer examId, @PathVariable Integer campusId,
            @PathVariable String subjectCode, @PathVariable String examNumber, @PathVariable Integer index)
            throws Exception {
        try {
            // 重定向模式
            // redirect(response,
            // fileService.getFileServer().concat(fileService.getSheetUri(examId,
            // examNumber, index)));
            // 直接下载模式
            download(response, fileService.downloadSheet(examId, examNumber, index));
        } catch (Exception e) {
            log.error("download sheet error", e);
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
        }
    }

    @RequestMapping("/ft-slice/{examId}-{campusId}/{subjectCode}/{examNumber}-{index}.jpg")
    public void getSlice(HttpServletResponse response, @PathVariable Integer examId, @PathVariable Integer campusId,
            @PathVariable String subjectCode, @PathVariable String examNumber, @PathVariable Integer index)
            throws Exception {
        try {
            ExamStudent student = studentService.findByExamIdAndExamNumber(examId, examNumber);
            if (student != null) {
                // 重定向模式
                // redirect(response,
                // fileService.getFileServer().concat(fileService.getSliceUri(examId,
                // student.getSecretNumber(), index)));
                // 直接下载模式
                download(response, fileService.downloadSlice(examId, student.getSecretNumber(), index));
            } else {
                log.error("download slice error: student not found");
                response.sendError(HttpServletResponse.SC_NOT_FOUND);
            }
        } catch (Exception e) {
            log.error("download slice error", e);
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
        }
    }

    @RequestMapping("/ft-package/{examId}/{packageCode}/{index}.jpg")
    public void getPackage(HttpServletResponse response, @PathVariable Integer examId, @PathVariable String packageCode,
            @PathVariable Integer index) throws Exception {
        try {
            // 重定向模式
            // redirect(response, fileService.getFileServer()
            // .concat(fileService.getPackageUri(examId, packageCode, index)));
            // 直接下载模式
            download(response, fileService.downloadPackage(examId, packageCode, index));
        } catch (Exception e) {
            log.error("download package error", e);
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
        }
    }

    private void redirect(HttpServletResponse response, String url) throws Exception {
        response.sendRedirect(url);
    }

    private void download(HttpServletResponse response, byte[] data) throws Exception {
        IOUtils.copy(new ByteArrayInputStream(data), response.getOutputStream());
    }

}
