'use strict';

const {
	fetchLatest,
	fetchApiLatest,
	fetchUpcoming,
	fetchApiUpcoming,
	fetchScene,
	fetchProfile,
	fetchApiProfile,
	scrapeAll,
} = require('./gamma');
const { get } = require('../utils/qu');
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}`;
}

function scrapeClassicProfile({ qu, html }, site) {
	const profile = {};

	profile.avatar = qu.img('.actorPicture');
	profile.releases = scrapeAll(html, null, site.url, false);

	return profile;
}

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

	const url = `${site.url}/en/pornstars`;
	const pornstarsRes = await get(url);

	if (!pornstarsRes.ok) return null;

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

	if (actorPath) {
		const actorUrl = `${site.url}${actorPath}`;
		const res = await get(actorUrl);

		if (res.ok) {
			return scrapeClassicProfile(res.item, site);
		}
	}

	return null;
}

async function networkFetchProfile(actorName, context, include) {
	const profile = await ((context.site.parameters?.api && fetchApiProfile(actorName, context, include))
		|| (context.site.parameters?.classic && include.scenes && fetchClassicProfile(actorName, context, include)) // classic profiles only have scenes, no bio
		|| fetchProfile(actorName, context, true, getActorReleasesUrl, include));

	return profile;
}

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