from datetime import datetime, timezone

from django.conf import settings
from django.db.models.fields import CharField, TextField

import requests

from requests.exceptions import RequestException

from newsreader.news.collection.response_handler import ResponseHandler


DEFAULT_HEADERS = {"User-Agent": f"linux:rss.fudiggity.nl:{settings.VERSION}"}


def build_publication_date(_datetime_info: tuple) -> datetime:
    try:
        return datetime(*_datetime_info[:6], tzinfo=timezone.utc)
    except (TypeError, ValueError):
        return datetime.now(tz=timezone.utc)


def fetch(url, auth=None, headers={}):
    headers = {**DEFAULT_HEADERS, **headers}

    with ResponseHandler() as response_handler:
        try:
            response = requests.get(url, auth=auth, headers=headers)
            response_handler.handle_response(response)
        except RequestException as exception:
            response_handler.map_exception(exception)

    return response


def post(url, data=None, auth=None, headers={}):
    headers = {**DEFAULT_HEADERS, **headers}

    with ResponseHandler() as response_handler:
        try:
            response = requests.post(url, data=data, auth=auth, headers=headers)
            response_handler.handle_response(response)
        except RequestException as exception:
            response_handler.map_exception(exception)

    return response


def truncate_text(cls, field_name, value):
    field = cls._meta.get_field(field_name)
    max_length = field.max_length
    field_cls = type(field)

    is_charfield = bool(issubclass(field_cls, CharField))
    is_textfield = bool(issubclass(field_cls, TextField))

    if not value or not max_length:
        return value
    elif not is_charfield or is_textfield:
        return value

    if len(value) > max_length:
        return f"{value[: max_length - 1]}…"

    return value
