'use strict';

const argv = require('../argv');
const {
    fetchLatest,
    fetchApiLatest,
    fetchUpcoming,
    fetchApiUpcoming,
    fetchScene,
    fetchProfile,
    fetchApiProfile,
    scrapeAll,
} = require('./gamma');
const { get } = require('../utils/q');
const slugify = require('../utils/slugify');

function extractLowArtActors(release) {
    const actors = release.title
        .replace(/solo/i, '')
        .split(/,|\band\b/ig)
        .map(actor => actor.trim());

    return {
        ...release,
        actors,
    };
}

async function networkFetchLatest(site, page = 1) {
    if (site.parameters?.api) return fetchApiLatest(site, page, false);

    const releases = await fetchLatest(site, page);

    if (site.slug === 'lowartfilms') {
        return releases.map(release => extractLowArtActors(release));
    }

    return releases;
}

async function networkFetchScene(url, site) {
    const release = await fetchScene(url, site);

    if (site.slug === 'lowartfilms') {
        return extractLowArtActors(release);
    }

    return release;
}

async function networkFetchUpcoming(site, page = 1) {
    if (site.parameters?.api) return fetchApiUpcoming(site, page, true);

    return fetchUpcoming(site, page);
}

function getActorReleasesUrl(actorPath, page = 1) {
    return `https://www.peternorth.com/en/videos/All-Categories/0${actorPath}/All-Dvds/0/latest/${page}`;
}

async function fetchClassicProfile(actorName, siteSlug) {
    const actorSlug = slugify(actorName);

    const url = `https://${siteSlug}.com/en/pornstars`;
    const { qa } = await get(url);

    const actorPath = qa('option[value*="/pornstar"]')
        .find(el => slugify(el.textContent) === actorSlug)
        ?.value;

    if (actorPath) {
        const actorUrl = `https://${siteSlug}.com${actorPath}`;
        const { html } = await get(actorUrl);

        const releases = scrapeAll(html, null, `https://www.${siteSlug}.com`, false);

        return { releases };
    }

    return null;
}

async function networkFetchProfile(actorName) {
    // not all Fame Digital sites offer Gamma actors
    const [devils, rocco, peter, silvia] = await Promise.all([
        fetchApiProfile(actorName, 'devilsfilm', true),
        fetchApiProfile(actorName, 'roccosiffredi'),
        argv.withReleases ? fetchProfile(actorName, 'peternorth', true, getActorReleasesUrl) : [],
        argv.withReleases ? fetchClassicProfile(actorName, 'silviasaint') : [],
        argv.withReleases ? fetchClassicProfile(actorName, 'silverstonedvd') : [],
    ]);

    if (devils || rocco || peter) {
        const releases = [].concat(devils?.releases || [], rocco?.releases || [], peter?.releases || [], silvia?.releases || []);

        return {
            ...peter,
            ...rocco,
            ...devils,
            releases,
        };
    }

    return null;
}

module.exports = {
    fetchLatest: networkFetchLatest,
    fetchProfile: networkFetchProfile,
    fetchScene: networkFetchScene,
    fetchUpcoming: networkFetchUpcoming,
};
