package com.qmth.teachcloud.mark.service.impl;

import com.qmth.boot.core.exception.StatusException;
import com.qmth.teachcloud.common.enums.UploadFileEnum;
import com.qmth.teachcloud.mark.enums.FormatType;
import com.qmth.teachcloud.mark.service.MarkFileService;
import com.qmth.teachcloud.mark.utils.FileStoreUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.InputStream;

@Service
public class MarkFileServiceImpl implements MarkFileService {

    @Autowired
    private FileStoreUtils fileStoreUtils;

    @Override
    public String getAnswerCardUri(Long examId, String coursePaperId, Integer number) {
        return UploadFileEnum.CARD.getPath(UploadFileEnum.CARD.getTitle(), examId, coursePaperId, number, FormatType.JSON.name().toLowerCase());
    }

    @Override
    public String uploadAnswerCard(InputStream in, String md5, Long examId, String coursePaperId, Integer number) {
        String path = getAnswerCardUri(examId, coursePaperId, number);
        try {
            return fileStoreUtils.uploadFile(in, md5, UploadFileEnum.CARD, path);
        } catch (RuntimeException e) {
            throw e;
        } catch (Exception e) {
            throw new StatusException("文件上传出错:" + path, e);
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                }
            }
        }
    }

    @Override
    public String uploadAdapteFile(InputStream in, String md5, Long examId, String coursePaperId, Integer number) {
        String path = getAdapteFileUri(examId, coursePaperId, number);
        try {
            return fileStoreUtils.uploadFile(in, md5, UploadFileEnum.ADAPT, path);
        } catch (RuntimeException e) {
            throw e;
        } catch (Exception e) {
            throw new StatusException("文件上传出错:" + path, e);
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                }
            }
        }
    }

    @Override
    public String getAdapteFileUri(Long examId, String coursePaperId, Integer cardNumber) {
        return UploadFileEnum.ADAPT.getPath(UploadFileEnum.CARD.getTitle(), examId, coursePaperId, cardNumber, FormatType.JSON.name().toLowerCase());
    }

    @Override
    public String uploadSheet(InputStream in, String md5, Long examId, Long batchId, String coursePaperId,
                              String studentCode, Integer paperNumber, Integer pageIndex) {
        String path = getSheetUri(examId, coursePaperId, studentCode, paperNumber, pageIndex);
        try {
            return fileStoreUtils.uploadFile(in, md5, UploadFileEnum.SHEET, path);
        } catch (RuntimeException e) {
            throw e;
        } catch (Exception e) {
            throw new StatusException("文件上传出错:" + path, e);
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                }
            }
        }
    }

    @Override
    public String getSheetUri(Long examId, String coursePaperId, String studentCode, Integer paperNumber, Integer pageIndex) {
        return UploadFileEnum.SHEET.getPath(UploadFileEnum.SHEET.getTitle(), examId, coursePaperId, studentCode, paperNumber, pageIndex, FormatType.JPG.name().toLowerCase());
    }

    @Override
    public String uploadPackage(InputStream in, String md5, Long examId, String coursePaperId, String packageCode,
                                Integer packageNo) {
        String path = getPackageUri(examId, coursePaperId, packageCode, packageNo);
        try {
            return fileStoreUtils.uploadFile(in, md5, UploadFileEnum.PACKAGE, path);
        } catch (RuntimeException e) {
            throw e;
        } catch (Exception e) {
            throw new StatusException("文件上传出错:" + path, e);
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                }
            }
        }
    }

    @Override
    public String getPackageUri(Long examId, String coursePaperId, String packageCode, Integer packageNo) {
        return UploadFileEnum.PACKAGE.getPath(UploadFileEnum.PACKAGE.getTitle(), examId, coursePaperId, packageCode, packageNo, FormatType.JPG.name().toLowerCase());
    }

    @Override
    public String getScanLogUri(String schoolCode, String deviceCode, String fileName) {
        return UploadFileEnum.LOG.getPath(UploadFileEnum.LOG.getTitle(), schoolCode, deviceCode, fileName);
    }

    @Override
    public String uploadScanLog(InputStream in, String md5, String schoolCode, String deviceCode, String fileName) {
        String path = getScanLogUri(schoolCode, deviceCode, fileName);
        try {
            return fileStoreUtils.uploadFile(in, md5, UploadFileEnum.LOG, path);
        } catch (RuntimeException e) {
            throw e;
        } catch (Exception e) {
            throw new StatusException("文件上传出错:" + path, e);
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                }
            }
        }
    }

    @Override
    public String getDocumentUri(Long examId, String paperNumber, String documentType, String fileName,String format) {
        return UploadFileEnum.DOCUMENT.getPath(UploadFileEnum.DOCUMENT.getTitle(),examId, paperNumber, documentType, fileName, format.toLowerCase());
    }

    @Override
    public String uploadDocument(InputStream in, String md5, Long examId, String paperNumber, String documentType,
                                String fileName,String format) {
        String path = getDocumentUri(examId, paperNumber, documentType, fileName,format);
        try {
            return fileStoreUtils.uploadFile(in, md5, UploadFileEnum.DOCUMENT, path);
        } catch (RuntimeException e) {
            throw e;
        } catch (Exception e) {
            throw new StatusException("文件上传出错:" + path, e);
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                }
            }
        }
    }
}
