import { SearchResult, VideoLink, SeasonOption } from '../../src/types/source.js';

function getMediaTypeFromUrl(url: string): 'movie' | 'series' {
    return url.includes('/serie/') ? 'series' : 'movie';
}

function unescapeHtml(html: string): string {
    return html
        .replace(/&amp;/g, '&')
        .replace(/&lt;/g, '<')
        .replace(/&gt;/g, '>')
        .replace(/&quot;/g, '"')
        .replace(/&#39;/g, "'")
        .replace(/&#038;/g, '&');
}

export class FlixArtParser {
    static parseSearchAjax(htmlStr: string): SearchResult[] {
        const results: SearchResult[] = [];
        const cardRegex = /<a class="[^"]*flixart-search-card[^"]*" href="([^"]+)"[^>]*>([\s\S]*?)<\/a>/g;
        let match;
        
        while ((match = cardRegex.exec(htmlStr)) !== null) {
            const href = unescapeHtml(match[1]);
            const inner = match[2];
            
            let title = '';
            const titleMatch = inner.match(/<span class="flixart-search-result-title">([^<]+)<\/span>/);
            if (titleMatch) title = unescapeHtml(titleMatch[1].trim());
            
            let year = null;
            const yearMatch = inner.match(/<span class="video-years">([^<]+)<\/span>/);
            if (yearMatch) year = yearMatch[1].trim();
            
            let image = null;
            const imgMatch = inner.match(/<img[^>]+src="([^"]+)"/);
            if (imgMatch) {
                image = unescapeHtml(imgMatch[1]);
                if (image.includes('&quality=')) image = image.split('&quality=')[0];
            }
            
            if (title && href) {
                results.push({
                    title,
                    year,
                    image,
                    hrefPath: href,
                    type: getMediaTypeFromUrl(href),
                    source: 'flixart'
                });
            }
        }

        return results;
    }

    static parseTrending(htmlStr: string, isSeries: boolean): SearchResult[] {
        const results: SearchResult[] = [];
        const sectionTitle = isSeries ? 'Top 10 séries du jour' : 'Top 10 films du jour';
        const fallbackTitle = isSeries ? 'Nouveautés séries' : 'Nouveautés films';
        
        // Find section containing the title
        let sectionRegexStr = `<section class="fx-section">\\s*<div class="fx-section-head">\\s*<h2>(${sectionTitle}|${fallbackTitle})<\\/h2>[\\s\\S]*?<\\/section>`;
        let sectionMatch = htmlStr.match(new RegExp(sectionRegexStr, 'i'));
        
        if (!sectionMatch) return results;
        const sectionHtml = sectionMatch[0];
        
        const cardRegex = /<article class="fx-card[^"]*">([\s\S]*?)<\/article>/g;
        let match;
        while ((match = cardRegex.exec(sectionHtml)) !== null) {
            const inner = match[1];
            
            let href = null;
            let title = '';
            const titleMatch = inner.match(/<h3 class="fx-card-title"><a href="([^"]+)"[^>]*>([^<]+)<\/a><\/h3>/);
            if (titleMatch) {
                href = unescapeHtml(titleMatch[1]);
                title = unescapeHtml(titleMatch[2].trim());
            }
            
            let image = null;
            const imgMatch = inner.match(/<img src="([^"]+)"/);
            if (imgMatch) {
                image = unescapeHtml(imgMatch[1]);
                if (image.includes('&quality=')) image = image.split('&quality=')[0];
            }
            
            let year = null;
            const yearMatch = inner.match(/<span>(\d{4})<\/span>\s*<\/span>/);
            if (yearMatch) {
                year = yearMatch[1];
            }
            
            if (title && href) {
                results.push({
                    title,
                    year,
                    image,
                    hrefPath: href,
                    type: isSeries ? 'series' : 'movie',
                    source: 'flixart'
                });
            }
        }

        return results;
    }

    static parseSelection(htmlStr: string): { links: VideoLink[], seasons: SeasonOption[], isSeries: boolean, postId: string, nonce: string | null } {
        const links: VideoLink[] = [];
        const seasons: SeasonOption[] = [];
        let isSeries = false;

        let postId = '';
        const postIdMatch = htmlStr.match(/data-post-id="(\d+)"/);
        if (postIdMatch) postId = postIdMatch[1];

        let nonce: string | null = null;
        const nonceMatch = htmlStr.match(/flixartDownloadCaptcha\s*=\s*\{[^}]*nonce:\s*'([^']+)'/);
        if (nonceMatch) nonce = nonceMatch[1];

        const seasonTabRegex = /<button[^>]+class="[^"]*flixart-season-tab[^"]*"[^>]+data-season="([^"]+)"[^>]*>([\s\S]*?)<\/button>/g;
        let match;
        while ((match = seasonTabRegex.exec(htmlStr)) !== null) {
            isSeries = true;
            const val = match[1];
            const inner = match[2];
            const numMatch = inner.match(/<span class="flixart-season-tab__number">([^<]+)<\/span>/);
            if (numMatch) {
                seasons.push({ label: `Saison ${numMatch[1].trim()}`, value: val });
            }
        }

        const rowRegex = /<div role="row" class="jws-lien-row[^"]*"[^>]*data-qualite="([^"]*)"[^>]*data-langue="([^"]*)"[^>]*>([\s\S]*?)<\/div>/g;
        while ((match = rowRegex.exec(htmlStr)) !== null) {
            const inner = match[3];
            let episode = null;
            const episodeMatch = htmlStr.substring(match.index - 100, match.index).match(/data-episode="([^"]+)"/);
            if (episodeMatch) episode = episodeMatch[1];
            
            const checkboxMatch = inner.match(/<input[^>]+data-flixart-download-select[^>]+data-row-index="(\d+)"[^>]*data-download-title="([^"]*)"[^>]*data-download-meta="([^"]*)"/);
            if (checkboxMatch) {
                const rowIndex = checkboxMatch[1];
                const title = unescapeHtml(checkboxMatch[2]);
                const meta = unescapeHtml(checkboxMatch[3]);
                
                let host = 'Inconnu';
                const lowerMeta = meta.toLowerCase();
                if (lowerMeta.includes('1fichier')) host = '1fichier';
                else if (lowerMeta.includes('uptobox')) host = 'uptobox';
                else if (lowerMeta.includes('nitroflare')) host = 'nitroflare';
                else if (lowerMeta.includes('ddownload')) host = 'ddownload';
                
                links.push({
                    id: rowIndex,
                    host,
                    label: title,
                    quality: meta,
                    url: null,
                    episode
                });
            }
        }

        return { links, seasons, isSeries, postId, nonce };
    }
}
