from io import StringIO
from html.parser import HTMLParser
import json
import requests
import urllib.parse
import urllib.request


def get_token(session):
    if not hasattr(session, "token") or not session.token:
        session.token = session.login()
    return session.token


def get_kinds(param):
    if "fileFormat" not in param.attributes:
        return []  # Default to an empty list

    # Get the list from the attributes
    kinds_string = param.attributes["fileFormat"]

    if kinds_string == "" or kinds_string == "*":
        return []  # Accepts all known kinds

    return kinds_string.split(";")


def get_permissions(job):
    url = f"{job.server_data.url}/rest/v1/jobs/{job.job_number}/permissions"
    request = urllib.request.Request(url)
    if job.server_data.authorization_header() is not None:
        request.add_header("Authorization", job.server_data.authorization_header())
    request.add_header("User-Agent", "GalaxyJupyterLabRest")

    response = urllib.request.urlopen(request)
    return json.loads(response.read())


def set_permissions(job, permissions):
    url = f"{job.server_data.url}/rest/v1/jobs/{job.job_number}/permissions"
    data = json.dumps(permissions).encode("utf8")
    request = urllib.request.Request(url, data=data, method="PUT")
    if job.server_data.authorization_header() is not None:
        request.add_header("Authorization", job.server_data.authorization_header())
    request.add_header("User-Agent", "GalaxyJupyterLabRest")
    urllib.request.urlopen(request)

    # def login(session):
    #    safe_url = ensure_safe_url(session._notebook_url)
    #    safe_email = urllib.parse.quote(session._notebook_email)
    #    safe_password = urllib.parse.quote(session._notebook_password)

    #    url = f"{safe_url}/rest/v1/oauth2/token?grant_type=password&username={safe_username}&password={safe_password}&client_id=GenePatternNotebook-{safe_username}"
    #    response = requests.post(url)

    #    try:
    #        response.raise_for_status()
    #        return response.json()['access_token']
    #    except requests.exceptions.Timeout:
    #        raise TimeoutError('Connection timed out attempting to contact GenePattern server')
    #    except requests.exceptions.TooManyRedirects:
    #        raise TimeoutError('Bad GenePattern server URL')
    #    except requests.exceptions.RequestException as e:
    #        raise TimeoutError('Invalid email or password')

    # def system_message(session):
    #    safe_url = ensure_safe_url(session._notebook_url)
    #    url = f"{safe_url}/rest/v1/config/system-message"
    #    response = requests.get(url)
    #    return strip_html(response.text)

    # def get_task(session, lsid_or_name): #ToDo
    #    url = f'{session._notebook_url}/rest/v1/tasks/{lsid_or_name}/'
    #    request = urllib.request.Request(url)
    #    if session.authorization_header() is not None:
    #        request.add_header('Authorization', session.authorization_header())
    #    request.add_header('User-Agent', 'GalaxyJupyterLabRest')#

    #    response = urllib.request.urlopen(request)
    #    task_dict = json.loads(response.read())
    #   return GalaxyTask(session, task_dict['lsid'], task_dict)

    # class HTMLStripper(HTMLParser):
    #    """Parse HTML blob and strip out all tags"""
    #    def __init__(self):
    #        super().__init__()
    #        self.reset()
    #        self.strict = False
    #        self.convert_charrefs = True
    #        self.text = StringIO()

    #    def error(self, message):
    #        pass

    #    def handle_data(self, d):
    #        self.text.write(d)

    #    def get_data(self):
    #        return self.text.getvalue()

    # def strip_html(html):
    """Strip potentially unsafe HTML from the system message string"""
    #    s = HTMLStripper()
    #    s.feed(html)
    #    return s.get_data()

    # def ensure_safe_url(url):
    """Ensure the Galaxy URL ends with /api"""


#    if url.endswith('/'):
#        url = url[:-1]
#    if not url.endswith('/api'):
#        url += '/api'
#    return url
