from __future__ import print_function
from pkg_resources import resource_filename
import os
import argparse
import logging

from resmico import evaluate
from resmico.commands import arguments


# functions
def get_desc():
    desc = 'Evaluate model'
    return desc


def parse_args(test_args=None, subparsers=None):
    desc = get_desc()
    epi = """DESCRIPTION:
    Evaluate a trained model generated by `resmico train`.

    Contig features used for evaluation must have been generated with resmico-sm.
    """
    if subparsers:
        parser = subparsers.add_parser('evaluate', description=desc, epilog=epi,
                                       formatter_class=argparse.RawTextHelpFormatter)
    else:
        parser = argparse.ArgumentParser(description=desc, epilog=epi,
                                         formatter_class=argparse.RawTextHelpFormatter)

    # default trained model
    pkg_model = resource_filename('resmico', 'model/resmico.h5')

    parser.add_argument('--model', default=pkg_model, type=str,
                        help='Location of the saved deep learning model')
    parser.add_argument('--batch-size', default=100, type=int,
                        help='Batch size (default: %(default)s)')
    parser.add_argument('--sdepth', default=None, type=str,
                        help='Use only data with this sequencing depth (default: %(default)s)')
    parser.add_argument('--rich', default=None, type=str,
                        help='Use only data with this comunity richness (default: %(default)s)')
    parser.add_argument('--filter10', action='store_true', default=False,
                        help='Use True if want to train on 1-9 reps')
    parser.add_argument('--rep10', action='store_true', default=False,
                        help='use only rep 10 (validation set)')
    parser.add_argument('--method-pred', default='chunks', type=str,
                        help='How to predict: random, fulllength, chunks')
    parser.add_argument('--min-len', default=1000, type=int,
                        help='Definition of -long- contig. If want predict for all use(default: %(default)s)')
    parser.add_argument('--mem-lim', default=500000, type=int,
                        help='Max contig that fits in one batch (default: %(default)s)')
    parser.add_argument('--window', default=5000, type=int,
                        help='Window size for chunks method, size of piece for random method (default: %(default)s)')
    parser.add_argument('--v1', action='store_true', default=False,
                        help='use deepmased')
    parser.add_argument('--longdir', action='store_true', default=False,
                        help='Six variable parameters in simulation (default: %(default)s)')
    parser.add_argument('--embeddings', action='store_true', default=False,
                        help='Produce embeddings (default: %(default)s)')
    parser.add_argument('--emb-ind', default=0, type=int,
                        help='Layer index to produce embedding (default: %(default)s)')
    parser.add_argument('--verify-insert-size', action='store_true', default=False,
                        help='Check if the insert size distribution is similar to the n9k-train dataset')
    arguments.add_common_args(parser)

    # running test args
    if test_args:
        args = parser.parse_args(test_args)
        return args

    return parser


def main(args=None):
    if args is None:
        args = parse_args()

    logging.basicConfig(format='%(asctime)s - %(message)s', level=logging._nameToLevel[args.log_level.upper()])

    print()
    print(args)
    print()
    evaluate.main(args)


if __name__ == '__main__':
    pass
