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

from tqdm import tqdm


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

MAX_Q_LEN = 15

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

    def _load_dataset(self):
        # LENS = []
        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 >= 400: break
                game = Game.from_annotation(annotation)
                if game.status != 'success':
                    continue
                
                questions = []
                answers = []
                # qgen_in = [self.sos_id]
                # qgen_tgt = []
                # LENS.append(0)
                for i, qa in enumerate(game.qas):
                    q_tokens = self._tokenizer.encode(qa['question'])
                    if len(q_tokens) > MAX_Q_LEN:
                        q_tokens = q_tokens[:MAX_Q_LEN-1] + [self.eoq_id]
                    a_token = self.answer2token[qa['answer']]
                    # Question mark will not be fed into the model
                    assert q_tokens[-1] == self.eoq_id,\
                        "There is question not ended with question mark in game-%d." % game.id
                    questions.append(q_tokens)
                    answers.append(a_token)
                # Add an end-of-dialog token
                questions.append([self.eod_id])

                item = dict()
                item['game'] = game
                item['image_id'] = game.image_id
                # item['categories'] = game.categories
                item['questions'] = questions
                item['answers'] = answers

                item['image_width'] = game.image_width
                item['image_height'] = game.image_height

                # feats, bboxs, _ = self._image_features_reader[game.image_id]
                # item['image_features'] = feats
                # item['bboxs'] = [
                #     bbox2spatial_gw(box, game.image_width, game.image_height, mode='xyxy') 
                #     for box in bboxs]
                
                entries.append(item)
        return entries

    def tensorize(self):
        for entry in self.entries:
            entry['answers'] = torch.from_numpy(np.array(entry['answers']))
            # entry['bboxs'] = torch.from_numpy(np.array(entry['bboxs']))
            # entry['image_features'] = torch.from_numpy(np.array(entry['image_features']))

    def __getitem__(self, index):
        entry = self.entries[index]
        game = entry['game']
        # bboxs = entry['bboxs']
        # img_feats = entry['image_features']
        img_id = entry['image_id']
        feats, bboxs, _ = self._image_features_reader[img_id]
        img_feats = torch.from_numpy(np.array(feats))
        bboxs = torch.from_numpy(np.array([
            bbox2spatial_gw(box, entry['image_width'], entry['image_height'], mode='xyxy') 
            for box in bboxs]))
        questions = entry['questions']
        answers = entry['answers']

        # begin: Qing adds
        img_feats = img_feats[0:36, :]
        bboxs = bboxs[0:36, :]
        # end: Qing adds



        return (
            game,
            questions,
            answers,
            img_feats,
            bboxs
        )


def collate_fn(batch, wrd_pad_id):
    batch_size = len(batch)
    # batch
    game, questions, answers, img_feats, bboxs = zip(*batch)

    # Pad questions
    qs = list(questions)
    # qs_tf_in = qs[:, :, :-1]
    qs_tf_in = copy.deepcopy(qs)
    max_q_seq_len = max([len(q) for _qs in qs for q in _qs])
    max_num_turns = max([len(_qs) for _qs in qs])
    q_len = []
    for b in range(len(qs)):
        _q_len = []
        for t in range(len(qs[b])):
            _q_len.append(len(qs[b][t]))
            n_pad = max_q_seq_len - len(qs[b][t])
            # Pad each sentence to max_q_seq_len
            qs[b][t].extend(n_pad * [wrd_pad_id])
            qs_tf_in[b][t] = qs_tf_in[b][t][:-1] + n_pad * [wrd_pad_id]
            assert len(qs[b][t]) == max_q_seq_len
        # Pad each batch to max_num_turns
        n_pad_turns = max_num_turns - len(qs[b])
        qs[b] += n_pad_turns * [max_q_seq_len * [wrd_pad_id]]
        qs_tf_in[b] += n_pad_turns * [(max_q_seq_len-1) * [wrd_pad_id]]
        q_len.append(_q_len + n_pad_turns * [0])
        assert len(qs[b]) == max_num_turns
        assert len(q_len[b]) == max_num_turns
    qs = torch.LongTensor(qs)
    qs_tf_in = torch.LongTensor(qs_tf_in)
    q_len = torch.LongTensor(q_len)
    
    # (batch_size, num_max_turns)
    answers = pad_sequence(answers, batch_first=True, padding_value=wrd_pad_id).long()
    img_feats = torch.stack(img_feats).float()
    bboxs = torch.stack(bboxs).float()
    obj_feats = torch.cat([img_feats, bboxs], dim=-1)

    return game, qs, qs_tf_in, answers, q_len, obj_feats

