# -*- coding: utf-8 -*-
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
import contextlib
import io
import logging
import os
from fvcore.common.file_io import PathManager
from fvcore.common.timer import Timer

from detectron2.data import DatasetCatalog, MetadataCatalog
from detectron2.structures import BoxMode


logger = logging.getLogger(__name__)

"""
load json for mode detectron2
"""

def load_coco_with_attributes_json(json_file, 
                                   image_root, 
                                   dataset_name=None, 
                                   extra_annotation_keys=None):
    """
    Extend load_coco_json() with additional support for attributes
    """
    from pycocotools.coco import COCO

    timer = Timer()
    json_file = PathManager.get_local_path(json_file)
    with contextlib.redirect_stdout(io.StringIO()):
        coco_api = COCO(json_file)
    if timer.seconds() > 1:
        logger.info("Loading {} takes {:.2f} seconds.".format(json_file, timer.seconds()))

    id_map = None
    if dataset_name is not None:
        meta = MetadataCatalog.get(dataset_name)
        cat_ids = sorted(coco_api.getCatIds())
        cats = coco_api.loadCats(cat_ids)
        thing_classes = [c["name"] for c in sorted(cats, key=lambda x: x["id"])]
        meta.thing_classes = thing_classes
        if not (min(cat_ids) == 1 and max(cat_ids) == len(cat_ids)):
            if "coco" not in dataset_name:
                logger.warning(
                    """
Category ids in annotations are not in [1, #categories]! We'll apply a mapping for you.
"""
                )
        id_map = {v: i for i, v in enumerate(cat_ids)}
        meta.thing_dataset_id_to_contiguous_id = id_map

    img_ids = sorted(coco_api.imgs.keys())
    imgs = coco_api.loadImgs(img_ids)
    anns = [coco_api.imgToAnns[img_id] for img_id in img_ids]

    if "minival" not in json_file:
        ann_ids = [ann["id"] for anns_per_image in anns for ann in anns_per_image]
        assert len(set(ann_ids)) == len(ann_ids), "Annotation ids in '{}' are not unique!".format(
            json_file
        )

    imgs_anns = list(zip(imgs, anns))

    logger.info("Loaded {} images in COCO format from {}".format(len(imgs_anns), json_file))

    dataset_dicts = []

    ann_keys = ["iscrowd", "bbox", "keypoints", "category_id"] + (extra_annotation_keys or [])

    num_instances_without_valid_segmentation = 0

    for (img_dict, anno_dict_list) in imgs_anns:
        record = {}
        record["file_name"] = os.path.join(image_root, img_dict["file_name"])
        record["height"] = img_dict["height"]
        record["width"] = img_dict["width"]
        image_id = record["image_id"] = img_dict["id"]

        objs = []
        for anno in anno_dict_list:
            assert anno["image_id"] == image_id

            assert anno.get("ignore", 0) == 0, '"ignore" in COCO json file is not supported.'

            obj = {key: anno[key] for key in ann_keys if key in anno}

            segm = anno.get("segmentation", None)
            if segm:  
                if not isinstance(segm, dict):
                    segm = [poly for poly in segm if len(poly) % 2 == 0 and len(poly) >= 6]
                    if len(segm) == 0:
                        num_instances_without_valid_segmentation += 1
                        continue
                obj["segmentation"] = segm

            keypts = anno.get("keypoints", None)
            if keypts:
                for idx, v in enumerate(keypts):
                    if idx % 3 != 2:
                        keypts[idx] = v + 0.5
                obj["keypoints"] = keypts

            attrs = anno.get("attribute_ids", None)
            if attrs:  # list[int]
                obj["attribute_ids"] = attrs  

            attr = anno.get("attribute", None)
            if attr:
                # NOTE import from bua
                # obj["attributes"] = attr  # 正常读入
                # obj["attribute_ids"] = attr  # 正常读入
                # print(attr)
                max_attributes_per_ins = 16
                attributes = [-1 for _ in range(max_attributes_per_ins)]
                for idx, a in enumerate(attr):
                    attributes[idx] = a - 1  # bua train、val的json中attr类别是1-400
                obj["attribute_ids"] = attributes        

            obj["bbox_mode"] = BoxMode.XYWH_ABS
            if id_map:
                obj["category_id"] = id_map[obj["category_id"]]
            objs.append(obj)
        record["annotations"] = objs
        dataset_dicts.append(record)

    if num_instances_without_valid_segmentation > 0:
        logger.warning(
            "Filtered out {} instances without valid segmentation. "
            "There might be issues in your dataset generation process.".format(
                num_instances_without_valid_segmentation
            )
        )
    return dataset_dicts

def register_coco_instances_with_attributes(name, metadata, json_file, image_root):
    DatasetCatalog.register(name, lambda: load_coco_with_attributes_json(json_file, 
                                                                         image_root, 
                                                                         name))
    MetadataCatalog.get(name).set(
        json_file=json_file, image_root=image_root, evaluator_type="coco", **metadata
    )
# ==== Predefined splits for visual genome images ===========
_PREDEFINED_SPLITS_VG = {
    "visual_genome_train": ("visual_genome/images", 
                            "visual_genome/annotations/visual_genome_train.json"),
    "visual_genome_val": ("visual_genome/images", 
                          "visual_genome/annotations/visual_genome_val.json"),
    "visual_genome_test": ("visual_genome/images", 
                           "visual_genome/annotations/visual_genome_test.json"),
}

def register_all_vg(root):
    for key, (image_root, json_file) in _PREDEFINED_SPLITS_VG.items():
        register_coco_instances_with_attributes(
            key,
            {}, # no meta data
            os.path.join(root, json_file),
            os.path.join(root, image_root),
        )

# Register them all under "./datasets"
_root = os.getenv("DETECTRON2_DATASETS", "datasets")
register_all_vg(_root)