# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: CC-BY-NC-4.0
import os
import torch
import logging
import jsonlines
import numpy as np
from tqdm import tqdm
import _pickle as cPickle
from functools import partial
from torch.utils.data import Dataset
from torch.nn.utils.rnn import pad_sequence
from src.tools.utils import Game, bbox2spatial_gw
from src.data.dataset import GuessWhatDataset


logger = logging.getLogger(__name__)  # pylint: disable=invalid-name


class GuesserDataset(GuessWhatDataset):
    def __init__(
        self,
        dataroot,
        split,
        image_features_reader,
        tokenizer,
        **kwargs
    ):
        super().__init__(
            dataroot,
            'guesser',
            split,
            image_features_reader,
            tokenizer,
            **kwargs)

    def _load_dataset(self):
        with jsonlines.open(self.data_path) as reader:
            # Build an index which maps image id with a list of qa annotations.
            entries = []
            for cur, annotation in tqdm(enumerate(reader)):
                #if cur >= 1000: break
                game = Game.from_annotation(annotation)
                if game.status != 'success':
                    continue
                dialog = []
                for qa in game.qas:
                    # q_tokens = self._tokenizer.encode(qa['question'])[:-1] + [self.eoq_id]
                    q_tokens = self._tokenizer.encode(qa['question'])
                    assert q_tokens[-1] == self.eoq_id, "Game %d has question which is not ended with `?`." % game.id
                    a_token = self.answer2token[qa['answer']]
                    dialog.extend(q_tokens + [a_token])

                item = dict()
                item['target_index'] = game.target_index
                item['categories'] = game.categories
                item['bboxs'] = [
                    bbox2spatial_gw(box, game.image_width, game.image_height) for box in game.bboxs]
                item['dialog'] = dialog
                entries.append(item)
        return entries

    def tensorize(self):
        for entry in self.entries:
            entry['target_index'] = torch.from_numpy(np.array(entry['target_index']))
            entry['dialog'] = torch.from_numpy(np.array(entry['dialog']))
            entry['categories'] = torch.from_numpy(np.array(entry['categories']))
            entry['bboxs'] = torch.from_numpy(np.array(entry['bboxs']))


    def __getitem__(self, index):
        entry = self.entries[index]
        #image_id = entry['image_id']
        dialog = entry['dialog']
        tgt_index = entry['target_index']
        cats = entry['categories']
        bboxs = entry['bboxs']
        # bboxs = torch.from_numpy(self._image_features_reader[image_id])

        label = torch.LongTensor([tgt_index])

        return (
            dialog,
            cats,
            bboxs,
            label
        )


def collate_fn(batch, wrd_pad_id):
    batch_size = len(batch)
    # batch
    dialog, cats, bboxs, label = zip(*batch)

    bboxs_mask = [torch.ones(len(xs)) for xs in bboxs]
    dialog_len = torch.LongTensor([len(d) for d in dialog])

    # (batch_size, padded_num_obj)
    cats = pad_sequence(cats, batch_first=True).long()
    # (batch_size, padded_num_bboxs, spatial dim)
    bboxs = pad_sequence(bboxs, batch_first=True).float()
    # (batch_size, padded_num_obj)
    bboxs_mask = pad_sequence(bboxs_mask, batch_first=True).bool()
    # (batch_size, padded_seq_length)
    dialog = pad_sequence(dialog, batch_first=True, padding_value=wrd_pad_id).long()
    label = torch.stack(label).view(-1)

    return dialog, dialog_len, cats, bboxs, bboxs_mask, label

