'use strict';

const { geta, ed } = require('../utils/q');

function scrapeBlockLatest(scenes) {
    return scenes.map(({ html, q, qa, qu, qt }) => {
        const release = {};

        const entryId = q('div[class*="videothumb"]', 'class').match(/videothumb_(\d+)/)
            || q('div[id*="videothumb"]', 'id').match(/videothumb_(\d+)/);

        release.entryId = entryId[1];

        release.title = q('h4 a', true);
        release.url = qu('h4 a');
        release.date = ed(html, 'MM/DD/YYYY', /\d{2}\/\d{2}\/\d{4}/);

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

        release.poster = q('div img').dataset.src;
        release.photos = [q('div img', 'src0_4x') || q('div img', 'src0_3x') || q('div img', 'src0_2x')];

        release.teaser = qt();

        return release;
    });
}

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

        release.entryId = el.dataset.setid;
        release.url = qu('a');

        release.title = q('.update_title_small', true) || q('a:nth-child(2)', true);

        const description = q('a', 'title');
        if (description) release.description = description;

        const date = qd('.date_small, .update_date', 'MM/DD/YYYY');
        if (date) release.date = date;

        const durationLine = q('.update_counts', true);
        if (durationLine) release.duration = Number(durationLine.match(/(\d+) min/i)[1]) * 60;

        const actors = qa('.update_models a', true);
        release.actors = actors.length > 0 ? actors : q('.update_models', true).split(/,\s*/);

        const photoCount = q('.update_thumb', 'cnt');
        [release.poster, ...release.photos] = Array.from({ length: photoCount }).map((value, index) => q('.update_thumb', `src${index}_3x`) || q('.update_thumb', `src${index}_2x`) || q('.update_thumb', `src${index}_1x`));

        return release;
    });
}

async function fetchLatest(site, page = 1) {
    if (!site.parameters) {
        return null;
    }

    const url = `${site.url}/tour_${site.parameters.siteId}/categories/movies_${page}_d.html`;
    const qLatest = await geta(url, '.updatesBlock .movieBlock, .updatesBlock .videoBlock, .latest_updates_block .update_details, .category_listing_block .update_details');

    if (qLatest && site.parameters.block) {
        return scrapeBlockLatest(qLatest, site);
    }

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

module.exports = {
    fetchLatest,
};
