from __future__ import annotations

import math

EARTH_RADIUS = 6371.0


def insert_spherical(vtk_points, longitude, latitude, depth):
    longitude = math.pi * longitude / 180
    latitude = math.pi * latitude / 180
    h = EARTH_RADIUS - depth
    return vtk_points.InsertNextPoint(
        h * math.cos(longitude) * math.cos(latitude),
        h * math.sin(longitude) * math.cos(latitude),
        h * math.sin(latitude),
    )


def insert_euclidian(vtk_point, longitude, latitude, depth):
    return vtk_point.InsertNextPoint(
        longitude,
        latitude,
        depth,
    )


def interpolate(a_longitude, a_latitude, b_longitude, b_latitude, distance):
    a_xyz = (
        EARTH_RADIUS * math.cos(a_longitude) * math.cos(a_latitude),
        EARTH_RADIUS * math.sin(a_longitude) * math.cos(a_latitude),
        EARTH_RADIUS * math.sin(a_latitude),
    )
    b_xyz = (
        EARTH_RADIUS * math.cos(b_longitude) * math.cos(b_latitude),
        EARTH_RADIUS * math.sin(b_longitude) * math.cos(b_latitude),
        EARTH_RADIUS * math.sin(b_latitude),
    )
    ab_xyz = (b_xyz[0] - a_xyz[0], b_xyz[1] - a_xyz[1], b_xyz[2] - a_xyz[2])
    ab_dist = math.sqrt(
        ab_xyz[0] * ab_xyz[0] + ab_xyz[1] * ab_xyz[1] + ab_xyz[2] * ab_xyz[2]
    )
    ratio = distance / ab_dist
    return (
        ratio * (b_longitude - a_longitude) + a_longitude,
        ratio * (b_latitude - a_latitude) + a_latitude,
    )
