#!/usr/bin/env python
# encoding: utf-8
"""
ped_parser

Command Line Interface for ped_parser

Created by Måns Magnusson on 2014-12-22.
Copyright (c) 2014 __MoonsoInc__. All rights reserved.
"""

from __future__ import print_function
from __future__ import unicode_literals

import sys
import os
import click
import json

from pprint import pprint as pp
from datetime import datetime

from codecs import getwriter, open

import pkg_resources
import ped_parser
from ped_parser import FamilyParser

if sys.version_info < (3,0):
    sys.stdout = getwriter('UTF-8')(sys.stdout)

version = pkg_resources.require("ped_parser")[0].version


def print_version(ctx, param, value):
    """Callback function for printing version and exiting
    Args:
        ctx (object) : Current context
        param (object) : Click parameter(s)
        value (boolean) : Click parameter was supplied or not
    Returns:
        None:
    """
    if not value or ctx.resilient_parsing:
        return
    click.echo('ped_parser version: ' + version)
    ctx.exit()


###         This is the main script         ###

@click.command()
@click.argument('family_file', 
                    nargs=1, 
                    type=click.File('r'),
                    metavar='<family_file> or -'
)
@click.option('-t', '--family_type',
                    type=click.Choice(['ped', 'alt', 'cmms', 'mip']),
                    default='ped',
                    help='If the analysis use one of the known setups, please specify which one. Default is ped'
)
@click.option('-o', '--outfile', 
                    type=click.File('a'),
                    help='Specify the path to a file where results should be stored.'
)
@click.option('--to_json', 
                    is_flag=True,
                    help='Print the ped file in json format.'
)
@click.option('--to_madeline', 
                    is_flag=True,
                    help='Print the ped file in madeline format.'
)
@click.option('--to_ped', 
                    is_flag=True,
                    help='Print the ped file in ped format with headers.'
)
@click.option('-v', '--verbose', 
                is_flag=True,
                help='Increase output verbosity.'
)
@click.option('--version',
                is_flag=True,
                callback=print_version,
                expose_value=False,
                is_eager=True
)
def ped_parser(family_file, family_type, outfile, to_json, to_madeline, 
                to_ped, verbose):
    """Tool for parsing ped files.\n
        Default is to prints the family file to in ped format to output. 
        For more information, please see github.com/moonso/ped_parser.
    """
    my_parser = FamilyParser(family_file, family_type, verbose)
    start = datetime.now()
    if verbose:
        print('Families found in file: {0}'.format(
                    ','.join(list(my_parser.families.keys()))
                    ), 
                    file=sys.stderr
                )
    
    if to_json:
        json_output = my_parser.to_json(outfile)
    elif to_madeline:
        madeline_output = my_parser.to_madeline(outfile)
    elif to_ped:
        madeline_output = my_parser.to_ped(outfile)
    else:
        # If no specific output is choosen, write a summary about the families to screen
        for family in my_parser.families:
            if verbose:
                print('Fam: %s' % family, file=sys.stderr)
                if family_type in ['cmms', 'mip']:
                    print('Expected Inheritance Models: {0}'.format(
                                my_parser.families[family].models_of_inheritance
                                ), 
                                file=sys.stderr
                        )
                print('Individuals: ', file=sys.stderr)
            for individual in my_parser.families[family].individuals:
                if verbose:
                    print(my_parser.families[family].individuals[individual], file=sys.stderr)
            if verbose:
                print('Affected individuals: {0} \n'.format(
                    ','.join(my_parser.families[family].affected_individuals)
                        ), 
                        file=sys.stderr
                    )
    

if __name__ == '__main__':
    ped_parser()
