'use strict';

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

function scrapeLatest(html, site) {
    const scenes = site.slug === 'paintoy'
        ? exa(html, '#articleTable table[cellspacing="2"]')
        : 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 td', '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, not applicable in brief mode
            release.title = title.slice(title.indexOf(':') + 1).trim();
            release.date = ed(title.slice(0, title.indexOf(':')), 'MMM D, YYYY');
        }

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

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

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

        release.likes = parseInt(q('.articlePostDateText td:nth-child(3)', true), 10);

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

        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.slug === 'paintoy' // paintoy's site is partially broken, use front page
        ? `${site.url}/corporal/punishment/gallery.php?type=brief&page=${page}`
        : `${site.url}/scripts/switch_tour.php?type=brief&page=${page}`;

    const res = await bhttp.get(url, {
        type: 'brief',
        page,
    });

    if (res.statusCode === 200) {
        return scrapeLatest(site.slug === 'paintoy' ? res.body.toString() : 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,
};
