#!/usr/bin/env python3

"""
This script is called by the wt watchdog. It searches for experiments with the status "run complete" and performs
the postprocessing protocol defined in the configure.yaml file.
"""

from wrftamer.main import project, list_projects
import datetime as dt
import click


@click.command()
@click.option(
    "--projects",
    help="Projects for witch ppp is performed. Run for all by default (str,list)",
)
@click.option(
    "--experiments",
    help="Experiments for witch ppp is performed. Run for all by default (str,list)",
)
@click.option("--silent", help="Run in silent mode")
@click.option("--verbose", help="Run in verbose mode")
@click.option("--force", help="Force ppp (overwrite files)")
def run_ppp_for_completed_exp(
    projects=None, experiments=None, silent=False, verbose=False, force=False
):
    """ ""
    Args:
        projects: The name of the project for which ppp is performed.
        experiments: The name of the experiment for which ppp is performed.
        silent: If true, no output is printed at all.
        verbose: If true: Everyting is printed.
        force: overwrite files (run even if status is not 'run complete')

    The default behaviour (silent=False, verbose=False) is most likely the desired behaviour.

    """

    if isinstance(force, bool) is False:
        force = force == "True"
    if isinstance(silent, bool) is False:
        silent = silent == "True"
    if isinstance(verbose, bool) is False:
        verbose = verbose == "True"

    if projects is None:
        list_of_proj = list_projects(False)
        list_of_proj.append(None)
    else:
        if isinstance(projects, list):
            list_of_proj = projects
        elif isinstance(projects, str):
            list_of_proj = [projects]
        else:
            list_of_proj = list_projects(False)
            list_of_proj.append(None)

    for proj_name in list_of_proj:

        if not silent:
            print("----------------")
            if proj_name is not None:
                print("Project: " + proj_name)
            else:
                print("Not associated with any project:")

        proj = project(proj_name)

        if experiments is None:
            list_of_exps = proj.list_exp(False)
        else:
            if isinstance(experiments, list):
                list_of_exps = experiments
            elif isinstance(experiments, str):
                list_of_exps = [experiments]
            else:
                list_of_exps = proj.list_exp(False)

        for exp_name in list_of_exps:

            status = proj.exp_get_status(exp_name)

            if status == "run complete" or force:
                if not silent:
                    print(
                        "    "
                        + str(dt.datetime.now())
                        + ": Performing ppp for experiment "
                        + exp_name
                    )
                proj.exp_run_postprocessing_protocol(exp_name, verbose)
            else:
                if not silent and verbose:
                    print(
                        "    "
                        + str(dt.datetime.now())
                        + ": Nothing to do for experiment"
                        + exp_name
                    )


if __name__ == "__main__":
    run_ppp_for_completed_exp()
