#!/usr/bin/python3
# Licensed under copyleft-next 0.3.1


# Import standard modules
import json, os.path
# Requires python3-requests
import requests
# Lets make this all GUI!
from PySide2.QtGui import QGuiApplication
from PySide2.QtQml import QQmlApplicationEngine
from PySide2.QtQml import qmlRegisterType
from PySide2.QtCore import QStringListModel


if __name__ == "__main__":
	print("Here we go...")
	apitoken = open(os.path.dirname(os.path.realpath(__file__)) + "/phabricator-apitoken", 'r').read().rstrip()

	mqurl = "https://phabricator.keithzg.ca/api/maniphest.query"

	r = requests.post(mqurl, data = {"api.token": apitoken, "order": "order-priority", "status": "status-open"})


	taskdetails = json.loads(r.text)
	tasklist = []
	tds = taskdetails["result"]
	for t in tds:
		tasklist.append(tds[t]["objectName"] + ": " + tds[t]["title"])

	app = QGuiApplication()
	engine = QQmlApplicationEngine()

	model = QStringListModel()
	model.setStringList(tasklist)

	context = engine.rootContext()
	context.setContextProperty("myModel", model)
	context.setContextProperty("taskJson", tds)
	engine.load("manifold.qml")


	if len(engine.rootObjects()) == 0:
		quit()
	app.exec_()

