package de.avatic.lcc.service.excelMapper;

import de.avatic.lcc.model.bulk.header.HiddenCountryHeader;
import de.avatic.lcc.model.db.country.Country;
import de.avatic.lcc.repositories.country.CountryRepository;
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 HiddenCountryExcelMapper {


    private final HeaderGenerator headerGenerator;
    private final CountryRepository countryRepository;

    public HiddenCountryExcelMapper(HeaderGenerator headerGenerator, CountryRepository countryRepository) {
        this.headerGenerator = headerGenerator;
        this.countryRepository = countryRepository;
    }

    public void fillSheet(Sheet sheet, CellStyle headerStyle) {
        headerGenerator.generateHeader(sheet, HiddenCountryHeader.class, headerStyle);
        countryRepository.listAllCountries().forEach(c -> mapToRow(c, sheet.createRow(sheet.getLastRowNum() + 1)));
    }


    private void mapToRow(Country entity, Row row) {
        row.createCell(HiddenCountryHeader.ISO_CODE.ordinal()).setCellValue(entity.getIsoCode().getCode());
        row.createCell(HiddenCountryHeader.NAME.ordinal()).setCellValue(entity.getIsoCode().getFullName());
    }
}
