#!/usr/bin/env python2.7
# -*- coding: utf-8 -*-
'''
brainy-project
==============

A part of brainy CLI:

 - Project management
 - Application skeleton builder
 - Run (execute) brainy project

@author: Yauhen Yakimovich <yauhen.yakimovich@uzh.ch>,
         Pelkmans Lab  <https://www.pelkmanslab.org>

@license: The MIT License (MIT). Read a copy of LICENSE distributed with
          this code.

Copyright (c) 2014-2015 Pelkmans Lab
'''
import os
import sys
import argparse
# We include <pkg_root>/src, <pkg_root>/lib/python
extend_path = lambda root_path, folder: sys.path.insert(
    0, os.path.join(root_path, folder))
ROOT = os.path.dirname(os.path.dirname(__file__))
extend_path(ROOT, '')
extend_path(ROOT, 'src')

# Import brainy modules.
from brainy.config import load_brainy_config
from brainy.project.base import BrainyProject
from brainy.errors import BrainyProjectError
from brainy.log import setup_logging, LOGGING_OPTIONS
from brainy.workflows import WorkflowLocations


# Now parse and handle command line.
if __name__ == '__main__':
    # Load user config plus frameworks.

    program = os.path.basename(sys.argv[0])
    parser = argparse.ArgumentParser(description='Manage brainy projects')
    parser.add_argument('project_name', nargs='?',
                        help='Brainy project name (alphanumerical).')
    parser.add_argument('-p', '--path', default=os.getcwd(),
                        help='Full path to the folder containing brainy '
                        'project.')
    parser.add_argument('-i', '--imperative', dest='action',
                        choices=['run', 'create', 'clean', 'restart', 'help',
                                 'init'],
                        help='Action')
    parser.add_argument('-s', '--subjective')
    parser.add_argument('--from', dest='workflow_name',
                        # choices=workflow_locations.workflows.keys(),
                        default='canonical',
                        help='Specify name of the workflow')
    # TODO: add argument to list/discover known workflows.
    parser.add_argument('--logging', dest='logging_option',
                        choices=LOGGING_OPTIONS, default='console')

    args = parser.parse_args()

    if args.project_name is None:
        if args.action == 'create':
            sys.stderr.write('%s: Create command requires ' % program +
                             'project_name or path specified. See \n\n  '
                             'brainy help project\n\n')
            exit()
        # Assume is a part of path, e.g. current working dir.
        args.project_name = os.path.basename(args.path)

    if args.action == 'help':
        parser.print_help()
        exit()

    # Preparing main entities.
    setup_logging(args.logging_option)
    brainy_config = load_brainy_config()
    workflow_locations = WorkflowLocations(brainy_config)
    brainy_project = BrainyProject(args.project_name, args.path,
                                   workflow_locations=workflow_locations)

    # Handle actions.
    try:
        if args.action == 'create':
            brainy_project.create(from_workflow=args.workflow_name)
        elif args.action == 'init':
            brainy_project.init()
        elif args.action == 'run':
            if not brainy_project.is_a_valid_project_folder():
                raise BrainyProjectError(
                    'Path is not a valid brainy project: %s' %
                    brainy_project.path +
                    '\n Have you done: `brainy create project?`')

            brainy_project.run(brainy_config)
        elif args.action == 'clean':
            brainy_project.clean(brainy_config)
    except BrainyProjectError as error:
        sys.stderr.write('Error: %s\n' % str(error))
