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

import de.avatic.lcc.model.calculation.ContainerCalculationResult;
import de.avatic.lcc.model.calculation.CustomResult;
import de.avatic.lcc.model.calculation.SectionInfo;
import de.avatic.lcc.dto.generic.RateType;
import de.avatic.lcc.model.db.premises.Premise;
import de.avatic.lcc.model.db.premises.route.Destination;
import de.avatic.lcc.model.db.properties.CountryPropertyMappingId;
import de.avatic.lcc.model.db.properties.CustomUnionType;
import de.avatic.lcc.model.db.properties.SystemPropertyMappingId;
import de.avatic.lcc.repositories.country.CountryPropertyRepository;
import de.avatic.lcc.repositories.premise.RouteNodeRepository;
import de.avatic.lcc.repositories.properties.PropertyRepository;
import de.avatic.lcc.service.api.CustomApiService;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;

@Service
public class CustomCostCalculationService {

    private final CustomApiService customApiService;
    private final CountryPropertyRepository countryPropertyRepository;
    private final RouteNodeRepository routeNodeRepository;
    private final PropertyRepository propertyRepository;
    private final ShippingFrequencyCalculationService shippingFrequencyCalculationService;

    public CustomCostCalculationService(CustomApiService customApiService, CountryPropertyRepository countryPropertyRepository, RouteNodeRepository routeNodeRepository, PropertyRepository propertyRepository, ShippingFrequencyCalculationService shippingFrequencyCalculationService) {
        this.customApiService = customApiService;
        this.countryPropertyRepository = countryPropertyRepository;
        this.routeNodeRepository = routeNodeRepository;
        this.propertyRepository = propertyRepository;
        this.shippingFrequencyCalculationService = shippingFrequencyCalculationService;
    }

    private BigDecimal getContainerShare(Premise premise, ContainerCalculationResult containerCalculationResult) {
        var weightExceeded = containerCalculationResult.isWeightExceeded();
        var mixable = premise.getHuMixable();

        if (mixable) {
            return BigDecimal.valueOf(weightExceeded ? containerCalculationResult.getHuUtilizationByWeight() : containerCalculationResult.getHuUtilizationByVolume());
        } else {
            return BigDecimal.ONE.divide(BigDecimal.valueOf(containerCalculationResult.getHuUnitCount()), 10, RoundingMode.HALF_UP);
        }
    }

    public CustomResult doCalculation(Integer setId, Premise premise, Destination destination, List<SectionInfo> sections) {

        var destUnion = countryPropertyRepository.getByMappingIdAndCountryId(CountryPropertyMappingId.UNION, setId, destination.getCountryId()).orElseThrow();
        var sourceUnion = countryPropertyRepository.getByMappingIdAndCountryId(CountryPropertyMappingId.UNION, setId, premise.getCountryId()).orElseThrow();

        if (CustomUnionType.EU.name().equals(destUnion.getCurrentValue()) && (sourceUnion.getCurrentValue() == null || CustomUnionType.NONE.name().equals(sourceUnion.getCurrentValue()))) {

            var relevantSections = getCustomRelevantRouteSections(setId, sections);

            var transportationCost = relevantSections.stream().map(s -> s.result().getAnnualCost()).reduce(BigDecimal.ZERO, BigDecimal::add);
            var transportationChanceCost = relevantSections.stream().map(s -> s.result().getAnnualChanceCost()).reduce(BigDecimal.ZERO, BigDecimal::add);
            var transportationRiskCost = relevantSections.stream().map(s -> s.result().getAnnualRiskCost()).reduce(BigDecimal.ZERO, BigDecimal::add);


            double huAnnualAmount = BigDecimal.valueOf(destination.getAnnualAmount()).divide(BigDecimal.valueOf(relevantSections.getFirst().containerResult().getHuUnitCount()),2, RoundingMode.HALF_UP).doubleValue();

            return getCustomCalculationResult(setId, premise, destination, getContainerShare(premise, relevantSections.getFirst().containerResult()), huAnnualAmount, transportationCost, transportationChanceCost, transportationRiskCost);
        }

        return CustomResult.EMPTY;
    }

    private CustomResult getCustomCalculationResult(Integer setId, Premise premise, Destination destination, BigDecimal containerShare, double huAnnualAmount, BigDecimal transportationCost, BigDecimal transportationChanceCost, BigDecimal transportationRiskCost) {
        var shippingFrequency = shippingFrequencyCalculationService.doCalculation(setId, huAnnualAmount);
        var customFee = Double.parseDouble(propertyRepository.getPropertyByMappingId(SystemPropertyMappingId.CUSTOM_FEE, setId).orElseThrow().getCurrentValue());

        var tariffRate =  premise.getTariffRate();

        var materialCost = premise.getMaterialCost().multiply(BigDecimal.valueOf(destination.getAnnualAmount()));
        var fcaFee = BigDecimal.ZERO;

        if (premise.getFcaEnabled()) {
            var fcaProperty = propertyRepository.getPropertyByMappingId(SystemPropertyMappingId.FCA_FEE, setId).orElseThrow();
            var fcaShare = Double.parseDouble(fcaProperty.getCurrentValue());
            fcaFee = BigDecimal.valueOf(fcaShare).multiply(materialCost);
        }

        var customValue = materialCost.add(fcaFee).add(transportationCost);
        var customDuties = customValue.multiply(tariffRate);
        var annualCustomFee = BigDecimal.valueOf(shippingFrequency).multiply(BigDecimal.valueOf(customFee)).multiply(containerShare);
        var annualCost = customDuties.add(annualCustomFee);

        var customRiskValue = materialCost.add(fcaFee).add(transportationRiskCost);
        var customRiskDuties = customRiskValue.multiply(tariffRate);
        var annualRiskCost = customRiskDuties.add(annualCustomFee);

        var customChanceValue = materialCost.add(fcaFee).add(transportationChanceCost);
        var customChanceDuties = customChanceValue.multiply(tariffRate);
        var annualChanceCost = customChanceDuties.add(annualCustomFee);

        return new CustomResult(customValue, customRiskValue, customChanceValue, customDuties, tariffRate, annualCost, annualRiskCost, annualChanceCost);
    }

    private List<SectionInfo> getCustomRelevantRouteSections(Integer setId, List<SectionInfo> sections) {

        List<SectionInfo> customSections = new ArrayList<>();

        if (sections.size() == 1 && sections.getFirst().result().getRateType().equals(RateType.D2D)) {
            customSections.add(sections.getFirst());
            return customSections;
        }

        for (SectionInfo section : sections) {
            if (!(CustomUnionType.EU == getCustomUnionByRouteNodeId(setId, section.section().getFromRouteNodeId()) &&
                    CustomUnionType.EU == getCustomUnionByRouteNodeId(setId, section.section().getToRouteNodeId()))) {
                customSections.add(section);
            }
        }

        return customSections;
    }

    private CustomUnionType getCustomUnionByRouteNodeId(Integer setId, Integer routeNodeId) {
        var node = routeNodeRepository.getById(routeNodeId).orElseThrow();
        return getCustomUnionByCountryId(setId, node.getCountryId());
    }

    private CustomUnionType getCustomUnionByCountryId(Integer setId, Integer countryId) {
        var property = countryPropertyRepository.getByMappingIdAndCountryId(CountryPropertyMappingId.UNION, setId, countryId).orElseThrow();
        if (property.getCurrentValue() == null)
            return CustomUnionType.NONE;

        return CustomUnionType.valueOf(property.getCurrentValue().toUpperCase());
    }


}
