# 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.
#

import math
from typing import Dict, Optional

import torch
from torch import nn

from kornia.core.check import KORNIA_CHECK_LAF, KORNIA_CHECK_SHAPE
from kornia.core.download import hf_url, load_state_dict_from_url
from kornia.filters.kernels import get_gaussian_kernel2d
from kornia.filters.sobel import SpatialGradient

from ._layout import _patch_conv_layout
from .laf import (
    ellipse_to_laf,
    extract_patches_from_pyramid,
    get_laf_orientation,
    get_laf_scale,
    laf_is_valid,
    make_upright,
    scale_laf,
    set_laf_orientation,
)

urls: Dict[str, str | list[str]] = {}
urls["affnet"] = [
    hf_url("affnet", "AffNet.pth"),
    "https://github.com/ducha-aiki/affnet/raw/master/pretrained/AffNet.pth",
]


class PatchAffineShapeEstimator(nn.Module):
    r"""Estimate the affine shape of patches from the second-moment matrix of their gradients.

    ``abc = PatchAffineShapeEstimator(patch_size)(patches)`` takes ``(B, 1, patch_size, patch_size)`` patches and
    returns the second-moment matrix of each patch's Gaussian-weighted gradients as the ellipse coefficients
    ``(a, b, c)`` that :func:`ellipse_to_laf` reads, ``(B, 1, 3)``, divided by the largest entry, as in
    :cite:`baumberg2000`. It returns coefficients, not LAFs: to adapt LAFs on an image, use
    :class:`LAFAffineShapeEstimator`, which runs it on each LAF's patch.

    For float16 and bfloat16 inputs, the gradients, Gaussian weighting, moments, and normalization are computed
    in float32; for a float64 input, the Gaussian weighting is built in float64.
    The output keeps the input dtype.

    Convention:
        - The output :math:`(a, b, c)` is :math:`(\langle g_x^2 \rangle, \langle g_x g_y \rangle,
          \langle g_y^2 \rangle)`, the means over the patch of the products of its Sobel gradients, x right and
          y down, each gradient weighted by a Gaussian centred on the patch. It is divided by its largest entry, which
          is therefore 1. When two of the three entries are below ``eps`` before that division -- a flat or
          low-contrast patch, or one whose gradient lies along an image axis -- the output is :math:`(1, 0, 1)`, a
          circle.
        - The patch must be ``patch_size`` pixels square.
        - Known defects: a negative :math:`b` counts as below ``eps``, so a patch and its mirror image can get a thin
          ellipse and a circle (`#5824 <https://github.com/kornia/kornia/issues/5824>`_).

    Args:
        patch_size: the input image patch size.
        eps: for safe division.

    See Also:
        - :class:`LAFAffineShapeEstimator`: runs it on each LAF's patch and adapts the LAF's shape to the result.
        - :class:`LAFAffNetShapeEstimator`: a learned shape, which AffNet predicts on each LAF's patch.
        - :class:`PatchDominantGradientOrientation`: the orientation of a patch, from the same Sobel gradients.
        - :func:`ellipse_to_laf`: turns a centre and these coefficients into a LAF.

    """

    def __init__(self, patch_size: int = 19, eps: float = 1e-10) -> None:
        super().__init__()
        self.patch_size: int = patch_size
        self.gradient: nn.Module = SpatialGradient("sobel", 1)
        self.eps: float = eps
        sigma: float = float(self.patch_size) / math.sqrt(2.0)
        # non-persistent: derived from `patch_size`, so it stays out of `state_dict()` but follows `.to()`
        self.register_buffer(
            "weighting",
            get_gaussian_kernel2d((self.patch_size, self.patch_size), (sigma, sigma), True),
            persistent=False,
        )

    def __repr__(self) -> str:
        return f"{self.__class__.__name__}(patch_size={self.patch_size}, eps={self.eps})"

    def forward(self, patch: torch.Tensor) -> torch.Tensor:
        """Run forward.

        Args:
            patch: ``(B, 1, patch_size, patch_size)``

        Returns:
            torch.Tensor: ellipse_shape ``(B, 1, 3)``

        """
        KORNIA_CHECK_SHAPE(patch, ["B", "1", str(self.patch_size), str(self.patch_size)])
        dtype = patch.dtype
        if dtype in (torch.float16, torch.bfloat16):
            # Squared weighted gradients and the degeneracy threshold can underflow in half precision.
            patch = patch.float()
        if dtype in (torch.float16, torch.bfloat16, torch.float64):
            # The buffer holds the kernel rounded to float32, or coarser after `.to(dtype)`: rebuild it in the
            # compute dtype instead of upcasting coefficients that have already lost precision.
            sigma = float(self.patch_size) / math.sqrt(2.0)
            weighting = get_gaussian_kernel2d(
                (self.patch_size, self.patch_size),
                (sigma, sigma),
                True,
                device=patch.device,
                dtype=patch.dtype,
            )
        else:
            # Cast into a local; rebinding `self.weighting` would make the module's dtype/device depend
            # on whichever tensor was passed last.
            weighting = self.weighting.to(device=patch.device, dtype=patch.dtype)
        grads: torch.Tensor = self.gradient(patch) * weighting
        # unpack the edges
        gx: torch.Tensor = grads[:, :, 0]
        gy: torch.Tensor = grads[:, :, 1]
        # abc == 1st axis, mixture, 2nd axis. Ellipse_shape is a 2nd moment matrix.
        ellipse_shape = torch.cat(
            [
                gx.pow(2).mean(dim=2).mean(dim=2, keepdim=True),
                (gx * gy).mean(dim=2).mean(dim=2, keepdim=True),
                gy.pow(2).mean(dim=2).mean(dim=2, keepdim=True),
            ],
            dim=2,
        )

        # Now lets detect degenerate cases: when 2 or 3 elements are close to zero (e.g. if patch is completely black
        bad_mask = (ellipse_shape < self.eps).sum(dim=2, keepdim=True) >= 2
        # We will replace degenerate shape with circular shapes.
        circular_shape = ellipse_shape.new_tensor([1.0, 0.0, 1.0]).view(1, 1, 3)
        ellipse_shape = torch.where(bad_mask, circular_shape, ellipse_shape)
        # normalization
        ellipse_shape = ellipse_shape / ellipse_shape.max(dim=2, keepdim=True)[0]
        return ellipse_shape.to(dtype)


def _sanitize_laf(laf: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor]:
    invalid = ~laf_is_valid(laf)
    unit = laf.new_tensor([[1.0, 0.0], [0.0, 1.0]]).expand(*laf.shape[:2], -1, -1)
    center = torch.where(laf[..., 2:3].isfinite(), laf[..., 2:3], torch.zeros_like(laf[..., 2:3]))
    replacement = torch.cat([unit, center], dim=-1)
    return torch.where(invalid[..., None, None], replacement, laf), invalid


def _compose_with_frame(shape_laf: torch.Tensor, frame: torch.Tensor) -> torch.Tensor:
    # A shape estimated on a patch sampled through `frame` is expressed in the patch coordinates; the image
    # frame of that region is `frame @ shape`. Dividing the frame by its scale first turns the upright frame of an
    # isotropic input into an identity factor up to rounding, so such an input gets the estimated shape itself; the
    # caller rescales the result to the input's scale.
    frame_shape = frame[..., :2] / get_laf_scale(frame)
    return torch.cat([frame_shape @ shape_laf[..., :2], shape_laf[..., 2:]], dim=-1)


def _finalize_laf(
    laf_out: torch.Tensor,
    laf: torch.Tensor,
    preserve_orientation: bool,
    bad: torch.Tensor,
    make_candidate_upright: bool = False,
) -> torch.Tensor:
    safe_input, invalid_input = _sanitize_laf(laf)
    bad = bad | invalid_input
    safe_fallback = safe_input if preserve_orientation else make_upright(safe_input)
    if preserve_orientation:
        fallback = laf
    else:
        zero_scale_upright = torch.cat([torch.zeros_like(laf[..., :2]), laf[..., 2:3]], dim=-1)
        fallback = torch.where(invalid_input[..., None, None], zero_scale_upright, safe_fallback)
    # This first selection is the backward-safety boundary: scale and orientation operations must only
    # see nonsingular LAFs. Candidate upright conversion must stay between this selection and the final one.
    safe_laf_out = torch.where(bad[..., None, None], safe_fallback, laf_out)
    scale_orig = get_laf_scale(safe_input)
    if preserve_orientation:
        ori_orig = get_laf_orientation(safe_input)
    ellipse_scale = get_laf_scale(safe_laf_out)
    if make_candidate_upright:
        safe_laf_out = make_upright(safe_laf_out)
    safe_laf_out = scale_laf(safe_laf_out, scale_orig / ellipse_scale)
    if preserve_orientation:
        safe_laf_out = set_laf_orientation(safe_laf_out, ori_orig)
    return torch.where(bad[..., None, None], fallback, safe_laf_out)


class LAFAffineShapeEstimator(nn.Module):
    r"""Adapt the affine shape of LAFs to the second-moment matrix of each LAF's image patch.

    ``lafs = LAFAffineShapeEstimator(patch_size)(laf, img)`` takes ``(B, N, 2, 3)`` LAFs in pixels and their
    grayscale images ``(B, 1, H, W)``, samples each LAF's patch through its upright frame, estimates the shape with
    ``affine_shape_detector`` (:class:`PatchAffineShapeEstimator` by default) and returns the ``(B, N, 2, 3)`` LAFs
    with the adapted shape and the input's centres and scales. For the shape that AffNet predicts, use
    :class:`LAFAffNetShapeEstimator` instead; to drop the rotation and the reflection and keep the shape, without
    reading the image, use :func:`make_upright`.

    Convention:
        - Each patch is sampled through the upright form of its LAF (:func:`~kornia.feature.make_upright`), so the
          shape is estimated in the coordinates of that frame, and the output region is the frame composed with the
          estimated shape. The estimated shape's ellipse :math:`A A^\top` is proportional to the inverse of the second
          moment matrix that ``affine_shape_detector`` returns. For an isotropic input LAF (a circle), the output shape
          is the estimated shape itself. An input whose shape already matches the local structure gives a nearly
          isotropic patch, so its shape changes little: one call is one step of the iterative shape adaptation of
          :cite:`baumberg2000`.
        - The centre and the scale of the input LAF are kept. With ``preserve_orientation=True`` (the default) its
          orientation is kept too; otherwise the output is upright, and an orientation estimator such as
          :class:`~kornia.feature.LAFOrienter` can be run after this module.
        - An adapted LAF has a positive determinant: a mirrored input loses its reflection. A LAF that
          :func:`~kornia.feature.laf_is_valid` rejects, such as the all-zero padding of a detector's unfilled slot,
          comes back unchanged with ``preserve_orientation=True``, and with a zero ``A`` and its centre otherwise. A
          valid LAF whose patch gives no finite, positive-definite shape is not adapted either: it comes back
          unchanged with ``preserve_orientation=True``, and as :func:`~kornia.feature.make_upright` returns it
          otherwise.

    Args:
        patch_size: the input image patch size.
        affine_shape_detector: Patch affine shape estimator, :class:`~kornia.feature.PatchAffineShapeEstimator`.
        preserve_orientation: if True, the original orientation is preserved.

    See Also:
        - :class:`LAFAffNetShapeEstimator`: the same adaptation, with the shape that AffNet predicts.
        - :class:`PatchAffineShapeEstimator`: the default ``affine_shape_detector``, on patches.
        - :func:`make_upright`: drops the rotation and the reflection of a LAF and keeps its shape, without the image.
        - :class:`LAFOrienter`: estimates the orientation, run after this module with ``preserve_orientation=False``.

    """  # pylint: disable

    def __init__(
        self, patch_size: int = 32, affine_shape_detector: Optional[nn.Module] = None, preserve_orientation: bool = True
    ) -> None:
        super().__init__()
        self.patch_size = patch_size
        self.affine_shape_detector = affine_shape_detector or PatchAffineShapeEstimator(self.patch_size)
        self.preserve_orientation = preserve_orientation

    def __repr__(self) -> str:
        return (
            f"{self.__class__.__name__}"
            f"(patch_size={self.patch_size}, "
            f"affine_shape_detector={self.affine_shape_detector}, "
            f"preserve_orientation={self.preserve_orientation})"
        )

    def forward(self, laf: torch.Tensor, img: torch.Tensor) -> torch.Tensor:
        """Run forward.

        Args:
            laf: ``(B, N, 2, 3)``
            img: ``(B, 1, H, W)``

        Returns:
            LAF_out: ``(B, N, 2, 3)``

        """
        KORNIA_CHECK_LAF(laf)
        KORNIA_CHECK_SHAPE(img, ["B", "1", "H", "W"])
        B, N = laf.shape[:2]
        PS: int = self.patch_size
        safe_input, _ = _sanitize_laf(laf)
        frame = make_upright(safe_input)
        patches: torch.Tensor = extract_patches_from_pyramid(img, frame, PS, True).view(-1, 1, PS, PS)
        ellipse_shape: torch.Tensor = self.affine_shape_detector(patches)
        ellipse_det = ellipse_shape[..., 0] * ellipse_shape[..., 2] - ellipse_shape[..., 1].square()
        # ellipse_to_laf takes the square root of a - (b / sqrt(c))^2, formed as below. For a nearly singular
        # shape that can round to zero or below although the determinant above stays positive; its nan would be
        # masked in the forward pass but not in the backward pass through the torch.where below.
        ellipse_schur = ellipse_shape[..., 0] - (ellipse_shape[..., 1] / ellipse_shape[..., 2].sqrt()).square()
        bad_shape = (
            ~ellipse_shape.isfinite().all(dim=-1)
            | ~ellipse_det.isfinite()
            | (ellipse_shape[..., 0] <= 0)
            | (ellipse_shape[..., 2] <= 0)
            | (ellipse_det <= 0)
            | ~(ellipse_schur > 0)
        )
        circular_shape = ellipse_shape.new_tensor([1.0, 0.0, 1.0])
        safe_ellipse_shape = torch.where(bad_shape[..., None], circular_shape, ellipse_shape)
        ellipses = torch.cat([laf.view(-1, 2, 3)[..., 2].unsqueeze(1), safe_ellipse_shape], dim=2).view(B, N, 5)
        laf_out = _compose_with_frame(ellipse_to_laf(ellipses), frame)
        bad = bad_shape.view(B, N) | ~laf_is_valid(laf_out)
        return _finalize_laf(laf_out, laf, self.preserve_orientation, bad)


class LAFAffNetShapeEstimator(nn.Module):
    """Adapt the affine shape of LAFs to the shape the learned AffNet CNN predicts on each LAF's patch.

    ``lafs = LAFAffNetShapeEstimator()(laf, img)`` builds AffNet with its pretrained weights, in eval mode, and takes
    ``(B, N, 2, 3)`` LAFs in pixels and their grayscale images ``(B, 1, H, W)``, predicts each LAF's shape on its
    32 x 32 patch, sampled through its upright frame, and returns the ``(B, N, 2, 3)`` LAFs with that shape and the
    input's centres and scales. :class:`GFTTAffNetHardNet`, :class:`HesAffNetHardNet`, :class:`DoGAffNetHardNet` and
    :class:`KeyNetAffNetHardNet` run it with ``preserve_orientation=False``, before their orientation step. For the
    second-moment estimate, which needs no weights, use :class:`LAFAffineShapeEstimator` instead. This is based on the
    original code from the paper "Repeatability Is Not Enough: Learning Discriminative Affine Regions via
    Discriminability". See :cite:`AffNet2018` for more details.

    Convention:
        As :class:`~kornia.feature.LAFAffineShapeEstimator`, with AffNet's prediction on a 32 x 32 patch in place of
        the second moment estimate; see its Convention block for the frame the patch is sampled through and for what
        the output keeps of the input LAF.

    Args:
        pretrained: Download and set pretrained weights to the model.
        preserve_orientation: if True, the original orientation is preserved.

    See Also:
        - :class:`LAFAffineShapeEstimator`: the same adaptation, with the second-moment matrix and no weights.
        - :class:`OriNet`: the orientation network of the same paper.
        - :class:`KeyNetAffNetHardNet`: a local feature that runs it on KeyNet detections, before OriNet and HardNet.

    """

    def __init__(self, pretrained: bool = True, preserve_orientation: bool = True) -> None:
        super().__init__()
        self.features = nn.Sequential(
            nn.Conv2d(1, 16, kernel_size=3, padding=1, bias=False),
            nn.BatchNorm2d(16, affine=False),
            nn.ReLU(),
            nn.Conv2d(16, 16, kernel_size=3, stride=1, padding=1, bias=False),
            nn.BatchNorm2d(16, affine=False),
            nn.ReLU(),
            nn.Conv2d(16, 32, kernel_size=3, stride=2, padding=1, bias=False),
            nn.BatchNorm2d(32, affine=False),
            nn.ReLU(),
            nn.Conv2d(32, 32, kernel_size=3, stride=1, padding=1, bias=False),
            nn.BatchNorm2d(32, affine=False),
            nn.ReLU(),
            nn.Conv2d(32, 64, kernel_size=3, stride=2, padding=1, bias=False),
            nn.BatchNorm2d(64, affine=False),
            nn.ReLU(),
            nn.Conv2d(64, 64, kernel_size=3, stride=1, padding=1, bias=False),
            nn.BatchNorm2d(64, affine=False),
            nn.ReLU(),
            nn.Dropout(0.25),
            nn.Conv2d(64, 3, kernel_size=8, stride=1, padding=0, bias=True),
            nn.Tanh(),
            nn.AdaptiveAvgPool2d(1),
        )
        self.patch_size = 32
        # use torch.hub to load pretrained model
        if pretrained:
            pretrained_dict = load_state_dict_from_url(urls["affnet"], map_location=torch.device("cpu"))
            self.load_state_dict(pretrained_dict["state_dict"], strict=False)
            self.eval()
        self.preserve_orientation = preserve_orientation

    @staticmethod
    def _normalize_input(x: torch.Tensor, eps: float = 1e-6) -> torch.Tensor:
        """Subtract each patch's mean and divide by its standard deviation plus ``eps``."""
        # `torch.std_mean` decomposes to `prims.sum`, which has no ONNX lowering; the split form is equivalent.
        mp = torch.mean(x, dim=(-3, -2, -1), keepdim=True)
        sp = torch.std(x, dim=(-3, -2, -1), keepdim=True)
        # WARNING: we need to .detach() input, otherwise the gradients produced by
        # the patches extractor with F.grid_sample are very noisy, making the detector
        # training totally unstable.
        return (x - mp.detach()) / (sp.detach() + eps)

    def forward(self, laf: torch.Tensor, img: torch.Tensor) -> torch.Tensor:
        """Run forward.

        Args:
            laf: ``(B, N, 2, 3)``
            img: ``(B, 1, H, W)``

        Returns:
            LAF_out: ``(B, N, 2, 3)``

        """
        KORNIA_CHECK_LAF(laf)
        KORNIA_CHECK_SHAPE(img, ["B", "1", "H", "W"])
        B, N = laf.shape[:2]
        PS: int = self.patch_size
        safe_input, _ = _sanitize_laf(laf)
        frame = make_upright(safe_input)
        patches: torch.Tensor = extract_patches_from_pyramid(img, frame, PS, True).view(-1, 1, PS, PS)
        xy = self.features(_patch_conv_layout(self._normalize_input(patches))).view(-1, 3)
        a1 = torch.cat([1.0 + xy[:, 0].reshape(-1, 1, 1), 0 * xy[:, 0].reshape(-1, 1, 1)], dim=2)
        a2 = torch.cat([xy[:, 1].reshape(-1, 1, 1), 1.0 + xy[:, 2].reshape(-1, 1, 1)], dim=2)
        new_laf_no_center = torch.cat([a1, a2], dim=1).reshape(B, N, 2, 2)
        new_laf = _compose_with_frame(torch.cat([new_laf_no_center, laf[:, :, :, 2:3]], dim=3), frame)
        bad = ~laf_is_valid(new_laf)
        return _finalize_laf(new_laf, laf, self.preserve_orientation, bad, make_candidate_upright=True)
