'use strict';

const { get, geta, ctxa } = require('../utils/q');
const slugify = require('../utils/slugify');

function scrapeAll(scenes) {
    return scenes.map(({ el, q, qa, qd, qu, ql }) => {
        const release = {};

        release.entryId = el.dataset.setid || q('.update_thumb', 'id').match(/\w+-\w+-(\d+)-\d+/)[1];
        release.url = qu('.title');

        release.title = q('.title', true);
        release.description = q('.title', 'title');

        release.date = qd('.video-data > span:last-child', 'YYYY-MM-DD');
        release.duration = ql('.video-data > span');

        release.actors = qa('.update_models a', true);

        const poster = q('.update_thumb', 'src0_1x');
        release.poster = [
            poster.replace('-1x', '-2x'),
            poster,
        ];

        return release;
    });
}

function scrapeScene({ q, qa, qd, qtx }, url, _site) {
    const release = { url };

    release.entryId = q('#image_parent img', 'id').match(/\w+-\w+-(\d+)-\d+/)[1];

    release.title = q('.trailer_title', true);
    release.description = qtx('.text p');
    release.date = qd('span[data-dateadded]', 'YYYY-MM-DD', null, 'data-dateadded');

    release.actors = qa('.update_models a', true);
    release.tags = qa('.video-info a[href*="/categories"]', true);

    const poster = q('#image_parent img', 'src0_1x');
    release.poster = [
        poster.replace('-1x', '-2x'),
        poster,
    ];

    return release;
}

function scrapeProfile({ el, q, qtx }) {
    const profile = {};

    const description = qtx('.model-bio');
    if (description) profile.description = description;

    profile.avatar = [
        q('.model-image img', 'src0_2x'),
        q('.model-image img', 'src0_1x'),
    ];

    profile.releases = scrapeAll(ctxa(el, '.update'));

    return profile;
}

async function fetchLatest(site, page = 1) {
    const url = `${site.url}/categories/movies_${page}_d.html`;
    const qLatest = await geta(url, '.latest-updates .update');

    return qLatest && scrapeAll(qLatest, site);
}

async function fetchScene(url, site) {
    const qScene = await get(url, '.content-wrapper');

    return qScene && scrapeScene(qScene, url, site);
}

async function fetchProfile(actorName, scraperSlug) {
    const actorSlug = slugify(actorName, { delimiter: '' });
    const url = scraperSlug === 'povperverts'
        ? `https://povperverts.net/models/${actorSlug}.html`
        : `https://${scraperSlug}.com/models/${actorSlug}.html`;

    const qProfile = await get(url);

    return qProfile && scrapeProfile(qProfile, actorName);
}

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