'use strict';

/* eslint-disable newline-per-chained-call */
const Promise = require('bluebird');
const bhttp = require('bhttp');
const cheerio = require('cheerio');
const moment = require('moment');

async function fetchPhotos(url) {
    const res = await bhttp.get(url);

    return res.body.toString();
}

function scrapePhotos(html) {
    const $ = cheerio.load(html, { normalizeWhitespace: true });

    return $('.preview .imgLink').toArray().map((linkEl) => {
        const url = $(linkEl).attr('href');

        if (url.match('/join')) {
            // URL links to join page instead of full photo, extract thumbnail
            const src = $(linkEl).find('img').attr('src');

            if (src.match('previews/')) {
                // resource often serves full photo at a modifier URL anyway, add as primary source
                const highRes = src
                    .replace('previews/', '')
                    .replace('_tb.jpg', '.jpg');

                // keep original thumbnail as fallback in case full photo is not available
                return [highRes, src];
            }

            return src;
        }

        // URL links to full photo
        return url;
    });
}

async function getPhotos(albumPath, siteDomain) {
    const albumUrl = `https://www.blowpass.com${albumPath}`;

    try {
        const html = await fetchPhotos(albumUrl);
        const $ = cheerio.load(html, { normalizeWhitespace: true });
        const photos = scrapePhotos(html);

        const pages = $('.paginatorPages a').map((pageIndex, pageElement) => $(pageElement).attr('href')).toArray();

        const otherPhotos = await Promise.map(pages, async (page) => {
            const pageUrl = `https://${siteDomain}${page}`;
            const pageHtml = await fetchPhotos(pageUrl);

            return scrapePhotos(pageHtml);
        }, {
            concurrency: 2,
        });

        return photos.concat(otherPhotos.flat());
    } catch (error) {
        console.error(`Failed to fetch Blowpass photos from ${albumPath}: ${error.message}`);

        return [];
    }
}

function scrape(html, site) {
    const $ = cheerio.load(html, { normalizeWhitespace: true });
    const sceneElements = $('.sceneList .scene').toArray();

    return sceneElements.map((element) => {
        const entryId = $(element).attr('data-itemid');

        const sceneLinkElement = $(element).find('.sceneTitle a');
        const title = sceneLinkElement.attr('title');
        const url = `${site.url}/en/scene/${sceneLinkElement.attr('href').split('/').slice(-2).join('/')}`;

        const date = moment.utc($(element).find('.sceneDate').text(), 'MM-DD-YYYY').toDate();
        const actors = $(element).find('.sceneActors a').map((actorIndex, actorElement) => $(actorElement).text()).toArray();

        const poster = $(element).find('a.imgLink img.img').attr('data-original');
        const trailer = `https://videothumb.gammacdn.com/600x339/${entryId}.mp4`;

        const likes = Number($(element).find('.rating .state_1 .value').text());

        return {
            url,
            entryId,
            title,
            actors,
            date,
            poster,
            trailer: {
                src: trailer,
                quality: 339,
            },
            rating: {
                likes,
            },
            site,
        };
    });
}

async function scrapeScene(html, url, site) {
    const $ = cheerio.load(html, { normalizeWhitespace: true });
    const json = $('script[type="application/ld+json"]').html();
    const data = JSON.parse(json).slice(-1)[0];
    const sceneElement = $('#wrapper');

    const videoScript = $('script:contains("window.ScenePlayerOptions")').html();
    const playerObject = videoScript.slice(videoScript.indexOf('{'), videoScript.indexOf('};') + 1);
    const playerData = JSON.parse(playerObject);

    const workName = data.isPartOf.name.split(' - ');
    const shootId = workName.length > 1 ? workName[1] : null;
    const entryId = url.split('/').slice(-1)[0];
    const title = data.title || $('meta[name="twitter:title"]').attr('content');
    const description = data.description || $('meta[name="twitter:description"]').attr('content');
    // date in data object is not the release date of the scene, but the date the entry was added
    const date = moment.utc($('.updatedDate').first().text(), 'MM-DD-YYYY').toDate();

    const actors = data.actor.map(({ name }) => name);

    const likes = Number(sceneElement.find('.rating .state_1 .value').text());
    const dislikes = Number(sceneElement.find('.rating .state_2 .value').text());

    const channel = $('.siteNameSpan').text().trim().toLowerCase();

    const poster = playerData.picPreview;
    const trailer = `${playerData.playerOptions.host}${playerData.url}`;
    const photos = await getPhotos($('.picturesItem a').attr('href'), channel, site);

    const duration = moment.duration(data.duration.slice(2)).asSeconds();
    const tags = data.keywords.split(', ');

    return {
        url,
        shootId,
        entryId,
        title,
        description,
        actors,
        date,
        duration,
        poster,
        photos,
        trailer: {
            src: trailer,
            quality: playerData.sizeOnLoad.slice(0, -1),
        },
        tags,
        rating: {
            likes,
            dislikes,
        },
        site,
        channel,
    };
}

async function fetchLatest(site, page = 1) {
    const res = await bhttp.get(`https://www.blowpass.com/en/videos/${site.slug}/latest/All-Categories/0/All-Pornstars/0/${page}`);

    return scrape(res.body.toString(), site);
}

async function fetchUpcoming(site) {
    const res = await bhttp.get(`https://www.blowpass.com/en/videos/${site.slug}/upcoming`);

    return scrape(res.body.toString(), site);
}

async function fetchScene(url, site) {
    const res = await bhttp.get(`https://www.blowpass.com/en/video/${site.id}/${new URL(url).pathname.split('/').slice(-2).join('/')}`);

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

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