import os
from tempfile import TemporaryDirectory

from django.db import models
from djconfig import config
from bs4 import BeautifulSoup
import requests
from PIL import Image
from telegram import Bot

from feeds.models import FeedModuleConfig


class TapasFeedModuleConfig(FeedModuleConfig):
    display_name = models.CharField(max_length=256)

    MODULE_NAME = 'Tapas.io comic'

    def execute(self, bot: Bot, chat_id, last_id):
        config._reload_maybe()

        if last_id is None:
            last_id = 0

        series_page = requests.get(
            f'https://tapas.io/series/{self.display_name}',
            headers={'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:67.0) Gecko/20100101 Firefox/67.0'},
        ).text
        ssoup = BeautifulSoup(series_page, 'html.parser')
        # episode_list = json.loads(re.search(r'episodeList : (\[.*\]),', series_page).group(1))
        for episode in ssoup.select('a[data-ga-category=Episode]'):
            eid = int(episode['data-id'])
            title = episode.select_one('.info__title').text.strip()
            if eid <= last_id:
                continue
            # if not episode['free']:
            #     continue

            caption = f'{title}\nhttps://tapas.io/episode/{eid}'

            esoup = BeautifulSoup(requests.get(
                f'https://tapas.io/episode/{eid}',
                headers={'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:67.0) Gecko/20100101 Firefox/67.0'},
            ).text, 'html.parser')
            imgs = []
            for img in esoup.select(f'.js-episode-article .content__img'):
                imgs.append(img['data-src'])

            if len(imgs) == 1:
                bot.send_photo(chat_id, imgs[0], caption=caption)
            if len(imgs) > 1:
                with TemporaryDirectory() as d:
                    ims = []
                    for i, img in enumerate(imgs):
                        img_path = os.path.join(d, f'{i}.jpg')
                        with open(img_path, 'wb') as f:
                            f.write(requests.get(img).content)
                        ims.append(Image.open(img_path))
                    widths, heights = zip(*(i.size for i in ims))
                    max_width = max(widths)
                    total_height = sum(heights)
                    new_im = Image.new('RGB', (max_width, total_height))
                    offset = 0
                    for im in ims:
                        new_im.paste(im, (0, offset))
                        offset += im.size[1]
                    new_im_path = os.path.join(d, 'combined.jpg')
                    new_im.save(new_im_path)
                    with open(new_im_path, 'rb') as f:
                        bot.send_document(chat_id, f, caption=caption)
            yield eid
