package de.avatic.lcc.service.calculation;

import de.avatic.lcc.model.country.DetourIndex;
import de.avatic.lcc.model.country.IsoCode;
import de.avatic.lcc.model.nodes.Location;
import de.avatic.lcc.model.nodes.Node;
import de.avatic.lcc.repositories.DistanceMatrixRepository;
import de.avatic.lcc.repositories.country.CountryRepository;
import org.springframework.stereotype.Service;

@Service
public class DistanceService {

    private static final double EARTH_RADIUS = 6371.0;
    private final DistanceMatrixRepository distanceMatrixRepository;
    private final CountryRepository countryRepository;

    public DistanceService(DistanceMatrixRepository distanceMatrixRepository, CountryRepository countryRepository) {
        this.distanceMatrixRepository = distanceMatrixRepository;
        this.countryRepository = countryRepository;
    }

    public double getDistance(Node src, Node dest, boolean fast) {
        if (fast) return getDistanceFast(src, dest);

        var distance = distanceMatrixRepository.getDistance(src, dest);

        // TODO do a api call if empty!.
        return distance.map(value -> value.getDistance().intValue()).orElse(0);

    }

    public double getDistanceFast(Integer srcCountryId, Location src, Integer destCountryId, Location dest ) {
        double srcLatitudeRadians = Math.toRadians(src.getLatitude());
        double srcLongitudeRad = Math.toRadians(src.getLongitude());
        double destLatitudeRadians = Math.toRadians(dest.getLatitude());
        double destLongitudeRadians = Math.toRadians(dest.getLongitude());

        double latitudeDifference = destLatitudeRadians - srcLatitudeRadians;
        double longitudeDifference = destLongitudeRadians - srcLongitudeRad;

        double a = Math.sin(latitudeDifference / 2) * Math.sin(latitudeDifference / 2) + Math.cos(srcLatitudeRadians) * Math.cos(destLatitudeRadians) * Math.sin(longitudeDifference / 2) * Math.sin(longitudeDifference / 2);

        IsoCode isoSrc = countryRepository.getById(srcCountryId).orElseThrow().getIsoCode();
        IsoCode isoDest = countryRepository.getById(destCountryId).orElseThrow().getIsoCode();

        return (getDetourIndex(isoSrc, isoDest) * (EARTH_RADIUS * 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a))));
    }

    private double getDistanceFast(Node src, Node dest) {

        double srcLatitudeRadians = Math.toRadians(src.getGeoLat().doubleValue());
        double srcLongitudeRad = Math.toRadians(src.getGeoLng().doubleValue());
        double destLatitudeRadians = Math.toRadians(dest.getGeoLat().doubleValue());
        double destLongitudeRadians = Math.toRadians(dest.getGeoLng().doubleValue());

        double latitudeDifference = destLatitudeRadians - srcLatitudeRadians;
        double longitudeDifference = destLongitudeRadians - srcLongitudeRad;

        double a = Math.sin(latitudeDifference / 2) * Math.sin(latitudeDifference / 2) + Math.cos(srcLatitudeRadians) * Math.cos(destLatitudeRadians) * Math.sin(longitudeDifference / 2) * Math.sin(longitudeDifference / 2);

        return (getDetourIndex(src, dest) * (EARTH_RADIUS * 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a))));
    }

    private double getDetourIndex(Node src, Node dest) {
        DetourIndex srcDetour = DetourIndex.getDetourIndex(countryRepository.getById(src.getCountryId()).orElseThrow().getIsoCode());

        if (src.getCountryId().equals(dest.getCountryId())) return srcDetour.getAverageDetourIndex();

        DetourIndex destDetour = DetourIndex.getDetourIndex(countryRepository.getById(dest.getCountryId()).orElseThrow().getIsoCode());

        return (srcDetour.getAverageDetourIndex() + destDetour.getAverageDetourIndex()) / 2;
    }

    private double getDetourIndex(IsoCode src, IsoCode dest) {
        DetourIndex srcDetour = DetourIndex.getDetourIndex(src);

        if (src.equals(dest)) return srcDetour.getAverageDetourIndex();

        DetourIndex destDetour = DetourIndex.getDetourIndex(dest);

        return (srcDetour.getAverageDetourIndex() + destDetour.getAverageDetourIndex()) / 2;
    }

}
