import json

import requests

import influxframework


@influxframework.whitelist()
def remote_login():
	try:
		login_url = influxframework.conf.subscription["login_url"]
		if influxframework.conf.subscription["expiry"] and login_url:
			resp = requests.post(login_url)

			if resp.status_code != 200:
				return

			return json.loads(resp.text)["message"]
	except Exception:
		return False

	return False


def enable_manage_subscription():
	if not influxframework.db.exists("Navbar Item", {"item_label": "Manage Subscriptions"}):
		return

	navbar_item, hidden = influxframework.db.get_value(
		"Navbar Item", {"item_label": "Manage Subscriptions"}, ["name", "hidden"]
	)
	if navbar_item and hidden:
		doc = influxframework.get_cached_doc("Navbar Item", navbar_item)
		doc.hidden = False
		doc.save()
