#!/usr/bin/env python

#
# Copyright 2015 sodastsai
#
# 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.
#

from __future__ import unicode_literals, division, absolute_import, print_function
import os
import requests
from depot_pm import __version__ as depot_pm_version
from taskr import task, console
import yaml
from .configuration import Configuration
from .check import test_names, check as check_core


@task
@task.set_argument('package_file', help='path of package file', nargs='?')
@task.set_argument('--verbose', '-v', dest='verbose', action='store_true')
@task.set_argument('--dry-run', '-d', dest='dry_run', action='store_true')
def install(package_file=None, verbose=False, dry_run=False):
    version(warning_only=True)

    try:
        configuration = Configuration.auto_discover(package_file)
    except yaml.YAMLError as e:
        if verbose:
            console.error('Cannot parse package file: {}'.format(e))
        task.exit(2)
        return  # suppress pycharm warning

    if configuration:
        if verbose:
            console.info('resolved package file: {}'.format(configuration.config_file_path))
    else:
        if verbose:
            console.error('Cannot find package file.')
        task.exit(1)
        return  # suppress pycharm warning

    # Go
    if dry_run and verbose:
        console.info('dry run: depot-pm won\'t perfom following commands.')

    for command in configuration.commands:
        if verbose:
            console.info('Execute command: {}'.format(command))
        if not dry_run:
            os.system(command)
        else:
            console.show(command)

    if verbose:
        console.success('done')


@task
@task.set_argument('--warning-only', dest='warning_only', action='store_true')
def version(warning_only=False):
    version_cmp = lambda s: tuple(map(int, s.split('.')))
    newest_version = max(list(requests.get('https://pypi.python.org/pypi/depot-pm/json').json()['releases'].keys()),
                         key=version_cmp)
    if max(newest_version, depot_pm_version, key=version_cmp) != depot_pm_version:
        console.info('The newest version of depot-pm is {}'.format(newest_version))
        console.info('Update your depot-pm via `pip install -U depot-pm`.')

    if not warning_only:
        console.show('depot-pm {}'.format(depot_pm_version))


@task
@task.set_argument('script_path', nargs='?')
def init_script(script_path=None):
    script = """#!/bin/sh

# Install depot-pm if necessary
which depot-pm 1>/dev/null 2>&1 || {
    # Find pip
    if [ -z "${PIP}" ]; then
        which pip3 1>/dev/null 2>&1 && PIP=pip3 || PIP=pip
    fi
    # Go
    ${PIP} install depot-pm
}
# Run install
depot-pm install
"""

    script_path = script_path or os.path.abspath(os.path.join(os.getcwd(), 'depot-pm-init'))
    with open(script_path, 'w') as f:
        f.write(script)
    os.system('chmod +x {}'.format(script_path))


@task
@task.set_argument('test_name', choices=test_names)
@task.set_argument('args', nargs='*')
@task.set_argument('-v', '--verbose', dest='verbose', action='store_true')
def check(test_name, args=(), verbose=False):
    try:
        check_core(test_name, *args)
    except ValueError as e:
        if verbose:
            console.error(str(e))
        task.exit(1)
    else:
        task.exit(0)


if __name__ == '__main__':
    task.should_raise_exceptions = True
    task.dispatch()
