import hashlib
import io
import mimetypes
from typing import Dict, Optional, Tuple

import requests
from furl import furl
from PIL import Image

from tungstenkit._internal.logging import log_debug, log_info

GRAVATAR_BASE_URL = "https://www.gravatar.com/avatar"


def get_default_avatar_png(name: str, size: int = 80, default: str = "retro") -> bytes:
    digest = hashlib.md5(name.encode("utf-8")).hexdigest()
    f = furl(GRAVATAR_BASE_URL)
    f.path = f.path / (digest + ".png")
    f.args["d"] = default
    f.args["s"] = str(size)
    log_debug(f"Get an avatar from '{f.url}'")
    try:
        r = requests.get(f.url)
        r.raise_for_status()
        avatar = r.content
    except requests.RequestException as e:
        log_info(f"Failed to get avatar: {type(e)}: {str(e)}", pretty=False)
        log_info(f"Set an empty image as avatar for {name}")

        img = Image.new("RGB", (size, size), color=(0, 0, 0))
        buf = io.BytesIO()
        img.save(buf, format="PNG")
        avatar = buf.getvalue()

    return avatar


def fetch_avatar(url: str, headers: Optional[Dict[str, str]] = None) -> Tuple[bytes, str]:
    mimetype = mimetypes.guess_type(url, strict=False)[0]
    ext = mimetypes.guess_extension(mimetype) if mimetype else None
    r = requests.get(url, headers=headers)

    r.raise_for_status()

    if ext is None:
        buf = io.BytesIO(r.content)
        im = Image.open(buf)
        ext = im.format.lower()

    return r.content, ext
