'use strict';

const bhttp = require('bhttp');

const { ed, ex, exa, get } = require('../utils/q');
const slugify = require('../utils/slugify');

/* eslint-disable newline-per-chained-call */
function scrapeAll(html, site, origin) {
    return exa(html, '.card.m-1:not(.pornstar-card)').map(({ q, qa, qd }) => {
        const release = {};

        release.title = q('a', 'title');
        release.url = `${site?.url || origin || 'https://ddfnetwork.com'}${q('a', 'href')}`;
        [release.entryId] = release.url.split('/').slice(-1);

        release.date = qd('small[datetime]', 'YYYY-MM-DD HH:mm:ss', null, 'datetime');
        release.actors = qa('.card-subtitle a', true).filter(Boolean);

        const duration = parseInt(q('.card-info div:nth-child(2) .card-text', true), 10) * 60;
        if (duration) release.duration = duration;

        release.poster = q('img').dataset.src;

        return release;
    });
}

async function scrapeScene(html, url, _site) {
    const { q, qa, qd, qm, qp, qus } = ex(html);
    const release = {};

    [release.entryId] = url.split('/').slice(-1);

    release.title = qm('itemprop=name');
    release.description = q('.descr-box p', true);
    release.date = qd('meta[itemprop=uploadDate]', 'YYYY-MM-DD', null, 'content')
        || qd('.title-border:nth-child(2) p', 'MM.DD.YYYY');

    release.actors = qa('.pornstar-card > a', 'title');
    release.tags = qa('.tags-tab .tags a', true);

    release.duration = parseInt(q('.icon-video-red + span', true), 10) * 60;
    release.likes = Number(q('.icon-like-red + span', true));

    release.poster = qp();
    release.photos = qus('.photo-slider-guest .card a');

    release.trailer = qa('source[type="video/mp4"]').map(trailer => ({
        src: trailer.src,
        quality: Number(trailer.attributes.res.value),
    }));

    return release;
}

async function fetchActorReleases(urls) {
    // DDF Network and DDF Network Stream list all scenes, exclude
    const sources = urls.filter(url => !/ddfnetwork/.test(url));

    const releases = await Promise.all(sources.map(async (url) => {
        const { html } = await get(url);

        return scrapeAll(html, null, new URL(url).origin);
    }));

    // DDF cross-releases scenes between sites, filter duplicates by entryId
    return Object.values(releases
        .flat()
        .sort((releaseA, releaseB) => releaseB.date - releaseA.date) // sort by date so earliest scene remains
        .reduce((acc, release) => ({ ...acc, [release.entryId]: release }), {}));
}

async function scrapeProfile(html, _url, actorName) {
    const { q, qa, qus } = ex(html);

    const keys = qa('.about-title', true).map(key => slugify(key, { delimiter: '_' }));
    const values = qa('.about-info').map((el) => {
        if (el.children.length > 0) {
            return Array.from(el.children, child => child.textContent.trim()).join(', ');
        }

        return el.textContent.trim();
    });

    const bio = keys.reduce((acc, key, index) => {
        if (values[index] === '-') return acc;

        return {
            ...acc,
            [key]: values[index],
        };
    }, {});

    const profile = {
        name: actorName,
    };

    profile.description = q('.description-box', true);
    profile.birthdate = ed(bio.birthday, 'MMMM DD, YYYY');

    if (bio.nationality) profile.nationality = bio.nationality;

    if (bio.bra_size) [profile.bust] = bio.bra_size.match(/\d+\w+/);
    if (bio.waist) profile.waist = Number(bio.waist.match(/\d+/)[0]);
    if (bio.hips) profile.hip = Number(bio.hips.match(/\d+/)[0]);

    if (bio.height) profile.height = Number(bio.height.match(/\d{2,}/)[0]);

    if (bio.tit_style && /Enhanced/.test(bio.tit_style)) profile.naturalBoobs = false;
    if (bio.tit_style && /Natural/.test(bio.tit_style)) profile.naturalBoobs = true;

    if (bio.body_art && /Tattoo/.test(bio.body_art)) profile.hasTattoos = true;
    if (bio.body_art && /Piercing/.test(bio.body_art)) profile.hasPiercings = true;

    if (bio.hair_style) profile.hair = bio.hair_style.split(',')[0].trim().toLowerCase();
    if (bio.eye_color) profile.eyes = bio.eye_color.match(/\w+/)[0].toLowerCase();

    if (bio.shoe_size) profile.shoes = Number(bio.shoe_size.split('|')[1]);

    const avatarEl = q('.pornstar-details .card-img-top');
    if (avatarEl && avatarEl.dataset.src.match('^//')) profile.avatar = `https:${avatarEl.dataset.src}`;

    profile.releases = await fetchActorReleases(qus('.find-me-tab li a'));

    return profile;
}

async function fetchLatest(site, page = 1) {
    const url = site.parameters?.native
        ? `${site.url}/videos/search/latest/ever/allsite/-/${page}`
        : `https://ddfnetwork.com/videos/search/latest/ever/${new URL(site.url).hostname}/-/${page}`;

    const res = await bhttp.get(url);

    if (res.statusCode === 200) {
        return scrapeAll(res.body.toString(), site);
    }

    return res.statusCode;
}

async function fetchScene(url, site) {
    const res = await bhttp.get(`https://ddfnetwork.com${new URL(url).pathname}`);

    return scrapeScene(res.body.toString(), url, site);
}

async function fetchProfile(actorName) {
    const resSearch = await bhttp.post('https://ddfnetwork.com/search/ajax',
        {
            type: 'hints',
            word: actorName,
        },
        {
            decodeJSON: true,
            headers: {
                'x-requested-with': 'XMLHttpRequest',
            },
        });

    if (resSearch.statusCode !== 200 || Array.isArray(resSearch.body.list)) {
        return null;
    }

    if (!resSearch.body.list.pornstarsName || resSearch.body.list.pornstarsName.length === 0) {
        return null;
    }

    const [actor] = resSearch.body.list.pornstarsName;
    const url = `https://ddfnetwork.com${actor.href}`;

    const resActor = await bhttp.get(url);

    if (resActor.statusCode !== 200) {
        return null;
    }

    return scrapeProfile(resActor.body.toString(), url, actorName);
}

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