from __future__ import absolute_import, division, print_function

import os
import sys
import time
import glob
import argparse
import numpy as np
import PIL.Image as pil
import matplotlib as mpl
import matplotlib.cm as cm

import torch
from torchvision import transforms, datasets

import networks
from layers import disp_to_depth


def parse_args():
    parser = argparse.ArgumentParser(
        description='Simple testing funtion for Monodepthv2 models.')

    parser.add_argument('--image_path', type=str,
                        help='path to a test image or folder of images', required=True)
    parser.add_argument('--model_name', type=str,
                        help='name of a pretrained model to use')
    parser.add_argument('--ext', type=str,
                        help='image extension to search for in folder', default="jpg")
    parser.add_argument("--no_cuda",
                        help='if set, disables CUDA',
                        action='store_true')
    parser.add_argument("--no_ddv",
                        help='if set, disables ddv',
                        action='store_true')
    parser.add_argument("--no_self_attention",
                        help='if set, disables self-attn',
                        action='store_true')
    parser.add_argument("--discretization",
                        type=str,
                        help="disparity discretization method",
                        default="UD",
                        choices=["SID", "UD"])

    return parser.parse_args()


def print_size_of_model(model):
    """ Print the size of the model.
    Args:
        model: model whose size needs to be determined
    """
    torch.save(model.state_dict(), "temp.p")
    print('Size of the model(MB):', os.path.getsize("temp.p") / 1e6)
    os.remove('temp.p')


def test_simple(args):
    """Function to predict for a single image or folder of images
    """
    assert args.model_name is not None, \
        "You must specify the --model_name parameter."

    if torch.cuda.is_available() and not args.no_cuda:
        device = torch.device("cuda")
    else:
        device = torch.device("cpu")

    print("-> Loading model from ", args.model_name)
    encoder_path = os.path.join(args.model_name, "encoder.pth")
    depth_decoder_path = os.path.join(args.model_name, "depth.pth")

    # LOADING PRETRAINED MODEL
    if args.no_ddv:
        encoder = networks.get_resnet101_asp_oc_dsn(
            2048, args.no_self_attention, False)
        depth_decoder = networks.DepthDecoder(
            encoder.num_ch_enc)
    else:
        encoder = networks.get_resnet101_asp_oc_dsn(
            128, args.no_self_attention, False)
        depth_decoder = networks.MSDepthDecoder(
            encoder.num_ch_enc, discretization=args.discretization)

    print("   Loading pretrained encoder")
    loaded_dict_enc = torch.load(encoder_path, map_location=device)

    # extract the height and width of image that this model was trained with
    feed_height = loaded_dict_enc['height']
    feed_width = loaded_dict_enc['width']
    filtered_dict_enc = {k: v for k, v in loaded_dict_enc.items() if k in encoder.state_dict()}
    encoder.load_state_dict(filtered_dict_enc)
    encoder.to(device)
    encoder.eval()

    print("   Loading pretrained decoder")
    loaded_dict = torch.load(depth_decoder_path, map_location=device)
    depth_decoder.load_state_dict(loaded_dict)

    print_size_of_model(encoder)
    print_size_of_model(depth_decoder)

    depth_decoder.to(device)
    depth_decoder.eval()

    # FINDING INPUT IMAGES
    if os.path.isfile(args.image_path):
        # Only testing on a single image
        paths = [args.image_path]
        output_directory = os.path.dirname(args.image_path)
    elif os.path.isdir(args.image_path):
        # Searching folder for images
        paths = glob.glob(os.path.join(args.image_path, '*.{}'.format(args.ext)))
        output_directory = args.image_path
    else:
        raise Exception("Can not find args.image_path: {}".format(args.image_path))

    print("-> Predicting on {:d} test images".format(len(paths)))

    # PREDICTING ON EACH IMAGE IN TURN
    timings = list()
    with torch.no_grad():
        for idx, image_path in enumerate(paths):

            if image_path.endswith("_disp.jpg"):
                # don't try to predict disparity for a disparity image!
                continue

            # Load image and preprocess
            input_image = pil.open(image_path).convert('RGB')
            original_width, original_height = input_image.size
            input_image = input_image.resize((feed_width, feed_height), pil.LANCZOS)
            input_image = transforms.ToTensor()(input_image).unsqueeze(0)

            # PREDICTION
            input_image = input_image.to(device)

            st = time.time()
            features = encoder(input_image)
            if args.no_ddv:
                outputs = depth_decoder(features)
            else:
                all_features = {}
                all_features['conv3'] = features[0]
                all_features['layer1'] = features[1]
                all_features['output'] = features[-1]
                outputs = depth_decoder(all_features)
            et = time.time()
            print('Elapsed time = {:0.4f} ms'.format((et - st) * 1000))
            timings.append((et - st) * 1000)

            disp = outputs[("disp", 0)]
            disp_resized = torch.nn.functional.interpolate(
                disp, (original_height, original_width), mode="bilinear", align_corners=False)

            # Saving numpy file
            output_name = os.path.splitext(os.path.basename(image_path))[0]
            name_dest_npy = os.path.join(output_directory, "{}_disp.npy".format(output_name))
            scaled_disp, _ = disp_to_depth(disp, 0.1, 100)
            np.save(name_dest_npy, scaled_disp.cpu().numpy())

            # Saving colormapped depth image
            disp_resized_np = disp_resized.squeeze().cpu().numpy()
            vmax = np.percentile(disp_resized_np, 95)
            normalizer = mpl.colors.Normalize(vmin=disp_resized_np.min(), vmax=vmax)
            mapper = cm.ScalarMappable(norm=normalizer, cmap='magma')
            colormapped_im = (mapper.to_rgba(disp_resized_np)[:, :, :3] * 255).astype(np.uint8)
            im = pil.fromarray(colormapped_im)

            name_dest_im = os.path.join(output_directory, "{}_disp.jpeg".format(output_name))
            im.save(name_dest_im)

            print("   Processed {:d} of {:d} images - saved prediction to {}".format(
                idx + 1, len(paths), name_dest_im))

    print('Mean time elapsed: {:0.4f}'.format(np.mean(timings[11:])))
    print('Std time elapsed: {:0.4f}'.format(np.std(timings[11:])))
    print('-> Done!')


if __name__ == '__main__':
    args = parse_args()
    test_simple(args)
