import json
from pprint import pprint

from google_play_scraper.constants.element import ElementSpecs
from google_play_scraper.constants.regex import Regex
from google_play_scraper.constants.url import Formats
from google_play_scraper.utils.request import get


def app(app_id, lang="en", country="us"):
    # type: (str, str, str) -> dict
    url = Formats.Detail.build(app_id=app_id, lang=lang, country=country)

    dom = get(url)

    matches = Regex.SCRIPT.findall(dom)

    res = {}

    for match in matches:
        key_match = Regex.KEY.findall(match)
        value_match = Regex.VALUE.findall(match)

        if key_match and value_match:
            key = key_match[0]
            value = json.loads(value_match[0])

            res[key] = value

    result = {}

    for k, spec in ElementSpecs.Detail.items():
        content = spec.extract_content(res)

        result[k] = content

    result["appId"] = app_id
    result["url"] = url

    return result
