# coding: utf8

"""{{ pipeline.title }} - Clinica Command Line Interface.
This file has been generated automatically by the `clinica generate template`
command line tool. See here for more details:
http://clinica.run/doc/InteractingWithClinica/
"""


import clinica.engine as ce


class {{ pipeline.class_name }}CLI(ce.CmdParser):

    def define_name(self):
        """Define the sub-command name to run this pipeline."""
        self._name = '{{ pipeline.command_name }}'

    def define_description(self):
        """Define a description of this pipeline."""
        self._description = ('Brief description:\n'
                             'http://clinica.run/doc/Pipelines/{{ pipeline.class_name }}/')

    def define_options(self):
        """Define the sub-command arguments."""
        from clinica.engine.cmdparser import PIPELINE_CATEGORIES

        # Clinica compulsory arguments (e.g. BIDS, CAPS, group_id...)
        # Most of the time, you will want to read your pipeline inputs into
        # a BIDS and/or CAPS directory. If your pipeline does not require BIDS input,
        # simply remove the two lines involving the BIDS directory.
        clinica_comp = self._args.add_argument_group(PIPELINE_CATEGORIES['CLINICA_COMPULSORY'])
        clinica_comp.add_argument("bids_directory",
                                  help='Path to the BIDS directory.')
        clinica_comp.add_argument("caps_directory",
                                  help='Path to the CAPS directory.')

        # group_id can be used by certain pipelines when some operations are performed at the group level
        # (for example, generation of a template in pipeline t1-volume)
        # clinica_comp.add_argument("group_id",
        #                          help='User-defined identifier for the provided group of subjects.')

        # Add your own pipeline command line arguments here to be used in the
        # method below. Example below:
        optional = self._args.add_argument_group(PIPELINE_CATEGORIES['OPTIONAL'])
        optional.add_argument("-hw", "--hello_word_arg",
                              help='Word to say hello.')

        # Clinica standard arguments (e.g. --n_procs)
        self.add_clinica_standard_arguments()

        # Add advanced arguments
        advanced = self._args.add_argument_group(PIPELINE_CATEGORIES['ADVANCED'])
        advanced.add_argument("-aa", "--advanced_arg",
                              help='Your advanced argument.')

    def run_command(self, args):
        ""
        """
        import os
        from networkx import Graph
        from colorama import Fore
        from .{{ pipeline.module_name }}_pipeline import {{ pipeline.class_name }}
        from clinica.utils.stream import cprint
        from clinica.utils.ux import print_end_pipeline, print_crash_files_and_exit

        # Most of the time, you will want to instantiate your pipeline with a
        # BIDS and/or CAPS directory as inputs. If the BIDS directory is not needed
        # for your pipeline, simply remove:
        # bids_directory=self.absolute_path(args.bids_directory),
        pipeline = T1FreeSurfer(
            bids_directory=self.absolute_path(args.bids_directory),
            caps_directory=self.absolute_path(args.caps_directory),
            tsv_file=self.absolute_path(args.subjects_sessions_tsv),
            base_dir=self.absolute_path(args.working_directory),
            name=self.name
        )
        pipeline.parameters = {
            # Add your own pipeline parameters here to use them inside your
            # pipeline. See the file `{{ pipeline.module_name }}_pipeline.py` to
            # see an example of use.
            'hello_word'        : args.hello_word_arg or 'Hello',
            'advanced_argument' : args.advanced_arg
        }

        if args.n_procs:
            exec_pipeline = pipeline.run(plugin='MultiProc',
                                         plugin_args={'n_procs': args.n_procs})
        else:
            exec_pipeline = pipeline.run()

        if isinstance(exec_pipeline, Graph):
            print_end_pipeline(self.name, os.path.join(pipeline.base_dir, self.name))
        else:
            print_crash_files_and_exit(args.logname, pipeline.base_dir)
