# The MIT License (MIT)
# Copyright (c) 2025 by the xcube development team and contributors
#
# 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 NON INFRINGEMENT. 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.

import unittest

import numpy as np
import pyproj.crs

from xcube_resampling.gridmapping import CRS_CRS84, CRS_WGS84, GridMapping

from ..sampledata import create_s2plus_dataset

CRS_UTM_32N = pyproj.crs.CRS(32632)


# noinspection PyMethodMayBeStatic
class TransformTest(unittest.TestCase):
    def test_transform(self):
        gm = GridMapping.regular(
            size=(3, 3), xy_min=(10.05, 53.05), xy_res=0.1, crs=CRS_CRS84
        )
        gm_t = gm.transform(crs=CRS_UTM_32N)

        self.assertEqual(CRS_UTM_32N, gm_t.crs)
        self.assertEqual(False, gm_t.is_regular)
        self.assertEqual(("transformed_x", "transformed_y"), gm_t.xy_var_names)
        self.assertEqual(("lon", "lat"), gm_t.xy_dim_names)
        np.testing.assert_almost_equal(
            gm_t.xy_coords[0],
            np.array(
                [
                    [570057.076286, 576728.9360228, 583400.7295284],
                    [570220.3304187, 576907.7404859, 583595.0849538],
                    [570383.3684844, 577086.3083212, 583789.1831954],
                ]
            ),
        )
        np.testing.assert_almost_equal(
            gm_t.xy_coords[1],
            np.array(
                [
                    [5900595.928991, 5900698.5746648, 5900810.5532744],
                    [5889471.9033896, 5889574.6540572, 5889686.7472201],
                    [5878348.0594403, 5878450.9138481, 5878563.1201969],
                ]
            ),
        )

    def test_transform_xy_var_names(self):
        gm = GridMapping.regular(
            size=(3, 3), xy_min=(10, 53), xy_res=0.1, crs=CRS_CRS84
        )
        gm_t = gm.transform(crs=CRS_UTM_32N, xy_var_names=("x", "y"))
        self.assertEqual(CRS_UTM_32N, gm_t.crs)
        self.assertEqual(("x", "y"), gm_t.xy_var_names)
        self.assertEqual(("lon", "lat"), gm_t.xy_dim_names)

    def test_transform_s2(self):
        dataset = create_s2plus_dataset()

        gm = GridMapping.from_dataset(dataset, prefer_is_regular=True)
        # Assert we've picked the projected one which is regular
        self.assertIn("Projected", gm.crs.type_name)
        self.assertEqual(True, gm.is_regular)

        gm_t = gm.transform(CRS_CRS84)
        self.assertEqual(CRS_CRS84, gm_t.crs)

        gm_t = gm.transform(CRS_WGS84)
        self.assertEqual(CRS_WGS84, gm_t.crs)

    def test_transform_no_op(self):
        dataset = create_s2plus_dataset()

        gm = GridMapping.from_dataset(dataset, prefer_is_regular=True)
        # Assert we've picked the projected one which is regular
        self.assertIn("Projected", gm.crs.type_name)
        self.assertEqual(True, gm.is_regular)

        gm_t = gm.transform(gm.crs)
        self.assertIs(gm, gm_t)

        # Almost no op
        gm = GridMapping.regular(
            size=(3, 3), xy_min=(10, 53), xy_res=0.1, crs=CRS_CRS84
        )
        gm_t = gm.transform(crs=gm.crs, xy_var_names=("x", "y"))
        self.assertEqual(("x", "y"), gm_t.xy_var_names)
