'use strict';

const bhttp = require('bhttp');
const { get, exa, fd } = require('../utils/q');

function scrapeLatest(html, site) {
    const scenes = exa(html, 'body > table');

    return scenes.map(({ q, qd, qi, qu, ql }) => {
        // if (q('.articleTitleText')) return scrapeFirstLatest(ctx(el), site);
        const release = {};

        const titleEl = q('.galleryTitleText, .articleTitleText');
        const [title, ...actors] = titleEl.textContent.split('|');
        const date = qd('.articlePostDateText', 'MMM D, YYYY');

        const url = qu(titleEl, 'a');
        [release.entryId] = url.split('/').slice(-2);
        release.url = `${site.url}${url}`;

        if (date) {
            release.title = title.trim();
            release.date = date;
        } else {
            //  title should contain date instead
            release.title = title.slice(title.indexOf(':') + 1).trim();
            release.date = fd(title.slice(0, title.indexOf(':')), 'MMM D, YYYY');
        }

        release.actors = actors.map(actor => actor.trim());

        const description = q('.articleCopyText .articleCopyText', true);
        if (description) release.description = description;

        const duration = ql('.articleCopyText a:nth-child(2)');
        if (duration) release.duration = duration;

        const poster = qi('a img');
        release.poster = [
            poster.replace('_thumbnail', ''),
            poster,
        ];

        return release;
    });
}

function scrapeScene({ q, qd, ql, qu, qis, qp, qt }, site) {
    const release = {};

    const titleEl = q('.articleTitleText');
    const [title, ...actors] = titleEl.textContent.split('|');

    const url = qu(titleEl, 'a');
    [release.entryId] = url.split('/').slice(-2);
    release.url = `${site.url}${url}`;

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

    release.actors = actors.map(actor => actor.trim());
    release.date = qd('.articlePostDateText', 'MMMM D, YYYY');
    release.duration = ql('.articlePostDateText a:nth-child(2)');

    const [cover, ...photos] = qis('img[src*="images"]');
    release.covers = [cover];
    release.photos = photos;

    release.poster = qp();

    const trailer = qt();
    release.trailer = { src: trailer };

    return release;
}

async function fetchLatest(site, page = 1) {
    const url = `${site.url}/scripts/switch_tour.php?page=${page}`;
    const res = await bhttp.get(url, {
        type: 'gallery',
        page,
    });

    if (res.statusCode === 200) {
        return scrapeLatest(res.body.html, site);
    }

    return null;
}

async function fetchScene(url, site) {
    const qScene = await get(url);

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

module.exports = {
    fetchLatest,
    fetchScene,
};
