import logging

from django.core.exceptions import ValidationError
from django.core.validators import URLValidator

from lxml.etree import XMLSyntaxError, parse

from newsreader.news.collection.models import CollectionRule


def parse_opml(file, user, skip_existing=False):
    known_urls = CollectionRule.objects.filter(user=user).values_list("url", flat=True)

    try:
        tree = parse(file)
    except XMLSyntaxError as e:
        raise IOError("Invalid file") from e

    root = tree.getroot()

    validate = URLValidator(schemes=["http", "https"])

    for element in root.iter(tag="outline"):
        if "xmlUrl" not in element.keys():
            continue

        feed_url = element.get("xmlUrl")
        name = element.get("text")

        if not all([feed_url, name]):
            continue
        elif skip_existing and feed_url in known_urls:
            continue

        try:
            validate(feed_url)
        except ValidationError as e:
            logging.info(f"Skipped due to invalid URL: {e}")
            continue

        # TODO create feed type rules
        yield CollectionRule(url=feed_url, name=name, user=user)
