'use strict';

const bhttp = require('bhttp');
const { JSDOM } = require('jsdom');
const cheerio = require('cheerio');
const moment = require('moment');

function extractTitle(originalTitle) {
    const titleComponents = originalTitle.split(' ');
    const sceneIdMatch = titleComponents.slice(-1)[0].match(/(AB|AF|GP|SZ|IV|GIO|RS|TW|MA|FM|SAL|NR|AA|GL|BZ|FS)\d+/); // detect studio prefixes
    const shootId = sceneIdMatch ? sceneIdMatch[0] : null;
    const title = sceneIdMatch ? titleComponents.slice(0, -1).join(' ') : originalTitle;

    return { shootId, title };
}

function getPoster(posterElement, sceneId) {
    const posterStyle = posterElement.attr('style');

    if (posterStyle) {
        return posterStyle.slice(posterStyle.indexOf('(') + 1, -1);
    }

    const posterRange = posterElement.attr('data-casting');
    const posterRangeData = posterRange ? JSON.parse(posterRange) : null;
    const posterTimeRange = posterRangeData[Math.floor(Math.random() * posterRangeData.length)];

    if (!posterTimeRange) {
        return null;
    }

    if (typeof posterTimeRange === 'number') {
        // poster time is already a single time value
        return `https://legalporno.com/casting/${sceneId}/${posterTimeRange}`;
    }

    const [max, min] = posterTimeRange.split('-');
    const posterTime = Math.floor(Math.random() * (Number(max) - Number(min) + 1) + Number(min));

    return `https://legalporno.com/casting/${sceneId}/${posterTime}`;
}

function scrapeLatest(html, site) {
    const $ = cheerio.load(html, { normalizeWhitespace: true });
    const scenesElements = $('.thumbnails > div').toArray();

    return scenesElements.map((element) => {
        const sceneLinkElement = $(element).find('.thumbnail-title a');
        const url = sceneLinkElement.attr('href');

        const originalTitle = sceneLinkElement.text().trim(); // title attribute breaks when they use \\ escaping
        const { shootId, title } = extractTitle(originalTitle);
        const entryId = new URL(url).pathname.split('/')[2];

        const date = moment.utc($(element).attr('release'), 'YYYY/MM/DD').toDate();

        const sceneId = $(element).attr('data-content');
        const posterElement = $(element).find('.thumbnail-avatar');

        const poster = getPoster(posterElement, sceneId);

        return {
            url,
            shootId,
            entryId,
            title,
            date,
            poster,
            site,
        };
    });
}

async function scrapeScene(html, url, site, useGallery) {
    const $ = cheerio.load(html, { normalizeWhitespace: true });
    const playerObject = $('script:contains("new VideoPlayer")').html();
    const data = JSON.parse(playerObject.slice(playerObject.indexOf('{"swf":'), playerObject.indexOf('} );') + 1));

    const originalTitle = $('h1.watchpage-title').text().trim();
    const { shootId, title } = extractTitle(originalTitle);
    const entryId = new URL(url).pathname.split('/')[2];

    const date = moment.utc($('span[title="Release date"] a').text(), 'YYYY-MM-DD').toDate();

    const [actorsElement, tagsElement, descriptionElement] = $('.scene-description__row').toArray();
    const actors = $(actorsElement)
        .find('a[href*="com/model"]')
        .map((actorIndex, actorElement) => $(actorElement).text()).toArray();

    const description = $('meta[name="description"]')?.attr('content')?.trim() || (descriptionElement && $(descriptionElement).find('dd').text().trim());
    const duration = moment.duration($('span[title="Runtime"]').text().trim()).asSeconds();

    const posterStyle = $('#player').attr('style');
    const poster = posterStyle.slice(posterStyle.indexOf('(') + 1, -1);

    const photos = useGallery
        ? $('.gallery a img').map((photoIndex, photoElement) => $(photoElement).attr('src')).toArray()
        : $('.screenshots img').map((photoIndex, photoElement) => $(photoElement).attr('src')).toArray();

    const trailer = data.clip.qualities.find(clip => clip.quality === 'vga' || clip.quality === 'hd');

    const studioName = $('.watchpage-studioname').first().text().trim();
    const studio = studioName.replace(/[\s.']+/g, '').toLowerCase();
    const tags = $(tagsElement).find('a').map((tagIndex, tagElement) => $(tagElement).text()).toArray();

    return {
        url,
        shootId,
        entryId,
        title,
        description,
        date,
        actors,
        duration,
        poster,
        photos,
        trailer: {
            src: trailer.src,
            type: trailer.type,
            quality: trailer.quality === 'vga' ? 480 : 720,
        },
        tags,
        site,
        studio,
    };
}

async function scrapeProfile(html, _url, actorName) {
    const { document } = new JSDOM(html).window;

    const profile = {
        name: actorName,
    };

    const avatarEl = document.querySelector('.model--avatar img[src^="http"]');
    const entries = Array.from(document.querySelectorAll('.model--description tr'), el => el.textContent.replace(/\n/g, '').split(':'));

    const bio = entries
        .filter(entry => entry.length === 2) // ignore entries without ':' (About section, see Blanche Bradburry)
        .reduce((acc, [key, value]) => ({ ...acc, [key.trim()]: value.trim() }), {});

    profile.birthPlace = bio.Nationality;

    if (bio.Age) profile.age = bio.Age;
    if (avatarEl) profile.avatar = avatarEl.src;

    return profile;
}

async function fetchLatest(site, page = 1) {
    const res = await bhttp.get(`${site.url}/new-videos/${page}`);

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

async function fetchScene(url, site) {
    const useGallery = true;

    const res = useGallery
        ? await bhttp.get(`${url}/gallery#gallery`)
        : await bhttp.get(`${url}/screenshots#screenshots`);

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

async function fetchProfile(actorName) {
    const res = await bhttp.get(`https://www.legalporno.com/api/autocomplete/search?q=${actorName.replace(' ', '+')}`);
    const data = res.body;

    const result = data.terms.find(item => item.type === 'model');

    if (result) {
        const bioRes = await bhttp.get(result.url);
        const html = bioRes.body.toString();

        return scrapeProfile(html, result.url, actorName);
    }

    return null;
}

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