/*
Copyright (c) 2022 Peter Hristozov / Петър Христозов

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

//! Astronomical time.

use crate::datetime::mjd;

/// An approximation to TDB-TT, the difference between barycentric
/// dynamical time and terrestrial time.
/// # Arguments
/// * `tdb_mjd` TDB as Modified Julian Date
/// # Returns
/// TDB-TT as part of day
/// # References
/// Fairhead, L., & Bretagnon, P., Astron.Astrophys., 229, 240-247 (1990).
pub fn tdb_m_tt_lo(tdb_mjd: f64) -> f64 {
    let dtm = (tdb_mjd - mjd::J2000) / mjd::DJMILL;
    let frec = 6283.075849991 * dtm;
    let wf0 = 1656.674564e-6 * (frec + 6.240054195).sin();
    let wf1 = 102.156724e-6 * (frec + 4.249032005).sin();
    (wf0 + wf1 * dtm) / mjd::SECDAY
}

//TODO tcb_m_tdb

//TODO tcg_m_tt

/// Terrestrial Time, TT, to International Atomic Time, TAI.
/// # Arguments
/// # Returns
/// TT - TAI = 32.184 second as part of day.
/// # References
/// Petit, G. and Luzum, B. (eds.), IERS Conventions (2010),
/// IERS Technical Note No. 36, BKG (2010)
pub fn tt_m_tai() -> f64 {
    32.184 / mjd::SECDAY
}

//TODO utc_m_tai

//TODO ut2_mut1

/// An approximate value of UT1 - TAI for a given year.
/// # Arguments
/// * `year` date as tropical epoch
/// # Returns
/// UT1 - TAI as part of day
/// # References
/// Five Millennium Canon of Solar Eclipses (Espenak and Meeus
/// `<https://eclipse.gsfc.nasa.gov/SEcat5/deltatpoly.html>`
pub fn ut1_m_tai(year: f64) -> f64 {
    let dt = 32.184
        - if year < -500.0 {
            let u = (year - 1820.0) / 100.0;
            -20.0 + 32.0 * u * u
        } else if year < 500.0 {
            let u = year / 100.0;
            10583.6
                + (-1014.41
                    + (33.78311
                        + (-5.952053 + (-0.1798452 + (0.022174192 + 0.0090316521 * u) * u) * u)
                            * u)
                        * u)
                    * u
        } else if year < 1600.0 {
            let u = (year - 1000.0) / 100.0;
            1574.2
                + (-556.01
                    + (71.23472
                        + (0.319781 + (-0.8503463 + (-0.005050998 + 0.0083572073 * u) * u) * u)
                            * u)
                        * u)
                    * u
        } else if year < 1700.0 {
            let u = year - 1600.0;
            120.0 + (-0.9808 + (-0.01532 + u / 7129.0) * u) * u
        } else if year < 1800.0 {
            let u = year - 1700.0;
            8.83 + (0.1603 + (-0.0059285 + (0.00013336 - u / 1174000.0) * u) * u) * u
        } else if year < 1860.0 {
            let u = year - 1800.0;
            13.72
                + (-0.332447
                    + (0.0068612
                        + (0.0041116
                            + (-0.00037436
                                + (0.0000121272 + (-0.0000001699 + 0.000000000875 * u) * u) * u)
                                * u)
                            * u)
                        * u)
                    * u
        } else if year < 1900.0 {
            let u = year - 1860.0;
            7.62 + (0.5737
                + (-0.251754 + (0.01680668 + (-0.0004473624 + u / 233174.0) * u) * u) * u)
                * u
        } else if year < 1920.0 {
            let u = year - 1900.0;
            -2.79 + (1.494119 + (-0.0598939 + (0.0061966 - 0.000197 * u) * u) * u) * u
        } else if year < 1941.0 {
            let u = year - 1920.0;
            21.20 + (0.84493 + (-0.076100 + 0.0020936 * u) * u) * u
        } else if year < 1961.0 {
            let u = year - 1950.0;
            29.07 + (0.407 + (-1.0 / 233.0 + u / 2547.0) * u) * u
        } else if year < 1986.0 {
            let u = year - 1975.0;
            45.45 + (1.067 + (-1.0 / 260.0 - u / 718.0) * u) * u
        } else if year < 2005.0 {
            let u = year - 2000.0;
            63.86
                + (0.3345
                    + (-0.060374 + (0.0017275 + (0.000651814 + 0.00002373599 * u) * u) * u) * u)
                    * u
        } else if year <= 2032.0 {
            let u = year - 2000.0;
            //(0.404229283 - 0.00331233402 * u) * u + 62.48
            (0.4042 - 0.003312 * u) * u + 62.48
        } else if year < 2050.0 {
            let u = year - 2000.0;
            62.92 + (0.32217 + 0.005589 * u) * u
        } else if year <= 2150.0 {
            let u = (year - 1820.0) / 100.0;
            -20.0 + 32.0 * u * u - 0.5628 * (2150.0 - year)
        } else {
            let u = (year - 1820.0) / 100.0;
            -20.0 + 32.0 * u * u
        };
    dt / mjd::SECDAY
}
