# Licensed under a 3-clause BSD style license - see LICENSE.rst

# pylint: disable=invalid-name
from math import cos, sin

import pytest
import numpy as np
from numpy.testing import assert_allclose

import astropy.units as u
from astropy.tests.helper import assert_quantity_allclose

from astropy.modeling import models
from astropy.modeling import rotations
from astropy.wcs import wcs


@pytest.mark.parametrize(('inp'), [(0, 0), (4000, -20.56), (-2001.5, 45.9),
                                   (0, 90), (0, -90), (np.mgrid[:4, :6]),
                                   ([[1, 2, 3], [4,   5,  6]],
                                    [[7, 8, 9], [10, 11, 12]]),
                                   ([[[1,   2,  3,  4],
                                      [5,   6,  7,  8],
                                      [9,  10, 11, 12]],
                                     [[13, 14, 15, 16],
                                      [17, 18, 19, 20],
                                      [21, 22, 23, 24]]],
                                    [[[25, 26, 27, 28],
                                      [29, 30, 31, 32],
                                      [33, 34, 35, 36]],
                                     [[37, 38, 39, 40],
                                      [41, 42, 43, 44],
                                      [45, 46, 47, 48]]])])
def test_against_wcslib(inp):
    w = wcs.WCS()
    crval = [202.4823228, 47.17511893]
    w.wcs.crval = crval
    w.wcs.ctype = ['RA---TAN', 'DEC--TAN']

    lonpole = 180
    tan = models.Pix2Sky_TAN()
    n2c = models.RotateNative2Celestial(crval[0], crval[1], lonpole)
    c2n = models.RotateCelestial2Native(crval[0], crval[1], lonpole)
    m = tan | n2c
    minv = c2n | tan.inverse

    radec = w.wcs_pix2world(inp[0], inp[1], 1)
    xy = w.wcs_world2pix(radec[0], radec[1], 1)

    assert_allclose(m(*inp), radec, atol=1e-12)
    assert_allclose(minv(*radec), xy, atol=1e-12)


@pytest.mark.parametrize(('inp'), [(1e-5, 1e-4), (40, -20.56), (21.5, 45.9),
                                   ([[1, 2, 3], [4,   5,  6]],
                                    [[7, 8, 9], [10, 11, 12]]),
                                   ([[[1,   2,  3,  4],