import json
import logging
from typing import TYPE_CHECKING

import urllib3

from article_import_utils.utils import create_image_from_url
from main.models import MainArticlePage, MainArticlesPage, ARTICLE_TYPES

if TYPE_CHECKING:
    pass

logger = logging.getLogger()


class ArticleDownloadService:
    api_url = 'https://piratipracuji.cz/api/'

    @staticmethod
    def get_existing_articles_slugs() -> list[str]:
        return MainArticlePage.objects.filter(article_type=ARTICLE_TYPES.WORK_TIMELINE).values_list('slug', flat=True)

    def get_articles_response(self):
        response = urllib3.PoolManager().request('GET', self.api_url)
        data = json.loads(response.data)

        return data

    def perform_update(self):
        existing_articles_slug_list = self.get_existing_articles_slugs()

        for article in self.get_articles_response():
            if article['slug'] not in existing_articles_slug_list:
                if 'thumbnail' in article:
                    img_filename = 'article-' + str(article['id']) + '.jpg'
                    img_obj = create_image_from_url(url=article['thumbnail'], filename=img_filename)
                else:
                    img_obj = None

                article_to_save = MainArticlePage(article_type=ARTICLE_TYPES.WORK_TIMELINE,
                                                  title=article['title'],
                                                  slug=article['slug'],
                                                  perex=article['description'].replace(" ", ""),
                                                  author='ČESKÁ PIRÁTSKÁ STRANA',
                                                  date=article['start_date'],
                                                  image=img_obj,
                                                  content=json.dumps([
                                                      {'type': 'text',
                                                       'value': article['content'].replace("</p>", "</p><br>")}
                                                  ])
                                                  )
                parent = MainArticlesPage.objects.all().first()
                parent.add_child(instance=article_to_save)