#!/usr/bin/env python

import argparse
import collections
import os
import re
import subprocess
import time
import getpass

import bs4
import requests

EXIT_FAILURE = 1
LOGIN_URL = 'https://aur.archlinux.org/login'
SEARCH_URL_TEMPLATE = 'https://aur.archlinux.org/packages/?O=%d&SB=w&SO=d&PP=250&do_Search=Go'
PACKAGES_URL = 'https://aur.archlinux.org/packages/'
VOTE_URL_TEMPLATE = 'https://aur.archlinux.org/pkgbase/%s/vote'
UNVOTE_URL_TEMPLATE = 'https://aur.archlinux.org/pkgbase/%s/unvote'
PACKAGES_PER_PAGE = 250

argument_parser = argparse.ArgumentParser()
argument_parser.add_argument('username')
argument_parser.add_argument('--ignore', '-i', action='append', default=[],
                             help='Regex for packages that should not be voted. Can be passed multiple times.')
argument_parser.add_argument('--unvote-all', '-u', action='store_true',
                             help='Unvote all voted-for packages, all other arguments are ignored.')
argument_parser.add_argument('--no-unvote', '-n', action="store_true", help="Don't unvote uninstalled packages.")
argument_parser.add_argument('--delay', '-d', type=float, default=0, help='Delay between voting actions (seconds).')

Package = collections.namedtuple(
    'Package', ('name', 'version', 'votes', 'popularity', 'voted', 'notify', 'description', 'maintainer', 'updated'))


def login(session, username, password):
    response = session.post(LOGIN_URL, {'user': username, 'passwd': password, 'next': '/'}, headers={'referer': 'https://aur.archlinux.org/login'})
    soup = bs4.BeautifulSoup(response.text, 'html5lib')
    return bool(soup.select_one('#archdev-navbar').find('form', action=lambda h: h and h.rstrip('/').endswith('/logout')))


def get_foreign_packages():
    return subprocess.check_output(('pacman', '-Qmq'), universal_newlines=True).splitlines()


def get_voted_packages(session):
    offset = 0

    while True:
        response = session.get(SEARCH_URL_TEMPLATE % offset)
        soup = bs4.BeautifulSoup(response.text, 'html5lib')
        for row in soup.select('.results > tbody > tr'):
            package = Package(*(c.get_text(strip=True) for c in row.select(':scope > td')[1:]))
            if not package.voted:
                return

            yield package

        offset += PACKAGES_PER_PAGE


def vote_package(session, package):
    response = session.post(VOTE_URL_TEMPLATE % package, {'do_Vote': 'Vote for this package'},
                            allow_redirects=True)                          
    return response.status_code == requests.codes.ok


def unvote_package(session, package):
    response = session.post(UNVOTE_URL_TEMPLATE % package, {'do_UnVote': 'Remove vote'},
                            allow_redirects=True)
    return response.status_code == requests.codes.ok


# TODO: Handle split packages better
def main(arguments):
    password = os.environ.get('AUR_AUTO_VOTE_PASSWORD') or getpass.getpass('Password: ')
    ignores = tuple(re.compile(r) for r in arguments.ignore)
    session = requests.Session()
    if not login(session, arguments.username, password):
        argument_parser.exit(EXIT_FAILURE, 'Could not login.\n')

    voted_packages = tuple(p.name for p in sorted(get_voted_packages(session)))

    # Unvote all packages and return immediately if --unvote-all was passed
    if arguments.unvote_all:
        for package in voted_packages:
            print('Unvoting package: %s... ' % package, end='', flush=True)
            if unvote_package(session, package):
                print('done.')
            else:
                print('failed.')
            time.sleep(arguments.delay)

        return

    foreign_packages = set(get_foreign_packages())
    voted_packages = set(voted_packages)
    for package in sorted(foreign_packages.difference(voted_packages)):
        if any(i.match(package) for i in ignores):
            print('Not voting for ignored package:', package)
            continue

        print('Voting for package: %s... ' % package, end='', flush=True)
        if vote_package(session, package):
            print('done.')
        else:
            print('failed.')
        time.sleep(arguments.delay)

    # Unvote packages that are voted for in the AUR but not available on the system anymore, i.e. uninstalled since the
    # last voting
    if arguments.no_unvote:
        return

    for package in sorted(voted_packages.difference(foreign_packages)):
        if any(i.match(package) for i in ignores):
            print('Not unvoting ignored package:', package)
            continue

        print('Unvoting removed package: %s... ' % package, end='', flush=True)
        if unvote_package(session, package):
            print('done.')
        else:
            print('failed.')
        time.sleep(arguments.delay)


if __name__ == '__main__':
    arguments = argument_parser.parse_args()
    main(arguments)
