#!/usr/bin/python3
import requests
import sys

API_BASE = 'http://concours/api'


def match(base, champion1, champion2, mape):
    s = requests.session()
    # acquire CSRF token
    s.get(base + "/matches/")
    try:
        token = s.cookies['csrftoken']
    except KeyError:
        print("Error: could not retrieve CSRF token (no SSO?)", file=sys.stderr)
        return 1
    res = s.post(base + "/matches/",
                 data={'champion_1': champion1, 'champion_2': champion2, 'csrfmiddlewaretoken': token, "map": mape})
    if not res.ok:
        print("Error", res.text, file=sys.stderr)
        return 1
    res = res.json()
    print(res)
    return 0


if __name__ == '__main__':
    l1 = []
    l2 = []
    mape = []
    step=0
    for i in range(1, len(sys.argv)):
        if sys.argv[i] == "-":
            step+=1
        elif step==0:
            l1.append(int(sys.argv[i]))
        elif step==1:
            l2.append(int(sys.argv[i]))
        else:
            mape.append(int(sys.argv[i]))
    for p1 in l1:
        for p2 in l2:
            for m in mape:
                match(API_BASE, p1, p2, m)
#                match(API_BASE, p2, p1, m)
