import numpy as np
import os
import pandas as pd
import torch
import decord
import timeit
import pickle
import json
import random
import cv2
import ffmpeg

from PIL import Image
from tqdm import tqdm
from torch.utils.data import Dataset, DataLoader
from torch.autograd.variable import Variable
from torchvision import transforms
from decord import VideoReader, cpu, gpu
from datetime import timedelta
from torch.utils.data._utils.collate import default_collate


print('imported!!!')
# os.environ['DECORD_EOF_RETRY_MAX'] = '40960'
decord.bridge.set_bridge('torch')


def multiple_samples_collate(batch):
    """
    Collate function for repeated augmentation. Each instance in the batch has
    more than one sample.
    Args:
        batch (tuple or list): data batch to collate.
    Returns:
        (tuple): collated data batch.
    """
    inputs, labels = zip(*batch)
    inputs = [x for x in inputs if x is not None]
    labels = [x for x in labels if x is not None]

    inputs, labels, = (
        default_collate(inputs),
        default_collate(labels),
    )

    return inputs, labels


# test_remove = ['084k_RL3ApU_000109_000119.mp4', '2xWiEVNUvhE_000064_000074.mp4', '305P2f9_lko_004145_004155.mp4',
#                'B4bn9G6__sY_000086_000096.mp4', 'BvBVQmm2RcM_000082_000092.mp4', 'CxjipYE57Yo_000199_000209.mp4',
#                'IhanWvpHGu8_001243_001253.mp4', 'Lw14NH9kAqE_000759_000769.mp4',
#                ' XFkykETgkoo_002967_002977.mp4', 'jJFqy6yiXzQ_000024_000034.mp4',
#                'kinMMqkswUk_000120_000130.mp4', 'y7cYaYX4gdw_000047_000057.mp4']

class SmthSmthDL(Dataset):
    def __init__(self, data_split, num_frames, resolution):
        self.data = []
        self.labels = []
        self.num_frames = num_frames
        self.data_split = data_split
        self.resolution = resolution
        self.root = '/squash/something-something-videos/'
        videos = os.listdir(self.root)
        if data_split == 'train':
            self.data = open("somesomev2_rgb_train_split.txt").readlines()
        else:
            self.data = open("somesomev2_rgb_validation_split.txt").readlines()
        self.data = [x.strip('\n') for x in self.data]

        # test = open("somesomev2_rgb_test_split.txt").readlines()
        # labels = [x.split(' ')[1] for x in test]
        #
        # print(len(np.unique(labels)), np.unique(labels))
        print(len(self.data))
        # self.data = self.data[:100]
        # exit()
        self.transforms = transforms.Compose([
            transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)),
            transforms.Resize([resolution, resolution]),
            # transforms.CenterCrop(size=(224, 224))
        ])

    def __len__(self):
        return len(self.data)

    def __getitem__(self, index):
        vid, _, label = self.data[index].split(' ')
        vid_path = os.path.join(self.root, vid + '.webm')
        # print(vid_path)

        try:
            probe = ffmpeg.probe(vid_path)
            video_stream = next((stream for stream in probe['streams'] if stream['codec_type'] == 'video'), None)
            width = int(video_stream['width'])
            height = int(video_stream['height'])
            out, _ = (
                ffmpeg
                .input(vid_path, loglevel="quiet")
                .output('pipe:', format='rawvideo', pix_fmt='rgb24')
                .run(capture_stdout=True)
            )
            frames = (
                np
                .frombuffer(out, np.uint8)
                .reshape([-1, height, width, 3])
            )
            # print(frames.shape)
            # exit()
            frames = torch.from_numpy(frames.copy())
            frames = frames.permute(0, 3, 1, 2).float() / 255.
            frame_indexer = np.linspace(0, frames.shape[0]-1, self.num_frames).astype(int)
            frames = frames[frame_indexer]
            frames = self.transforms(frames).permute(1, 0, 2, 3)
        except Exception as e:
            print(e)
            return None, None
        return frames, int(label)


if __name__ == '__main__':
    shuffle = False
    dataloader_gen = SmthSmthDL('test', 2, 224)
    dataloader = DataLoader(dataloader_gen, num_workers=0, batch_size=4, collate_fn=multiple_samples_collate)
    for frames, label in tqdm(dataloader):
        print(frames.shape)