#!/usr/bin/env python3

"""
    Check if user enter all necessary arguments correctly.
    Otherwise, a message is sent to the user.
"""

import sys
import os


def check_int_type(rep):
    """
    Check if the answer can be cast into an integer and return it
    If not, an errro message is sent

    Args:
        rep: answer given by the user in str

    Returns:
        The answer casted into an integer
    """
    try:
        rep = int(rep)
    except ValueError:
        print("Be carefull, a number is expected!")
        sys.exit(2)
    if rep < 0:
        print("The number must be positive!")
        sys.exit(2)
    return rep


def check_bool_type(rep):
    """
    Check if the answer is of the boolean type.

    Args:
        rep: answer given by the user (str)

    Returns:
        The answer casted into a boolean
    """
    rep = rep.lower()
    if rep in ("true", "t", "yes", "y"):
        return True
    elif rep in ("false", "f", "no", "n"):
        return False
    else:
        print("Enter a boolean type (True, T, False, F) !")
        sys.exit(2)


def check_existance(rep):
    """
    Check if the foldrec file, given as an argument of the program, exists.
    If it is not the case, print an error and exit the program

    Args:
        rep: answer given by the user (str)

    Returns:
        The path to the foldrec file given as argument
    """
    if not os.path.isfile(rep):
        print("Specified foldrec file doesn't exists!\n")
        sys.exit(2)

    else:
        return rep


def check_arguments(argv):
    """
    Assign options into variable and check if options are correct.

    Args:
        argv: arguments given by the user
    """
    global CUTOFF, TPL_GAP, QUERY_GAP, NB_OUTPUT, QUERY, BENCH_MODE
    global BASE_DIR
    CUTOFF = check_int_type(argv["--cutoff"])
    if CUTOFF > 15:
        print("Be carefull, the cutoff must be inferior or equal to 15A !")
        sys.exit(2)
    TPL_GAP = check_int_type(argv["--tplGap"])
    QUERY_GAP = check_int_type(argv["--queryGap"])
    NB_OUTPUT = check_int_type(argv["--nbOutput"])
    QUERY = check_existance(argv["--input"])
    BENCH_MODE = check_bool_type(argv["--bench"])
    BASE_DIR = argv["--baseDir"]

    # Check if folder in test_dataset are in the HOMSTRAD Database
    # else copy them in HOMSTRAD
    if not os.path.isdir(BASE_DIR + "Data/HOMSTRAD/Cohesin/"):
        os.system("cp -r " + BASE_DIR + "Data/test_dataset/* " +
                  BASE_DIR + "Data/HOMSTRAD/")

    # Check for presence of the log file associated with the cleaning of the
    # database
    # If NOT present, user must clean his database
    if not os.path.isfile(BASE_DIR + "clean.log"):
        error_str = ("\n\nERROR: Database needs to be cleaned before " +
                     "computing Modeller score.\n" +
                     "       To clean database, run:\n" +
                     "             ./clean.py -d /path/to/data/\n\n")
        sys.exit(error_str)

    # Check for presence of the txt file associated with the benchmarking of
    # all the computed scores
    # If NOT present, user needs to bench, before being able to use the global
    # score
    if not BENCH_MODE and not os.path.isfile("results/bench/weights.txt"):
        error_str = ("\n\nERROR: Need to process benchmarking before being " +
                     "able to use global score(should be the best score).\n" +
                     "       To benchmark scores, run:\n" +
                     "             ./bench.py -f /path/to/foldrec/files/ -d "+
                     "/path/to/data/\n\n")
        sys.exit(error_str)
