package de.avatic.lcc.service.excelMapper;

import de.avatic.lcc.model.bulk.header.HiddenNodeHeader;
import de.avatic.lcc.model.db.nodes.Node;
import de.avatic.lcc.repositories.NodeRepository;
import de.avatic.lcc.service.bulk.helper.HeaderGenerator;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.springframework.stereotype.Service;

@Service
public class HiddenNodeExcelMapper {


    private final HeaderGenerator headerGenerator;
    private final NodeRepository nodeRepository;

    public HiddenNodeExcelMapper(HeaderGenerator headerGenerator, NodeRepository nodeRepository) {
        this.headerGenerator = headerGenerator;
        this.nodeRepository = nodeRepository;
    }

    public void fillSheet(Sheet sheet, CellStyle headerStyle, boolean originOnly){
        headerGenerator.generateHeader(sheet, HiddenNodeHeader.class, headerStyle);
        nodeRepository.listAllNodes(originOnly).forEach(node -> mapToRow(node, sheet.createRow(sheet.getLastRowNum()+1)));
    }

    private void mapToRow(Node node, Row row) {
        row.createCell(HiddenNodeHeader.MAPPING_ID.ordinal()).setCellValue(node.getExternalMappingId());
        row.createCell(HiddenNodeHeader.NAME.ordinal()).setCellValue(node.getName());
    }
}
