'use strict';

const qu = require('../utils/qu');
const slugify = require('../utils/slugify');

function scrapeAll(scenes, site) {
	return scenes.map(({ query }) => {
		const url = query.url('.text-thumb a');
		const { pathname } = new URL(url);
		const channelUrl = query.url('.badge');

		if (site?.parameters?.extract && query.q('.badge', true) !== site.name) {
			return null;
		}

		const release = {};

		release.url = channelUrl ? `${channelUrl}${pathname}` : url;
		release.entryId = pathname.match(/\/trailers\/(.*).html/)[1];
		release.title = query.q('.text-thumb a', true);

		release.date = query.date('.date', 'YYYY-MM-DD', /\d{4}-\d{2}-\d{2}/);
		release.duration = query.dur('.date', /(\d{2}:)?\d{2}:\d{2}/);

		release.actors = query.all('.category a', true);

		release.poster = query.img('img.video_placeholder, .video-images img');
		release.teaser = { src: query.trailer() };

		return release;
	}).filter(Boolean);
}

function scrapeScene({ query, html }, url, _site, baseRelease) {
	const release = { url };

	const { pathname } = new URL(url);
	release.entryId = pathname.match(/\/\d+/)[0].slice(1);

	release.title = query.q('.trailer-block_title', true);
	release.description = query.q('.info-block:nth-child(3) .text', true);
	release.date = query.date('.info-block_data .text', 'MMMM D, YYYY', /\w+ \d{1,2}, \d{4}/);

	const duration = baseRelease?.duration || Number(query.q('.info-block_data .text', true).match(/(\d+)\s+min/)?.[1]) * 60;
	if (duration) release.duration = duration;

	release.actors = query.all('.info-block_data a[href*="/models"]', true);
	release.tags = query.all('.info-block a[href*="/categories"]', true);

	const posterEl = query.q('.update_thumb');
	const poster = posterEl?.getAttribute('src0_3x') || posterEl?.getAttribute('src0_2x') || posterEl?.dataset.src;

	if (poster && baseRelease?.poster) release.photos = [poster];
	else if (poster) release.poster = poster;

	const trailer = html.match(/video src="(.*?)"/);

	if (trailer) {
		release.trailer = trailer[1];
	}

	return release;
}

function scrapeProfile({ query }) {
	const profile = {};

	const keys = query.all('.model-descr_line:not(.model-descr_rait) p.text span', true);
	const values = query.all('.model-descr_line:not(.model-descr_rait) p.text').map(el => query.text(el));
	const bio = keys.reduce((acc, key, index) => ({ ...acc, [slugify(key, '_')]: values[index] }), {});

	if (bio.height) profile.height = Number(bio.height.match(/\((\d+)\s*cm\)/)?.[1]);
	if (bio.weight) profile.weight = Number(bio.weight.match(/\((\d+)kg\)/)?.[1]);
	if (bio.race) profile.ethnicity = bio.race;

	if (bio.date_of_birth) profile.birthdate = qu.extractDate(bio.date_of_birth, 'MMMM D, YYYY');
	if (bio.birthplace) profile.birthPlace = bio.birthplace;

	if (bio.measurements) {
		const [bust, waist, hip] = bio.measurements.split('-');
		if (!/\?/.test(bust)) profile.bust = bust;
		if (!/\?/.test(waist)) profile.waist = waist;
		if (!/\?/.test(hip)) profile.hip = hip;
	}

	if (bio.hair) profile.hair = bio.hair;
	if (bio.eyes) profile.eyes = bio.eyes;

	if (/various/i.test(bio.tattoos)) profile.hasTattoos = true;
	else if (/none/i.test(bio.tattoos)) profile.hasTattoos = false;
	else if (bio.tattoos) {
		profile.hasTattoos = true;
		profile.tattoos = bio.tattoos;
	}

	if (/various/i.test(bio.piercings)) profile.hasPiercings = true;
	else if (/none/i.test(bio.piercings)) profile.hasPiercings = false;
	else if (bio.piercings) {
		profile.hasPiercings = true;
		profile.piercings = bio.piercings;
	}

	if (bio.aliases) profile.aliases = bio.aliases.split(',').map(alias => alias.trim());

	const avatar = query.q('.model-img img');
	profile.avatar = avatar.getAttribute('src0_3x') || avatar.getAttribute('src0_2x') || avatar.dataset.src;

	const releases = query.all('.video-thumb');
	profile.releases = scrapeAll(qu.initAll(releases));

	return profile;
}

async function fetchLatest(site, page = 1) {
	const url = site.parameters?.extract
		? `https://cherrypimps.com/categories/movies_${page}.html`
		: `${site.url}/categories/movies_${page}.html`;
	const res = await qu.getAll(url, 'div.video-thumb');

	return res.ok ? scrapeAll(res.items, site) : res.status;
}

async function fetchScene(url, site, release) {
	const res = await qu.get(url);

	return res.ok ? scrapeScene(res.item, url, site, release) : res.status;
}

async function fetchProfile({ name: actorName }, { site, network, scraper }) {
	const actorSlug = slugify(actorName);
	const actorSlug2 = slugify(actorName, '');

	const origin = site?.url || network.url;

	const [url, url2] = ['cherrypimps', 'wildoncam'].includes(scraper)
		? [`${origin}/models/${actorSlug}.html`, `${origin}/models/${actorSlug2}.html`]
		: [`${origin}/models/${actorSlug}.html`, `${origin}/models/${actorSlug2}.html`];

	const res = await qu.get(url);
	if (res.ok) return scrapeProfile(res.item);

	const res2 = await qu.get(url2);
	return res2.ok ? scrapeProfile(res2.item) : res2.status;
}

module.exports = {
	fetchLatest,
	fetchScene,
	fetchProfile,
};