#!/usr/bin/python3 -s
import argparse
import os
import sys
import webbrowser
from git import Repo
"""
Open your Github project in a browser

Args:
    -p --pulls         Open at pull requests page
    -b --branches      Open at Branches page
    -s --settings      Open at Settings page
    -c --collaboration Open at Collaboration page
    -r --releases      Open at Releases page
    -t --tags          Open at Tags page
    -w --wiki          Open at Wiki
    -i --issues        Open at Issues page
    -d --debug         Enable debug output
    -v --version       Print the installed version of gh
"""

version_number = '0.0.4'


def parse_arguments():
    PARSER = argparse.ArgumentParser(description='Github browser opener')
    PARSER.add_argument('--home', help="Open at the home page (Default action)", action='store_true')
    PARSER.add_argument('-p', '--pulls', help="Open at pull requests page", action='store_true')
    PARSER.add_argument('-b', '--branches', help="Open at Branches page", action='store_true')
    PARSER.add_argument('-s', '--settings', help="Open at Settings page", action='store_true')
    PARSER.add_argument('-r', '--releases', help="Open at Releases page", action='store_true')
    PARSER.add_argument('-t', '--tags', help="Open at Tags page", action='store_true')
    PARSER.add_argument('-c', '--collaboration', help="Open at collaboration page", action='store_true')
    PARSER.add_argument('-w', '--wiki', help="Open at Wiki", action='store_true')
    PARSER.add_argument('-i', '--issues', help="Open at Issues page", action='store_true')
    PARSER.add_argument('-d', '--debug', help="Enable debug output", action='store_true')
    PARSER.add_argument('-v', '--version', help="Show the installed version of gh", action='store_true')
    return PARSER.parse_args()


def main():
    ARGS = parse_arguments()
    home = ARGS.home
    pulls = ARGS.pulls
    branches = ARGS.branches
    settings = ARGS.settings
    releases = ARGS.releases
    tags = ARGS.tags
    collaboration = ARGS.collaboration
    wiki = ARGS.wiki
    issues = ARGS.issues
    debug = ARGS.debug
    version = ARGS.version

    if version:
        print('gh version: v{}'.format(version_number))
        sys.exit(0)

    count = sum(i is True for i in [pulls, branches, settings, releases, tags, collaboration, wiki, issues])

    if count > 1:
        print('You can only specify one option')
    elif count == 0:
        home = True

    cwd = os.getcwd()

    try:
        Repo(cwd)
    except Exception:
        print('Couldnt detect Git repo, are you executing gh from within the right directory..?')
        sys.exit(1)

    git_clone_path = Repo(cwd).remotes.origin.url

    url = git_clone_path.replace(':', '/').replace('git@', 'https://').replace('.git', '')

    if home:
        url = url + '/'
    elif pulls:
        url = url + '/pulls'
    elif branches:
        url = url + '/branches'
    elif settings:
        url = url + '/settings'
    elif releases:
        url = url + '/releases'
    elif tags:
        url = url + '/tags'
    elif collaboration:
        url = url + '/settings/collaboration'
    elif wiki:
        url = url + '/wiki'
    elif issues:
        url = url + '/issues'

    if debug:
        print('Current working directory {}'.format(cwd))
        print('SSH clone path detected as {}'.format(git_clone_path))
        print('URL constructed as {}'.format(url))
        print('Attempting to open browser using the python webbrowser package')

    try:
        webbrowser.open_new_tab(url)
    except Exception:
        print('Couldnt open the browser.')


if __name__ == "__main__":
    main()
