from .base import BaseVideoPlayer
from bs4 import BeautifulSoup
import re
import httpx


class DoodStreamDownloader(BaseVideoPlayer):
    """Downloader for doodstream.com"""

    def can_handle(self, url: str) -> bool:
        return any(domain in url.lower() for domain in ["doodstream.com", "dood.stream", "dood.to", "dood.lol", "dood.cx", "dood.so", "dood.watch", "dood.sh"])

    async def get_download_link(self, url: str, target_filename: str = None) -> tuple[str, str]:
        try:
            # Get the page
            response = await self.client.get(url)
            response.raise_for_status()
            soup = BeautifulSoup(response.text, 'lxml')

            # Doodstream usually has the video URL in a script with '$(function)'
            # or in a token-based system
            download_url = None
            filename = "doodstream_video.mp4"

            # Method 1: Look for /pass_md5 or similar patterns
            scripts = soup.find_all('script')
            for script in scripts:
                if script.string:
                    # Look for token patterns
                    match = re.search(r'https?://[^\"\']+\.(?:mp4|mkv|avi)', script.string)
                    if match:
                        download_url = match.group(0)
                        break

                    # Look for doodstream CDN patterns
                    match = re.search(r'(https?://[^\s\"\'<>]+/download/[^\s\"\'<>]+)', script.string)
                    if match:
                        download_url = match.group(0)
                        break

            # Method 2: Try to construct download URL from page
            if not download_url:
                # Extract video ID from URL
                # Format: https://doodstream.com/e/VIDEO_ID or /d/VIDEO_ID
                video_id_match = re.search(r'/[ed]/([a-zA-Z0-9]+)', url)
                if video_id_match:
                    video_id = video_id_match.group(1)
                    # Try direct download pattern
                    download_url = f"https://dood.stream/e/{video_id}"

            # Method 3: Look for any MP4 source in iframes or video tags
            if not download_url:
                video = soup.find('video')
                if video and video.get('src'):
                    download_url = video['src']
                else:
                    sources = soup.find_all('source')
                    for source in sources:
                        if source.get('src'):
                            download_url = source['src']
                            filename = source.get('src', '').split('/')[-1]
                            break

            if download_url:
                # Try to get real filename from HEAD request
                try:
                    head_resp = await self.client.head(download_url, timeout=5.0)
                    fname = self._extract_filename_from_headers(head_resp.headers)
                    if fname:
                        filename = fname
                except:
                    pass

                return download_url, filename

            raise Exception("Could not extract download link from Doodstream page")

        except Exception as e:
            raise Exception(f"Error extracting Doodstream link: {str(e)}")
