#!/usr/bin/env python
import argparse
import os
import sys
import webbrowser
from git import Repo
"""
Open your Gitlab project in a browser

Args:
    -p --merges        Open at pull requests page
    -b --branches      Open at Branches page
    -s --settings      Open at Settings page
    -M --members       Open at Members page
    -r --releases      Open at Releases page
    -t --tags          Open at Tags page
    -w --wiki          Open at Wiki
    -i --issues        Open at Issues page
    -a --activity      Open at Activity page
    -I --integrations  Open at Integrations page
    -c --cicd          Open at CI/CD page
    -d --debug         Enable debug output
    -v --version       Print the installed version of gtlb
"""

version_number = '0.0.1'


def parse_arguments():
    PARSER = argparse.ArgumentParser(description='Gitlab browser opener')
    PARSER.add_argument('--home', help="Open at the home page (Default action)", action='store_true')
    PARSER.add_argument('-m', '--merges', help="Open at Merge 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('-M', '--members', help="Open at members 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('-a', '--activity', help="Open at Activity page", action='store_true')
    PARSER.add_argument('-I', '--integrations', help="Open at Integrations page", action='store_true')
    PARSER.add_argument('-c', '--cicd', help="Open at CI/CD 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 gtlb", action='store_true')
    return PARSER.parse_args()


def main():
    ARGS = parse_arguments()
    home = ARGS.home
    merges = ARGS.merges
    branches = ARGS.branches
    settings = ARGS.settings
    releases = ARGS.releases
    tags = ARGS.tags
    members = ARGS.members
    wiki = ARGS.wiki
    issues = ARGS.issues
    activity = ARGS.activity
    integrations = ARGS.integrations
    cicd = ARGS.cicd
    debug = ARGS.debug
    version = ARGS.version

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

    count = sum(i is True for i in [merges, branches, settings, releases, tags, members, wiki, issues, activity, integrations, cicd])

    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 gtlb 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 merges:
        url = url + '/merge_requests'
    elif branches:
        url = url + '/branches'
    elif settings:
        url = url + '/settings'
    elif releases:
        url = url + '/releases'
    elif tags:
        url = url + '/tags'
    elif members:
        url = url + '/project_members'
    elif wiki:
        url = url + '/wikis/home'
    elif issues:
        url = url + '/issues'
    elif activity:
        url = url + '/activity'
    elif integrations:
        url = url + '/settings/integrations'
    elif cicd:
        url = url + '/settings/ci_cd'

    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()
