# coding=utf-8
# Copyright 2019 The TensorFlow Datasets Authors.
#
# 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.

"""Celeba-HQ dataset."""
import os

import tensorflow as tf
from tensorflow_datasets.core import api_utils
import tensorflow_datasets.public_api as tfds

_CITATION = """\
@article{DBLP:journals/corr/abs-1710-10196,
  author    = {Tero Karras and
               Timo Aila and
               Samuli Laine and
               Jaakko Lehtinen},
  title     = {Progressive Growing of GANs for Improved Quality, Stability, and Variation},
  journal   = {CoRR},
  volume    = {abs/1710.10196},
  year      = {2017},
  url       = {http://arxiv.org/abs/1710.10196},
  archivePrefix = {arXiv},
  eprint    = {1710.10196},
  timestamp = {Mon, 13 Aug 2018 16:46:42 +0200},
  biburl    = {https://dblp.org/rec/bib/journals/corr/abs-1710-10196},
  bibsource = {dblp computer science bibliography, https://dblp.org}
}
"""

_DESCRIPTION = """\
High-quality version of the CELEBA
dataset, consisting of 30000 images in 1024 x 1024 resolution.

WARNING: This dataset currently requires you to prepare images on your own.
"""


class CelebaHQConfig(tfds.core.BuilderConfig):
  """BuilderConfig for CelebaHQ."""

  @api_utils.disallow_positional_args
  def __init__(self, resolution, **kwargs):
    """BuilderConfig for SQUAD.

    Args:
      resolution: Resolution of the image. Values supported: powers of 2 up to
        1024.
      **kwargs: keyword arguments forwarded to super.
    """
    # Versions history:
    # 2.0.0: S3 with new hashing function (different shuffle).
    # 1.0.0: S3 (new shuffling, sharding and slicing mechanism).
    super(CelebaHQConfig, self).__init__(
        name="%d" % resolution,
        description=("CelebaHQ images in %d x %d resolution" %
                     (resolution, resolution)),
        version=tfds.core.Version("0.1.0",
                                  experiments={tfds.core.Experiment.S3: False}),
        supported_versions=[
            tfds.core.Version("2.0.0"),
            tfds.core.Version("1.0.0"),
        ],
        **kwargs)
    self.resolution = resolution
    self.file_name = "data%dx%d.tar" % (resolution, resolution)


class CelebAHq(tfds.core.GeneratorBasedBuilder):
  """Celeba_HQ Dataset."""

  VERSION = tfds.core.Version("0.1.0")

  BUILDER_CONFIGS = [
      CelebaHQConfig(resolution=1024),
      CelebaHQConfig(resolution=512),
      CelebaHQConfig(resolution=256),
      CelebaHQConfig(resolution=128),
      CelebaHQConfig(resolution=64),
      CelebaHQConfig(resolution=32),
      CelebaHQConfig(resolution=16),
      CelebaHQConfig(resolution=8),
      CelebaHQConfig(resolution=4),
      CelebaHQConfig(resolution=2),
      CelebaHQConfig(resolution=1),
  ]

  def _info(self):
    return tfds.core.DatasetInfo(
        builder=self,
        description=_DESCRIPTION,
        features=tfds.features.FeaturesDict({
            "image":
                tfds.features.Image(
                    shape=(self.builder_config.resolution,
                           self.builder_config.resolution, 3),
                    encoding_format="png"),
            "image/filename":
                tfds.features.Text(),
        },),
        urls=["https://github.com/tkarras/progressive_growing_of_gans"],
        citation=_CITATION,
    )

  def _split_generators(self, dl_manager):
    """Returns SplitGenerators."""
    image_tar_file = os.path.join(dl_manager.manual_dir,
                                  self.builder_config.file_name)
    if not tf.io.gfile.exists(image_tar_file):
      # The current celebahq generation code depends on a concrete version of
      # pillow library and cannot be easily ported into tfds.
      msg = "You must download the dataset files manually and place them in: "
      msg += dl_manager.manual_dir
      msg += " as .tar files. See testing/test_data/fake_examples/celeb_a_hq "
      raise AssertionError(msg)
    return [
        tfds.core.SplitGenerator(
            name=tfds.Split.TRAIN,
            num_shards=50,
            gen_kwargs={"archive": dl_manager.iter_archive(image_tar_file)},
        )
    ]

  def _generate_examples(self, archive):
    for fname, fobj in archive:
      record = {"image": fobj, "image/filename": fname}
      yield fname, record
