package de.avatic.lcc.service.bulk;

import de.avatic.lcc.dto.bulk.BulkFileType;
import de.avatic.lcc.dto.bulk.BulkProcessingType;
import de.avatic.lcc.dto.bulk.BulkStatus;
import de.avatic.lcc.model.bulk.BulkFileTypes;
import de.avatic.lcc.service.excelMapper.*;
import de.avatic.lcc.util.exception.badrequest.FileFormatNotSupportedException;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;

@Service
public class BulkFileProcessingService {

    private final MatrixRateExcelMapper matrixRateExcelMapper;
    private final ContainerRateExcelMapper containerRateExcelMapper;
    private final MaterialExcelMapper materialExcelMapper;
    private final PackagingExcelMapper packagingExcelMapper;
    private final NodeExcelMapper nodeExcelMapper;
    private final BulkStatusService bulkStatusService;

    public BulkFileProcessingService(MatrixRateExcelMapper matrixRateExcelMapper, ContainerRateExcelMapper containerRateExcelMapper, MaterialExcelMapper materialExcelMapper, PackagingExcelMapper packagingExcelMapper, NodeExcelMapper nodeExcelMapper, BulkStatusService bulkStatusService) {
        this.matrixRateExcelMapper = matrixRateExcelMapper;
        this.containerRateExcelMapper = containerRateExcelMapper;
        this.materialExcelMapper = materialExcelMapper;
        this.packagingExcelMapper = packagingExcelMapper;
        this.nodeExcelMapper = nodeExcelMapper;
        this.bulkStatusService = bulkStatusService;
    }

    public Integer processFile(BulkFileType type, BulkProcessingType processingType, MultipartFile file) {

        //TODO: launch parallel task

        String contentType = file.getContentType();
        if (!"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet".equals(contentType) &&
                !"application/vnd.ms-excel".equals(contentType)) {
            throw new FileFormatNotSupportedException(contentType);
        }

        try (InputStream in = file.getInputStream()) {
            Workbook workbook = new XSSFWorkbook(in);
            Sheet sheet = workbook.getSheet(BulkFileTypes.valueOf(type.name()).getSheetName());

            //todo: if processing type = append than merge with current data, if full, delete old data
            switch (type) {
                case CONTAINER_RATE:
                    var containerRates = containerRateExcelMapper.extractSheet(sheet);
                    break;
                case COUNTRY_MATRIX:
                    var matrixRates = matrixRateExcelMapper.extractSheet(sheet);
                    break;
                case MATERIAL:
                    var materials = materialExcelMapper.extractSheet(sheet);
                    break;
                case PACKAGING:
                    var packaging = packagingExcelMapper.extractSheet(sheet);
                    break;
                case NODE:
                    var nodes = nodeExcelMapper.extractSheet(sheet);
                    // check predecessors chains for loops or contradictions
                    break;
                default:

            }

        } catch (Exception e) {

        }

        return null;
    }

    public BulkStatus getStatus(Integer id) {
        return bulkStatusService.getStatus(id);
    }
}
