#!/usr/bin/env python
# coding: utf-8

"""
pipver
author: John Shanahan <shanahan.jrs@gmail.com>

TODO:
  - Fix giant run-on main()
  - git support
  - git tag
  - TESTS
  - add a license
  - update readme
  - push to pypi ?
"""

from pipver.lib import *
from pipver.semanticversion import SemanticVersion

import click
from git import Repo
from colorama import Fore, Style

import sys

# lambda print
f = '%sλ%s ' % (Fore.GREEN, Style.RESET_ALL)

def bye(message='%sExiting...' % f, exit_code=1):
    print(message)
    sys.exit(exit_code)

def publish_to_pypi():
    print('%s Building package for Pypi...' % f)
    os.system('python setup.py sdist bdist_wheel')
    print('%s Uploading to Pypi...' % f)
    os.system('twine upload dist/*')
    bye(exit_code=1)

@click.command()
@click.option('--filepath', default=False, help='Name of file containing the version variable/string.')
@click.option('--version', default=False, help='SemVer string of the new version to use.')
@click.option('--major', default=False, help='Increment the version Major.')
@click.option('--minor', default=False, help='Increment the version Minor.')
@click.option('--patch', default=True, help='Increment the version Patch (This is the default).')
@click.option('--from-git', default=False, help='Check the latest git tag for the version string.')
@click.option('--dont-tag', default=False, help='Skip tagging the commit with the generated version.')
@click.option('--yes', is_flag=True, default=False, help='Always answer Yes when prompted with a Y/n question.')
@click.option('--publish', is_flag=True, default=False, help='Build and publish your package to PyPi')
def main(filepath, version, major, minor, patch, from_git, dont_tag, yes, publish):
    """
    main
    """
    # Publish to Pypi
    if publish:
        return publish_to_pypi()

    # init git
    _git = Repo()

    # Make sure they know git repo isnt clean
    if _git.is_dirty():
        if not yes:
            # if --yes was not specified prompt for an answer
            if input('%sGit repo not clean, continue? %s[yes]>%s ' % (f, Fore.YELLOW, Style.RESET_ALL)).lower() != 'yes':
                bye()

    # Find current version
    print('%sSearching for version string...' % f)
    if filepath:
        old_version_str = search_file_for_valid_semver(filepath)
        if not old_version_str:
            raise ValueError('No valid semver string found in file %s' % filepath)
    else:
        old_version_str, filepath = search_each_existing_file(
            search_for_possible_files()
        )
    
    old_version = SemanticVersion(old_version_str)
    print('%sFound current version: %s%s%s' % (f, Fore.CYAN, old_version, Style.RESET_ALL))

    # Now bump the version...
    if version:
        # If a --version string was specified use that
        if not is_valid_semver(version):
            if not yes:
                # if --yes was not specified prompt for an answer
                print('%sWARNING: %s%s%s is not valid, use anyway?' % (f, Fore.RED, version, Style.RESET_ALL))
                if input('Keep? %s[yes]>%s ' % (Fore.YELLOW, Style.RESET_ALL)).lower() != 'yes' or not yes:
                    bye()
        new_version = version
    else:
        # If a --version was not specified then use one of these
        if major:
            new_version = old_version.increment_major()
        elif minor:
            new_version = old_version.increment_minor()
        elif patch:
            new_version = old_version.increment_patch()
        else:
            raise ValueError('No proper version argument was specified [--version, --major, --minor, --patch]')
    
    # New version string Ok?
    print('%sNew version to be written: %s%s%s' % (f, Fore.CYAN, new_version, Style.RESET_ALL))

    if not yes:
        # Ask to use the new version string if they havent specified --yes
        if input('Keep? %s[yes]>%s ' % (Fore.YELLOW, Style.RESET_ALL)).lower() != 'yes':
            bye()

    modify_file_with_new_version_string(filepath, old_version_str, new_version)

    # add/commit/tag updated file to git
    if dont_tag:
        # Skip the git tagging process
        pass
    else:
        _git.git.add(filepath)
        _git.git.commit('-m "[Pipver] Updated version to %s in %s."' % (new_version, filepath))
        _git.create_tag(new_version)

    # Done
    bye('%sDone!' % f, 0)


if __name__ == '__main__':
    main()
