# LICENSE HEADER MANAGED BY add-license-header
#
# Copyright 2018 Kornia Team
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

from typing import Any, Dict, Optional, Tuple, Union

import torch

from kornia.augmentation import random_generator as rg
from kornia.augmentation._2d.geometric.base import GeometricAugmentationBase2D
from kornia.constants import Resample, SamplePadding
from kornia.core.utils import _as_tensor_nonblocking
from kornia.geometry.transform import get_translation_matrix2d, warp_affine


class RandomTranslate(GeometricAugmentationBase2D):
    r"""Translate images by a random 2D shift, applied per sample with probability ``p`` (default 0.5).

    ``output = RandomTranslate(translate_x, translate_y, ...)(input)`` shifts selected images of shape
    ``(B, C, H, W)``.

    Args:
        translate_x: tuple of maximum absolute fraction for horizontal translations.
            For example translate_x=(a, b), then horizontal shift
            is randomly sampled in the range img_width * a < dx < img_width * b
        translate_y: tuple of maximum absolute fraction for vertical translations.
            For example translate_y=(a, b), then vertical shift
            is randomly sampled in the range img_height * a < dy < img_height * b.
        resample: resample mode from "nearest" (0) or "bilinear" (1).
        padding_mode: padding mode from "torch.zeros" (0), "border" (1) or "reflection" (2).
        same_on_batch: apply the same transformation across the batch.
        align_corners: interpolation flag.
        p: probability of applying the transformation to each sample.
        keepdim: whether to keep the output shape the same as input (True) or broadcast it to the batch form (False).

    Shape:
        - Input: ``(C, H, W)`` or ``(B, C, H, W)``
        - Output: ``(B, C, H, W)``

    Convention:
        - See :class:`~kornia.augmentation.GeometricAugmentationBase2D` for coordinates, defaults and inverse.
        - ``translate_x`` and ``translate_y`` are independently sampled fractions of ``W`` and ``H``; positive
          values move content right and down. Omitting either range fixes that axis at zero.

    .. note::
        This function internally uses :func:`kornia.geometry.transform.warp_affine`.

    .. note::
        Unlike torchvision's ``RandomAffine(degrees=0, translate=...)``, which translates every image, this class
        translates each sample with probability ``p``, 0.5 by default; with ``same_on_batch=True`` one draw decides for
        the whole batch. Pass ``p=1.0`` to translate every sample.

    See Also:
        - :class:`RandomAffine`: samples a combined affine transformation.

    Examples:
        >>> import torch
        >>> rng = torch.manual_seed(0)
        >>> input = torch.rand(1, 1, 3, 3)
        >>> aug = RandomTranslate((-0.2, 0.2), (-0.1, 0.1), p=1.)
        >>> out = aug(input)
        >>> out, aug.transform_matrix
        (tensor([[[[0.3897, 0.6847, 0.2314],
                  [0.1384, 0.3297, 0.5771],
                  [0.3749, 0.7565, 0.4780]]]]), tensor([[[ 1.0000,  0.0000,  0.1588],
                 [ 0.0000,  1.0000, -0.0907],
                 [ 0.0000,  0.0000,  1.0000]]]))
        >>> aug.inverse(out)
        tensor([[[[0.3969, 0.5571, 0.1770],
                  [0.1930, 0.3910, 0.4591],
                  [0.4113, 0.6812, 0.4096]]]])

    To apply the exact augmenation again, you may take the advantage of the previous parameter state:
        >>> input = torch.randn(1, 3, 32, 32)
        >>> aug = RandomTranslate((-0.2, 0.2), (-0.1, 0.1), p=1.)
        >>> (aug(input) == aug(input, params=aug._params)).all()
        tensor(True)

    """

    def __init__(
        self,
        translate_x: Optional[Union[torch.Tensor, Tuple[float, float]]] = None,
        translate_y: Optional[Union[torch.Tensor, Tuple[float, float]]] = None,
        resample: Union[str, int, Resample] = Resample.BILINEAR.name,
        same_on_batch: bool = False,
        align_corners: bool = False,
        padding_mode: Union[str, int, SamplePadding] = SamplePadding.ZEROS.name,
        p: float = 0.5,
        keepdim: bool = False,
    ) -> None:
        super().__init__(p=p, same_on_batch=same_on_batch, keepdim=keepdim)
        self._param_generator: rg.TranslateGenerator = rg.TranslateGenerator(translate_x, translate_y)
        self.flags = {
            "resample": Resample.get(resample),
            "padding_mode": SamplePadding.get(padding_mode),
            "align_corners": align_corners,
        }

    def compute_transformation(
        self, input: torch.Tensor, params: Dict[str, torch.Tensor], flags: Dict[str, Any]
    ) -> torch.Tensor:
        translate_x, translate_y = params["translate_x"], params["translate_y"]
        if translate_x.device != translate_y.device:
            # A learned magnitude sits with its operation, and the generator's other draw stays where it was drawn. With
            # both on one device, the stack below is moved once rather than each component: the shared form costs this
            # method 16 to 19 % on CUDA (one extra copy, about 20 us; RTX 4090, torch 2.15).
            translate_x = _as_tensor_nonblocking(translate_x, input.device, input.dtype)
            translate_y = _as_tensor_nonblocking(translate_y, input.device, input.dtype)
        translations = torch.stack([translate_x, translate_y], dim=-1)
        return get_translation_matrix2d(_as_tensor_nonblocking(translations, input.device, input.dtype))

    def apply_transform(
        self,
        input: torch.Tensor,
        params: Dict[str, torch.Tensor],
        flags: Dict[str, Any],
        transform: Optional[torch.Tensor] = None,
    ) -> torch.Tensor:
        _, _, height, width = input.shape
        if not isinstance(transform, torch.Tensor):
            raise TypeError(f"Expected the `transform` be a torch.Tensor. Got {type(transform)}.")

        return warp_affine(
            input,
            transform[:, :2, :],
            (height, width),
            flags["resample"].name.lower(),
            align_corners=flags["align_corners"],
            padding_mode=flags["padding_mode"].name.lower(),
        )

    def inverse_transform(
        self,
        input: torch.Tensor,
        flags: Dict[str, Any],
        transform: Optional[torch.Tensor] = None,
        size: Optional[Tuple[int, int]] = None,
    ) -> torch.Tensor:
        if not isinstance(transform, torch.Tensor):
            raise TypeError(f"Expected the `transform` be a torch.Tensor. Got {type(transform)}.")
        return self.apply_transform(
            input,
            params=self._params,
            transform=torch.as_tensor(transform, device=input.device, dtype=input.dtype),
            flags=flags,
        )
