# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: CC-BY-NC-4.0

class Game(object):
    def __init__(self, game_id, object_id, image_info, objects, qas, status):
        self.id = game_id
        self.object_id = object_id
        self.image_info = image_info
        self.image_id = image_info['id']
        self.image_height = image_info['height']
        self.image_width = image_info['width']
        self.objects = objects
        self.qas = qas
        self.status = status
        tgt_index, bboxs, cats = -1, [], []
        for index, obj in enumerate(self.objects):

            bboxs.append(obj['bbox'])
            cats.append(obj['category_id'])
            if obj['id'] == self.object_id:
                assert tgt_index == -1, "Too many target object in game %d." % self.id
                tgt_index = index
        assert tgt_index != -1, "There is no target object in game %d" % self.id
        self.target_index = tgt_index
        self.bboxs = bboxs
        self.categories = cats

    @classmethod
    def from_annotation(cls, annotation):
        return cls(
            game_id=annotation['id'],
            object_id=annotation['object_id'],
            image_info=annotation['image'],
            objects=annotation['objects'],
            qas=annotation['qas'],
            status=annotation['status'])


def bbox2spatial_gw(bbox, image_width, image_height, mode='xywh'):
    if mode == 'xywh':
        x_width = bbox[2]
        y_height = bbox[3]
    elif mode == 'xyxy':
        x_width = bbox[2] - bbox[0]
        y_height = bbox[3] - bbox[1]

    x_left = bbox[0]
    y_upper = bbox[1]
    x_right = x_left + x_width
    y_lower = y_upper + y_height

    x_center = x_left + 0.5 * x_width
    y_center = y_upper + 0.5 * y_height

    # Rescale features fom -1 to 1

    x_left = (1.*x_left / image_width) * 2 - 1
    x_right = (1.*x_right / image_width) * 2 - 1
    x_center = (1.*x_center / image_width) * 2 - 1

    y_lower = (1.*y_lower / image_height) * 2 - 1
    y_upper = (1.*y_upper / image_height) * 2 - 1
    y_center = (1.*y_center / image_height) * 2 - 1

    x_width = (1.*x_width / image_width) * 2
    y_height = (1.*y_height / image_height) * 2

    # Concatenate features
    spatial = [
        x_left, y_upper, x_right, y_lower,
        x_center, y_center, x_width, y_height
    ]
    return spatial
        
