#!/usr/bin/env python

from nifti_snapshot import nifti_snapshot
from pathlib import Path
import matplotlib.pyplot as plt
import argparse

if __name__ == '__main__':
    argparser = argparse.ArgumentParser(
        formatter_class=argparse.RawDescriptionHelpFormatter,
        description='''\
        nifti_snapshot.
        Takes snapshot of Nifti images.
        ''', epilog="Kevin Cho Thursday, August 22, 2019")

    argparser.add_argument("--input", "-i",
                           type=str,
                           nargs='+',
                           help='Nifti image. One or more than one location of'
                                ' images')

    argparser.add_argument("--volumes", "-v",
                           type=int,
                           nargs='+',
                           help='Volume to snapshot from, if the input is 4d '
                                'image')

    argparser.add_argument("--tbss", "-tbss",
                           action='store_true',
                           help='For tbss randomise outputs')

    argparser.add_argument("--threshold", "-thr",
                           type=float,
                           nargs='+',
                           help='One or more threshold for the input image.')

    argparser.add_argument("--cmap", "-c",
                           type=str,
                           nargs='+',
                           help='One or more matplotlib cmap for the '
                                'input image.')

    argparser.add_argument("--alpha", "-a",
                           type=float,
                           nargs='+',
                           help='One or more alpha value for the '
                                'input image.')

    argparser.add_argument("--background", "-b",
                           type=str,
                           nargs='+',
                           help='One or more backgroudn images')

    argparser.add_argument("--title", "-t",
                           type=str,
                           help='Title on top of the image')

    argparser.add_argument("--intensity_percentile", "-ip",
                           type=int,
                           nargs='+',
                           default=(5, 95),
                           help='min and max percentile to be used '
                                'when visualizing the data')

    argparser.add_argument("--cbar_title", "-ct",
                           type=str,
                           nargs='+',
                           help='Colorbar title')

    argparser.add_argument("--overlap", "-ol",
                           action='store_true',
                           help='produce overlap')

    argparser.add_argument("--overlap_cmap", "-olc",
                           type=str,
                           help='matplotlib cmap for the overlap figure')

    argparser.add_argument("--overlap_alpha", "-ola",
                           type=float,
                           default=0.8,
                           help='alpha value for the overlap figure')

    argparser.add_argument("--output_file", "-o",
                           type=str,
                           help='Output png file')

    args = argparser.parse_args()

    # TBSS pipeline
    if args.tbss:
        # If overlap option is on
        if args.overlap and len(args.input) == 2:
            tbssFigure = nifti_snapshot.TbssFigure(
                image_files=args.input,
                output_file=args.output_file,
                cmap_list=args.cmap,
                overlap_cmap=args.overlap_cmap,
                cbar_titles=args.cbar_title,
                alpha_list=[1, 1]+[args.overlap_alpha],
                title=args.title)

            tbssFigure.create_figure_two_maps_and_overlap()

        elif len(args.input) == 1:
            tbssFigure = nifti_snapshot.TbssFigure(
                image_files=args.input,
                output_file=args.output_file,
                cmap_list=args.cmap,
                cbar_titles=args.cbar_title,
                alpha_list=[0.8],
                title=args.title,
                cbar_x=0.35, 
                cbar_width=0.3)

            tbssFigure.create_figure_one_map()
    else:
        fig = nifti_snapshot.SimpleFigure(
        image_files = args.input,
        title = args.title,
        make_transparent_zero = True,
        cbar_width = 0.5,
        cbar_title = args.cbar_title,
        output_file = args.output_file,
        volumes = args.volumes,
        percentile = args.intensity_percentile

        # vmin_list = [-100],
        # vmax_list = [100],
    )
