package com.koobietech.eas.service.impl;

import com.koobietech.eas.common.constant.FileType;
import com.koobietech.eas.common.utils.FileManager;
import com.koobietech.eas.common.utils.ArchiveManager;
import com.koobietech.eas.dao.dto.ArchivesDto;
import com.koobietech.eas.mbg.mapper.EasArcTlsStudentsMapper;
import com.koobietech.eas.mbg.model.EasArcTlsStudents;
import com.koobietech.eas.mbg.model.EasArcTlsStudentsExample;
import com.koobietech.eas.service.EasArchivesFilesService;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.*;
import java.util.List;

/**
 * @author lc
 */
@Service
public class EasArchivesFilesServiceImpl implements EasArchivesFilesService {

    @Value("${project.path}")
    String archivesSavePath;

    final String BAKUP_SUFFIX =  ".bak";

    @Resource
    FileManager fileManager;

    @Resource
    EasArcTlsStudentsMapper arcTlsStudentsMapper;

    String separator = File.separator;

    @Override
    public ArchivesDto saveArchiveAttendanceFile(String studentNumber, XWPFDocument document) {
        return saveArchiveDocumentFile(studentNumber, document, FileType.XLSX.name());
    }

    @Override
    public ArchivesDto saveArchiveScoresFile(String studentNumber, XWPFDocument document) {
        return saveArchiveDocumentFile(studentNumber, document, FileType.XLSX.name());
    }

    @Override
    public ArchivesDto saveArchiveStudentsFile(String studentNumber, XWPFDocument document) {
        return saveArchiveDocumentFile(studentNumber, document, FileType.DOCX.name());
    }

    @Override
    public ArchivesDto saveArchiveFile(String studentNumber, Workbook workbook) {
        return saveArchiveWorkbookFile(studentNumber, workbook, FileType.XLSX.name());
    }

    @Override
    public ArchivesDto saveArchiveFile(String studentNumber, InputStream stream, String type) {
        String archiveCode = getArchiveCode(studentNumber, FileType.valueOf(type).getSuffix());
        String path = getArchivePath(studentNumber, archiveCode, FileType.valueOf(type).getSuffix());
        boolean status = fileManager.saveFile(stream, path);
        return new ArchivesDto(path, status, archiveCode, type);
    }

    @Override
    public boolean deleteArchiveFile(String filePath) {
        return fileManager.deleteFile(filePath);
    }

    @Override
    public FileInputStream getArchiveFile(String filePath) {
        return fileManager.getArchiveFile(filePath);
    }

    @Override
    public InputStream getResourceStream(String filePath) {
        InputStream resourceAsStream = null;
        try {
            resourceAsStream = this.getClass().getClassLoader().getResourceAsStream(filePath);
        } catch (Exception e) {
        }
        return resourceAsStream;
    }

    @Override
    public EasArcTlsStudents getArchiveStudentsFile(String studentNumber) {
        EasArcTlsStudentsExample easArcTlsStudentsExample = new EasArcTlsStudentsExample();
        easArcTlsStudentsExample.createCriteria().andStudentNumberEqualTo(studentNumber);
        List<EasArcTlsStudents> easArcTlsStudents = arcTlsStudentsMapper.selectByExample(easArcTlsStudentsExample);
        return easArcTlsStudents.stream().findFirst().orElse(null);
    }

    @Override
    public boolean backupArchiveFile(String filePath) {
        boolean ret = false;
        if (filePath == null || filePath.isEmpty()) {
            return ret;
        }
        if ( fileManager.isFileExists(filePath) ) {
            long time = System.currentTimeMillis();
            ret = fileManager.moveFile(filePath, filePath + time + BAKUP_SUFFIX, true);
        }
        return ret;
    }

    @Override
    public String getArchiveUrl(String filePath) {
        String url = null;
        if (filePath == null || filePath.isEmpty()) {
            return url;
        }
        return null;
    }

    private ArchivesDto saveArchiveDocumentFile(String studentNumber, XWPFDocument document, String type) {
        String archiveCode = ArchiveManager.generateArchiveCode(studentNumber, FileType.valueOf(type).getSuffix());
        String path = getArchivePath(studentNumber, archiveCode, FileType.valueOf(type).getSuffix());
        boolean status = fileManager.saveDocument(document, path);
        return new ArchivesDto(path, status, archiveCode, type);
    }

    private ArchivesDto saveArchiveWorkbookFile(String studentNumber, Workbook workbook, String type) {
        String archiveCode = ArchiveManager.generateArchiveCode(studentNumber, FileType.valueOf(type).getSuffix());
        String path = getArchivePath(studentNumber, archiveCode, FileType.valueOf(type).getSuffix());
        boolean status = fileManager.saveWorkbook(workbook, path);
        return new ArchivesDto(path, status, archiveCode, type);
    }

    private String getArchiveCode(String studentNumber, String fileCode) {
        return ArchiveManager.generateArchiveCode(studentNumber, fileCode);
    }

    private String getArchivePath(String studentNumber, String archiveCode, String suffix) {
        String path = archivesSavePath + separator + studentNumber;
        fileManager.createPath(path);
        return path + separator + archiveCode + suffix;
    }

}
