#!/usr/bin/python

import os
import sys

from argparse import ArgumentParser
from launchpadlib.launchpad import Launchpad
from subprocess import check_call, check_output
from tempfile import NamedTemporaryFile

def main():
    parser = ArgumentParser("A script for proposing merges in Launchpad.")
    parser.add_argument("--target", "-t",
                        help="The target to merge into.")
    parser.add_argument("--withhold","-w",
                        action='store_true',
                        help="Don't ask for a review now, just create the MR")
    args = parser.parse_args()

    lp = Launchpad.login_with(sys.argv[0], 'production')

    # 1. Push the branch to Launchpad and get the URL
    lp_user = check_output(['bzr','launchpad-login']).strip()
    
    parent_branch = check_output('bzr info | grep "parent branch"', shell=True)

    target = args.target
    if not args.target:
        target = parent_branch.strip().split('bazaar.launchpad.net/')[-1]

    lp_project = target.split('/')[1]

    if '+branch' in target:
        target = target.split('/')[1]

    lp_nick = check_output(['bzr','nick']).strip()

    branch_url = 'lp:~' + '/'.join([lp_user, lp_project, lp_nick])

    try:
        check_call(['bzr','push',branch_url])
    except:
        print("Problem pushing branch.")

    # 2. Use the URL to get the branch object
    merge_branch = lp.branches.getByUrl(url=branch_url)

    # 3. Get the target branch object
    target_branch = lp.branches.getByUrl(url='lp:' + target)

    # 4. Prompt for a description

    # TODO: Get the description from 'your favourite editor'
    editor = os.environ.get('EDITOR','nano')
    tempfile = NamedTemporaryFile(delete=False)
    try:
        check_call([editor,tempfile.name])
    except OSError:
        print("Failed to open text editor")
    
    description = tempfile.read()
    tempfile.close()

    # 5. Create the merge proposal
    merge_request = merge_branch.createMergeProposal(target_branch=target_branch, initial_comment=description, needs_review=not(args.withhold))
    print(merge_request.web_link)

if __name__ == "__main__":
    sys.exit(main())
