import typing
import requests


def parseTOURL(url:str, verify:bool) -> typing.Tuple[bool, str, int]:
	"""
	Parses and verifies the passed URL and breaks it into parts for the caller

	:param url: At minimum an FQDN for a Traffic Ops server, but can include schema and port number
	:param verify: Whether or not to verify the server's SSL certificate
	:returns: Whether or not the Traffic Ops server uses SSL (http vs https), the server's FQDN, and the port on which it listens
	:raises ValueError: if ``url`` does not point at a valid HTTP server or is incorrectly formatted
	"""
	url = url.rstrip('/')

	useSSL, host, port = True, None, 443

	try:
		_ = requests.head(url, verify=verify)
	except requests.exceptions.RequestException as e:
		raise ValueError("Cannot contact any server at '%s' (%s)" % (url, e)) from e

	if url.lower().startswith("http://"):
		port = 80
		useSSL = False
		url = url[7:]
	elif url.lower().startswith("https://"):
		url = url[8:]

	# I'm assuming here that a valid FQDN won't include ':' - and it shouldn't
	portpoint = url.find(':')
	if portpoint > 0:
		host = url[:portpoint]
		port = int(url[portpoint+1:])
	else:
		host = url

	return useSSL, host, port