#   Foremast - Pipeline Tooling
#
#   Copyright 2016 Gogo, LLC
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.
"""Prepare the Application Configurations."""
import collections
import logging

from ..consts import ENVS
from ..utils import GitLookup

LOG = logging.getLogger(__name__)


def process_git_configs(git_short=''):
    """Retrieve _application.json_ files from GitLab.

    Args:
        git_short (str): Short Git representation of repository, e.g.
            forrest/core.

    Returns:
        collections.defaultdict: Configurations stored for each environment
        found.
    """
    LOG.info('Processing application.json files from GitLab "%s".', git_short)

    file_lookup = GitLookup(git_short=git_short)

    app_configs = collections.defaultdict(dict)
    for env in ENVS:
        app_json = 'runway/application-master-{env}.json'.format(env=env)
        try:
            app_configs[env] = file_lookup.json(filename=app_json)
        except FileNotFoundError:
            LOG.critical('Application configuration not available for %s.', env)
            # TODO: Use default configs anyways?
            continue

    LOG.info('Processing pipeline.json from GitLab.')

    pipeline_json = 'runway/pipeline.json'
    try:
        app_configs['pipeline'] = file_lookup.json(filename=pipeline_json)
        LOG.info('Pipeline configuration found.')
    except FileNotFoundError:
        LOG.info('Pipeline configuration not available, using defaults.')
        app_configs['pipeline'] = {'env': ['stage', 'prod']}

    config_commit = file_lookup.server.getbranch(file_lookup.project_id, 'master')['commit']['id']
    LOG.info('Commit ID used: %s', config_commit)
    app_configs['pipeline']['config_commit'] = config_commit

    LOG.debug('Application configs:\n%s', app_configs)
    return app_configs


def process_runway_configs(runway_dir=''):
    """Read the _application.json_ files.

    Args:
        runway_dir (str): Name of runway directory with app.json files.

    Returns:
        collections.defaultdict: Configurations stored for each environment
        found.
    """
    LOG.info('Processing application.json files from local directory "%s".', runway_dir)

    file_lookup = GitLookup(runway_dir=runway_dir)

    app_configs = collections.defaultdict(dict)
    for env in ENVS:
        file_json = 'application-master-{env}.json'.format(env=env)
        try:
            app_configs[env] = file_lookup.json(filename=file_json)
        except FileNotFoundError:
            LOG.critical('Application configuration not available for %s.', env)
            continue

    LOG.info('Processing pipeline.json from local directory')

    try:
        app_configs['pipeline'] = file_lookup.json(filename='pipeline.json')
    except FileNotFoundError:
        LOG.warning('Unable to process pipeline.json. Using defaults.')
        app_configs['pipeline'] = {'env': ['stage', 'prod']}

    LOG.debug('Application configs:\n%s', app_configs)
    return app_configs

@@ -747,19 +747,20 @@
  import 
-Git
+File
 Lookup%0A%0A
@@ -1202,27 +1202,28 @@
 le_lookup = 
-Git
+File
 Lookup(git_s
@@ -2726,19 +2726,20 @@
 ookup = 
-Git
+File
 Lookup(r

Update to new name FileLookup