import pathlib

import _static
import dotenv
import requests
from jinja2 import Template
from requests import Response
from rich import print

from mealie.schema._mealie import MealieModel

BASE = pathlib.Path(__file__).parent.parent.parent

API_KEY = dotenv.get_key(BASE / ".env", "CROWDIN_API_KEY")

NAMES = {
    "en-US": "American English",
    "en-GB": "British English",
    "af-ZA": "Afrikaans (Afrikaans)",
    "ar-SA": "العربية (Arabic)",
    "ca-ES": "Català (Catalan)",
    "cs-CZ": "Čeština (Czech)",
    "da-DK": "Dansk (Danish)",
    "de-DE": "Deutsch (German)",
    "el-GR": "Ελληνικά (Greek)",
    "es-ES": "Español (Spanish)",
    "fi-FI": "Suomi (Finnish)",
    "fr-FR": "Français (French)",
    "he-IL": "עברית (Hebrew)",
    "hu-HU": "Magyar (Hungarian)",
    "it-IT": "Italiano (Italian)",
    "ja-JP": "日本語 (Japanese)",
    "ko-KR": "한국어 (Korean)",
    "no-NO": "Norsk (Norwegian)",
    "nl-NL": "Nederlands (Dutch)",
    "pl-PL": "Polski (Polish)",
    "pt-BR": "Português do Brasil (Brazilian Portuguese)",
    "pt-PT": "Português (Portugese)",
    "ro-RO": "Română (Romanian)",
    "ru-RU": "Pусский (Russian)",
    "sr-SP": "српски (Serbian)",
    "sv-SE": "Svenska (Swedish)",
    "tr-TR": "Türkçe (Turkish)",
    "uk-UA": "Українська (Ukrainian)",
    "vi-VN": "Tiếng Việt (Vietnamese)",
    "zh-CN": "简体中文 (Chinese simplified)",
    "zh-TW": "繁體中文 (Chinese traditional)",
}

LOCALE_TEMPLATE = """// This Code is auto generated by gen_global_components.py
export const LOCALES = [{% for locale in locales %}
  {
    name: "{{ locale.name }}",
    value: "{{ locale.locale }}",
    progress: {{ locale.progress }},
  },{% endfor %}
]
"""


class TargetLanguage(MealieModel):
    id: str
    name: str
    locale: str
    threeLettersCode: str
    twoLettersCode: str
    progress: float = 0.0


class CrowdinApi:
    project_name = "Mealie"
    project_id = "451976"
    api_key = API_KEY

    def __init__(self, api_key: str):
        api_key = api_key

    @property
    def headers(self) -> dict:
        return {
            "Content-Type": "application/json",
            "Authorization": f"Bearer {self.api_key}",
        }

    def get_projects(self) -> Response:
        return requests.get("https://api.crowdin.com/api/v2/projects", headers=self.headers)

    def get_project(self) -> Response:
        return requests.get(f"https://api.crowdin.com/api/v2/projects/{self.project_id}", headers=self.headers)

    def get_languages(self) -> list[TargetLanguage]:
        response = self.get_project()
        tls = response.json()["data"]["targetLanguages"]

        models = [TargetLanguage(**t) for t in tls]

        models.insert(
            0,
            TargetLanguage(
                id="en-US", name="English", locale="en-US", threeLettersCode="en", twoLettersCode="en", progress=100
            ),
        )

        progress: list[dict] = self.get_progress()["data"]

        for model in models:
            if model.locale in NAMES:
                model.name = NAMES[model.locale]

            for p in progress:
                if p["data"]["languageId"] == model.id:
                    model.progress = p["data"]["translationProgress"]

        models.sort(key=lambda x: x.locale, reverse=True)
        return models

    def get_progress(self) -> dict:
        response = requests.get(
            f"https://api.crowdin.com/api/v2/projects/{self.project_id}/languages/progress?limit=500",
            headers=self.headers,
        )
        return response.json()


def main():
    print("Starting...")  # noqa

    if API_KEY is None:
        print("CROWDIN_API_KEY is not set")  # noqa
        return

    api = CrowdinApi("")
    models = api.get_languages()
    tmpl = Template(LOCALE_TEMPLATE)
    rendered = tmpl.render(locales=models)

    with open(_static.CodeDest.use_locales, "w") as f:
        f.write(rendered)  # type:ignore

    print("Finished...")  # noqa


if __name__ == "__main__":
    main()
