import { Innertube, Session } from 'youtubei.js';
import { env } from '../../config.js';
import { cleanString } from '../../sub/utils.js';
import { fetch } from 'undici'

const ytBase = await Innertube.create();

const codecMatch = {
    h264: {
        codec: "avc1",
        aCodec: "mp4a",
        container: "mp4"
    },
    av1: {
        codec: "av01",
        aCodec: "mp4a",
        container: "mp4"
    },
    vp9: {
        codec: "vp9",
        aCodec: "opus",
        container: "webm"
    }
}

const cloneInnertube = (customFetch) => {
    const session = new Session(
        ytBase.session.context,
        ytBase.session.key,
        ytBase.session.api_version,
        ytBase.session.account_index,
        ytBase.session.player,
        undefined,
        customFetch ?? ytBase.session.http.fetch,
        ytBase.session.cache
    );

    const yt = new Innertube(session);
    return yt;
}

export default async function(o) {
    const yt = cloneInnertube(
        (input, init) => fetch(input, { ...init, dispatcher: o.dispatcher })
    );

    let info, isDubbed, format = o.format || "h264";
    let quality = o.quality === "max" ? "9000" : o.quality; // 9000(p) - max quality

    function qual(i) {
        if (!i.quality_label) {
            return;
        }

        return i.quality_label.split('p')[0].split('s')[0]
    }

    try {
        info = await yt.getBasicInfo(o.id, 'WEB');
    } catch {
        return { error: 'ErrorCantConnectToServiceAPI' };
    }

    if (!info) return { error: 'ErrorCantConnectToServiceAPI' };

    if (info.playability_status.status !== 'OK') return { error: 'ErrorYTUnavailable' };
    if (info.basic_info.is_live) return { error: 'ErrorLiveVideo' };

    // return a critical error if returned video is "Video Not Available"
    // or a similar stub by youtube
    if (info.basic_info.id !== o.id) {
        return {
            error: 'ErrorCantConnectToServiceAPI',
            critical: true
        }
    }

    let bestQuality, hasAudio;

    const filterByCodec = (formats) => formats.filter(e => 
        e.mime_type.includes(codecMatch[format].codec) || e.mime_type.includes(codecMatch[format].aCodec)
    ).sort((a, b) => Number(b.bitrate) - Number(a.bitrate));

    let adaptive_formats = filterByCodec(info.streaming_data.adaptive_formats);
    if (adaptive_formats.length === 0 && format === "vp9") {
        format = "h264"
        adaptive_formats = filterByCodec(info.streaming_data.adaptive_formats)
    }

    bestQuality = adaptive_formats.find(i => i.has_video && i.content_length);
    hasAudio = adaptive_formats.find(i => i.has_audio && i.content_length);

    if (bestQuality) bestQuality = qual(bestQuality);
    if (!bestQuality && !o.isAudioOnly || !hasAudio) return { error: 'ErrorYTTryOtherCodec' };
    if (info.basic_info.duration > env.durationLimit) return { error: ['ErrorLengthLimit', env.durationLimit / 60] };

    let checkBestAudio = (i) => (i.has_audio && !i.has_video),
        audio = adaptive_formats.find(i => checkBestAudio(i) && !i.is_dubbed);

    if (o.dubLang) {
        let dubbedAudio = adaptive_formats.find(i =>
            checkBestAudio(i) && i.language === o.dubLang && i.audio_track && !i.audio_track.audio_is_default
        );
        if (dubbedAudio) {
            audio = dubbedAudio;
            isDubbed = true
        }
    }
    let fileMetadata = {
        title: cleanString(info.basic_info.title.trim()),
        artist: cleanString(info.basic_info.author.replace("- Topic", "").trim()),
    }
    if (info.basic_info.short_description && info.basic_info.short_description.startsWith("Provided to YouTube by")) {
        let descItems = info.basic_info.short_description.split("\n\n");
        fileMetadata.album = descItems[2];
        fileMetadata.copyright = descItems[3];
        if (descItems[4].startsWith("Released on:")) {
            fileMetadata.date = descItems[4].replace("Released on: ", '').trim()
        }
    }

    let filenameAttributes = {
        service: "youtube",
        id: o.id,
        title: fileMetadata.title,
        author: fileMetadata.artist,
        youtubeDubName: isDubbed ? o.dubLang : false
    }

    if (hasAudio && o.isAudioOnly) return {
        type: "render",
        isAudioOnly: true,
        urls: audio.decipher(yt.session.player),
        filenameAttributes: filenameAttributes,
        fileMetadata: fileMetadata,
        bestAudio: format === "h264" ? 'm4a' : 'opus'
    }
    const matchingQuality = Number(quality) > Number(bestQuality) ? bestQuality : quality,
        checkSingle = i => qual(i) === matchingQuality && i.mime_type.includes(codecMatch[format].codec),
        checkRender = i => qual(i) === matchingQuality && i.has_video && !i.has_audio;

    let match, type, urls;
    if (!o.isAudioOnly && !o.isAudioMuted && format === 'h264') {
        match = info.streaming_data.formats.find(checkSingle);
        type = "bridge";
        urls = match?.decipher(yt.session.player);
    }

    const video = adaptive_formats.find(checkRender);
    if (!match && video) {
        match = video;
        type = "render";
        urls = [video.decipher(yt.session.player), audio.decipher(yt.session.player)];
    }

    if (match) {
        filenameAttributes.qualityLabel = match.quality_label;
        filenameAttributes.resolution = `${match.width}x${match.height}`;
        filenameAttributes.extension = codecMatch[format].container;
        filenameAttributes.youtubeFormat = format;
        return {
            type,
            urls,
            filenameAttributes, 
            fileMetadata
        }
    }

    return { error: 'ErrorYTTryOtherCodec' }
}
