package de.avatic.lcc.service.calculation.execution.steps;

import de.avatic.lcc.model.db.properties.SystemPropertyMappingId;
import de.avatic.lcc.repositories.properties.PropertyRepository;
import org.springframework.stereotype.Service;

@Service
public class ShippingFrequencyCalculationService {

    private final PropertyRepository propertyRepository;

    public ShippingFrequencyCalculationService(PropertyRepository propertyRepository) {
        this.propertyRepository = propertyRepository;
    }

    public int doCalculation(Integer setId, int huAnnualAmount) {
        var minAnnualFrequency = Integer.parseInt(propertyRepository.getPropertyByMappingId(SystemPropertyMappingId.FREQ_MIN, setId).orElseThrow().getCurrentValue());
        var maxAnnualFrequency = Integer.parseInt(propertyRepository.getPropertyByMappingId(SystemPropertyMappingId.FREQ_MAX, setId).orElseThrow().getCurrentValue());

        if (huAnnualAmount > maxAnnualFrequency)
            return maxAnnualFrequency;

        return Math.max(huAnnualAmount, minAnnualFrequency);

    }

    public double doCalculation(Integer setId, double huAnnualAmount) {
        Integer minAnnualFrequency = Integer.parseInt(propertyRepository.getPropertyByMappingId(SystemPropertyMappingId.FREQ_MIN, setId).orElseThrow().getCurrentValue());
        Integer maxAnnualFrequency = Integer.parseInt(propertyRepository.getPropertyByMappingId(SystemPropertyMappingId.FREQ_MAX, setId).orElseThrow().getCurrentValue());

        if (huAnnualAmount > maxAnnualFrequency.doubleValue())
            return maxAnnualFrequency;

        return Math.max(huAnnualAmount, minAnnualFrequency.doubleValue());

    }

}
