import json
import logging

import pendulum
import requests
from django.conf import settings

API_KEY = settings.GOOGLE_API_KEY
GOOGLE_BOOKS_URL = (
    "https://www.googleapis.com/books/v1/volumes?q=\"{title}\"&key={key}"
)

logger = logging.getLogger(__name__)


def lookup_book_from_google(title: str) -> dict:
    book_dict = {"title": title}

    url = GOOGLE_BOOKS_URL.format(title=title, key=API_KEY)
    headers = {"User-Agent": "Vrobbler 0.11.12"}
    response = requests.get(url, headers=headers)

    if response.status_code != 200:
        logger.warning(
            "Bad response from Google", extra={"response": response}
        )
        return book_dict

    google_result = (
        json.loads(response.content).get("items", [{}])[0].get("volumeInfo")
    )
    if not google_result:
        return {}

    publish_date = pendulum.parse(google_result.get("publishedDate"))

    isbn_13 = ""
    isbn_10 = ""
    for ident in google_result.get("industryIdentifiers", []):
        if ident.get("type") == "ISBN_13":
            isbn_13 = ident.get("identifier")
        if ident.get("type") == "ISBN_10":
            isbn_10 = ident.get("identifier")
    # TODO this may lead to issues with the first get if Google changes our title
    # book_metadata.title = google_result.get("title")
    # if google_result.get("subtitle"):
    #    book_metadata["title"] = ": ".join(
    #        [google_result.get("title"), google_result.get("subtitle")]
    #    )
    # book_dict["subtitle"] = google_result.get("subtitle")
    book_dict["authors"] = google_result.get("authors")
    book_dict["publisher"] = google_result.get("publisher")
    book_dict["first_publish_year"] = publish_date.year
    book_dict["pages"] = google_result.get("pageCount")
    book_dict["isbn_13"] = isbn_13
    book_dict["isbn_10"] = isbn_10
    book_dict["publish_date"] = google_result.get("publishedDate")
    if len(book_dict["publish_date"]) == 4:
        book_dict["publish_date"] = f"{book_dict['publish_date']}-1-1"
    book_dict["language"] = google_result.get("language")
    book_dict["summary"] = google_result.get("description")
    book_dict["genres"] = google_result.get("categories")
    book_dict["cover_url"] = (
        google_result.get("imageLinks", {})
        .get("thumbnail", "")
        .replace("zoom=1", "zoom=15")
        .replace("&edge=curl", "")
    )

    book_dict["base_run_time_seconds"] = 3600
    if book_dict.get("pages"):
        book_dict["base_run_time_seconds"] = book_dict.get("pages", 10) * getattr(
            settings, "AVERAGE_PAGE_READING_SECONDS", 60
        )

    return book_dict
