import argparse
import os
import random
import time

import numpy as np
from keras.layers import (LSTM, BatchNormalization, Convolution3D, Dense, Dropout, Flatten, Input,
                          MaxPooling3D, TimeDistributed, ZeroPadding3D)
from keras.models import Model, Sequential

from src.data import import_labels
from src.io import get_duration, get_num_frames, video_to_array
from src.processing import activity_localization, get_classification, smoothing

runtime_measures = {
    'load_video': [],
    'extract_features_c3d': [],
    'temporal_localization_network': [],
    'post-processing': [],
    'video_duration': []
}


def run_runtime_tests(input_video, model_features, c3d_mean, model_localization):
    input_size = (112, 112)
    length = 16

    # Setup post-processing variables
    smoothing_k = 5
    activity_threshold = .2

    # Load labels
    with open('dataset/labels.txt', 'r') as f:
        labels = import_labels(f)

    print('')
    print('#'*50)
    print(input_video)
    print('Reading Video...')
    t_s = time.time()
    video_array = video_to_array(input_video, resize=input_size)
    t_e = time.time()
    print('Loading Video: {:.2f}s'.format(t_e-t_s))
    runtime_measures['load_video'].append(t_e-t_s)
    if video_array is None:
        raise Exception('The video could not be read')
    nb_frames = get_num_frames(input_video)
    duration = get_duration(input_video)
    fps = nb_frames / duration
    runtime_measures['video_duration'].append(duration)
    print('Duration: {:.1f}s'.format(duration))
    print('FPS: {:.1f}'.format(fps))
    print('Number of frames: {}'.format(nb_frames))

    nb_clips = nb_frames // length
    video_array = video_array.transpose(1, 0, 2, 3)