# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

"""xView2 dataset."""

import glob
import os
from collections.abc import Callable
from typing import ClassVar

import matplotlib.pyplot as plt
import numpy as np
import torch
from matplotlib.figure import Figure
from PIL import Image
from torch import Tensor

from .errors import DatasetNotFoundError
from .geo import NonGeoDataset
from .utils import (
    Path,
    check_integrity,
    draw_semantic_segmentation_masks,
    extract_archive,
)


class XView2(NonGeoDataset):
    """xView2 dataset.

    The `xView2 <https://xview2.org/>`__
    dataset is a dataset for building disaster change detection. This dataset object
    uses the "Challenge training set (~7.8 GB)" and "Challenge test set (~2.6 GB)" data
    from the xView2 website as the train and test splits. Note, the xView2 website
    contains other data under the xView2 umbrella that are _not_ included here. E.g.
    the "Tier3 training data", the "Challenge holdout set", and the "full data".

    Dataset format:

    * images are three-channel pngs
    * masks are single-channel pngs where the pixel values represent the class

    Dataset classes:

    0. background
    1. no damage
    2. minor damage
    3. major damage
    4. destroyed

    If you use this dataset in your research, please cite the following paper:

    * https://arxiv.org/abs/1911.09296

    .. versionadded:: 0.2
    """

    metadata: ClassVar[dict[str, dict[str, str]]] = {
        'train': {
            'filename': 'train_images_labels_targets.tar.gz',
            'md5': 'a20ebbfb7eb3452785b63ad02ffd1e16',
            'directory': 'train',
        },
        'test': {
            'filename': 'test_images_labels_targets.tar.gz',
            'md5': '1b39c47e05d1319c17cc8763cee6fe0c',
            'directory': 'test',
        },
    }
    classes = ('background', 'no-damage', 'minor-damage', 'major-damage', 'destroyed')
    colormap = ('green', 'blue', 'orange', 'red')

    def __init__(
        self,
        root: Path = 'data',
        split: str = 'train',
        transforms: Callable[[dict[str, Tensor]], dict[str, Tensor]] | None = None,
        checksum: bool = False,
    ) -> None:
        """Initialize a new xView2 dataset instance.

        Args:
            root: root directory where dataset can be found
            split: one of "train" or "test"
            transforms: a function/transform that takes input sample and its target as
                entry and returns a transformed version
            checksum: if True, check the MD5 of the downloaded files (may be slow)

        Raises:
            AssertionError: If *split* is invalid.
            DatasetNotFoundError: If dataset is not found.
        """
        assert split in self.metadata
        self.root = root
        self.split = split
        self.transforms = transforms
        self.checksum = checksum

        self._verify()

        self.class2idx = {c: i for i, c in enumerate(self.classes)}
        self.files = self._load_files(root, split)

    def __getitem__(self, index: int) -> dict[str, Tensor]:
        """Return an index within the dataset.

        Args:
            index: index to return

        Returns:
            data and label at that index
        """
        files = self.files[index]
        image1 = self._load_image(files['image1'])
        image2 = self._load_image(files['image2'])
        mask1 = self._load_target(files['mask1'])
        mask2 = self._load_target(files['mask2'])

        image = torch.stack(tensors=[image1, image2], dim=0)
        mask = torch.stack(tensors=[mask1, mask2], dim=0)
        sample = {'image': image, 'mask': mask}

        if self.transforms is not None:
            sample = self.transforms(sample)

        return sample

    def __len__(self) -> int:
        """Return the number of data points in the dataset.

        Returns:
            length of the dataset
        """
        return len(self.files)

    def _load_files(self, root: Path, split: str) -> list[dict[str, str]]:
        """Return the paths of the files in the dataset.

        Args:
            root: root dir of dataset
            split: subset of dataset, one of [train, test]

        Returns:
            list of dicts containing paths for each pair of images and masks
        """
        files = []
        directory = self.metadata[split]['directory']
        image_root = os.path.join(root, directory, 'images')
        mask_root = os.path.join(root, directory, 'targets')
        images = glob.glob(os.path.join(image_root, '*.png'))
        basenames = [os.path.basename(f) for f in images]
        basenames = ['_'.join(f.split('_')[:-2]) for f in basenames]
        for name in sorted(set(basenames)):
            image1 = os.path.join(image_root, f'{name}_pre_disaster.png')
            image2 = os.path.join(image_root, f'{name}_post_disaster.png')
            mask1 = os.path.join(mask_root, f'{name}_pre_disaster_target.png')
            mask2 = os.path.join(mask_root, f'{name}_post_disaster_target.png')
            files.append(dict(image1=image1, image2=image2, mask1=mask1, mask2=mask2))
        return files

    def _load_image(self, path: Path) -> Tensor:
        """Load a single image.

        Args:
            path: path to the image

        Returns:
            the image
        """
        filename = os.path.join(path)
        with Image.open(filename) as img:
            array: np.typing.NDArray[np.int_] = np.array(img.convert('RGB'))
            tensor = torch.from_numpy(array)
            # Convert from HxWxC to CxHxW
            tensor = tensor.permute((2, 0, 1))
            return tensor

    def _load_target(self, path: Path) -> Tensor:
        """Load the target mask for a single image.

        Args:
            path: path to the image

        Returns:
            the target mask
        """
        filename = os.path.join(path)
        with Image.open(filename) as img:
            array: np.typing.NDArray[np.int_] = np.array(img.convert('L'))
            tensor = torch.from_numpy(array)
            tensor = tensor.to(torch.long)
            return tensor

    def _verify(self) -> None:
        """Verify the integrity of the dataset."""
        # Check if the files already exist
        exists = []
        for split_info in self.metadata.values():
            for directory in ['images', 'targets']:
                exists.append(
                    os.path.exists(
                        os.path.join(self.root, split_info['directory'], directory)
                    )
                )

        if all(exists):
            return

        # Check if .tar.gz files already exists (if so then extract)
        exists = []
        for split_info in self.metadata.values():
            filepath = os.path.join(self.root, split_info['filename'])
            if os.path.isfile(filepath):
                if self.checksum and not check_integrity(filepath, split_info['md5']):
                    raise RuntimeError('Dataset found, but corrupted.')
                exists.append(True)
                extract_archive(filepath)
            else:
                exists.append(False)

        if all(exists):
            return

        raise DatasetNotFoundError(self)

    def plot(
        self,
        sample: dict[str, Tensor],
        show_titles: bool = True,
        suptitle: str | None = None,
        alpha: float = 0.5,
    ) -> Figure:
        """Plot a sample from the dataset.

        Args:
            sample: a sample returned by :meth:`__getitem__`
            show_titles: flag indicating whether to show titles above each panel
            suptitle: optional string to use as a suptitle
            alpha: opacity with which to render predictions on top of the imagery

        Returns:
            a matplotlib Figure with the rendered sample
        """
        ncols = 2
        image1 = draw_semantic_segmentation_masks(
            sample['image'][0],
            sample['mask'][0],
            alpha=alpha,
            colors=list(self.colormap),
        )
        image2 = draw_semantic_segmentation_masks(
            sample['image'][1],
            sample['mask'][1],
            alpha=alpha,
            colors=list(self.colormap),
        )
        if 'prediction' in sample:  # NOTE: this assumes predictions are made for post
            ncols += 1
            image3 = draw_semantic_segmentation_masks(
                sample['image'][1],
                sample['prediction'],
                alpha=alpha,
                colors=list(self.colormap),
            )

        fig, axs = plt.subplots(ncols=ncols, figsize=(ncols * 10, 10))
        axs[0].imshow(image1)
        axs[0].axis('off')
        axs[1].imshow(image2)
        axs[1].axis('off')
        if ncols > 2:
            axs[2].imshow(image3)
            axs[2].axis('off')

        if show_titles:
            axs[0].set_title('Pre disaster')
            axs[1].set_title('Post disaster')
            if ncols > 2:
                axs[2].set_title('Predictions')

        if suptitle is not None:
            plt.suptitle(suptitle)

        return fig
