# The Clear BSD License
#
# Copyright (c) 2024 Julia Burton
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted (subject to the limitations in the disclaimer
# below) provided that the following conditions are met:
#
#      * Redistributions of source code must retain the above copyright notice,
#      this list of conditions and the following disclaimer.
#
#      * Redistributions in binary form must reproduce the above copyright
#      notice, this list of conditions and the following disclaimer in the
#      documentation and/or other materials provided with the distribution.
#
#      * Neither the name of the copyright holder nor the names of its
#      contributors may be used to endorse or promote products derived from this
#      software without specific prior written permission.
#
# NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY
# THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
# CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
# BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

import datetime

import pysolar.solar as ps
from astropy import units as u
from astropy.coordinates import AltAz, EarthLocation, get_body
from astropy.time import Time
from pysolar import constants

from ..general_framework.general_classes import SimpleLocation


def get_refraction(date: datetime, location: SimpleLocation) -> float:
    """The get_refraction function takes a date and location and generates the refraction correction that Pysolar implements for ground based simulation.

    Returns:
        float: refraction correction in degrees
    """

    pressure = constants.standard_pressure
    temperature = constants.standard_temperature

    topocentric_sun_decl, topocentric_local_hour_angle = ps.get_topocentric_position(
        location.latitude, location.latitude, date, location.elevation
    )

    topocentric_elevation_angle = ps.get_topocentric_elevation_angle(
        location.latitude, topocentric_sun_decl, topocentric_local_hour_angle
    )

    refraction_correction = ps.get_refraction_correction(pressure, temperature, topocentric_elevation_angle)

    return refraction_correction


def get_pysolar_solar_elevation(date: datetime, location: SimpleLocation) -> float:
    """The get_solar_elevation() function generates the solar elevation angle. The refraciton correction generated by Pysolar is removed from this angle.

    Returns:
        float: solar elevation angle in degrees
    """

    apparent_elevation_angle = ps.get_altitude(location.latitude, location.longitude, date)

    refraction_correction = get_refraction(date, location)

    solar_elevation_angle = apparent_elevation_angle

    return solar_elevation_angle - refraction_correction


def get_solar_elevation(date: Time, location: SimpleLocation, detector_frame: AltAz = None):
    if detector_frame is None:
        detector_frame = AltAz(obstime=date, location=location.det_loc)
    sun_alt = get_body("sun", date).transform_to(detector_frame).alt.deg
    return sun_alt * u.deg


def get_lunar_elevation(date: Time, location: SimpleLocation, detector_frame: AltAz = None):
    if detector_frame is None:
        detector_frame = AltAz(obstime=date, location=location.det_loc)
    moon_alt = get_body("moon", date).transform_to(detector_frame).alt
    return moon_alt
