'use strict';

const util = require('util');

const knex = require('../knex');
const { get, geta, fd } = require('../utils/q');
const slugify = require('../utils/slugify');

function extractPoster(posterPath, site, baseRelease) {
    if (posterPath && !/400.jpg/.test(posterPath)) {
        const poster = `${site.parameters?.media || site.url}${posterPath}`;
        const posterSources = [
            poster,
            // upscaled
            poster.replace('-1x', '-2x'),
            poster.replace('-1x', '-3x'),
        ];

        if (baseRelease?.poster) {
            return [posterSources, [baseRelease.poster]];
        }

        return [posterSources, []];
    }

    return [baseRelease?.poster || null, []];
}

function scrapeLatest(scenes, site) {
    return scenes.map(({ q, qu, qd, ql }) => {
        const release = {};

        release.entryId = q('.modelimg img', 'id').match(/set-target-(\d+)/)[1];

        release.title = q('h3 a', 'title') || q('h3 a', true);
        release.url = qu('h3 a');

        release.date = qd('.modeldata p', 'YYYY-MM-DD', /\d{4}-\d{2}-\d{2}/);
        release.duration = ql('.modeldata p');

        if (/bts|behind the scenes/i.test(release.title)) release.tags = ['behind-the-scenes'];

        release.poster = [
            q('.modelimg img', 'src0_3x'),
            q('.modelimg img', 'src0_2x'),
            q('.modelimg img', 'src0_1x'),
        ].filter(Boolean).map(src => `${site.parameters?.media || site.url}${src}`);

        return release;
    });
}

function scrapeLatestT1(scenes, site) {
    return scenes.map(({ q, qi, qd, ql, qu }) => {
        const release = {};

        release.title = q('h4 a', 'title') || q('h4 a', true);
        release.url = qu('h4 a');

        release.date = qd('.more-info-div', 'MMM D, YYYY');
        release.duration = ql('.more-info-div');

        // release.entryId = q('.img-div img', 'id')?.match(/set-target-(\d+)/)[1];
        release.entryId = `${slugify(fd(release.date, 'YYYY-MM-DD'))}-${slugify(release.title)}`;

        const posterPath = q('.img-div img', 'src0_1x') || qi('img.video_placeholder');

        if (posterPath) {
            const poster = /^http/.test(posterPath) ? posterPath : `${site.parameters?.media || site.url}${posterPath}`;

            release.poster = [
                poster.replace('-1x', '-3x'),
                poster.replace('-1x', '-2x'),
                poster,
            ];
        }

        return release;
    });
}

function scrapeScene({ html, q, qa, qd, ql }, site, url, baseRelease) {
    const release = { url };

    release.entryId = html.match(/set-target-(\d+)/)[1];
    release.title = q('.centerwrap h2', true);
    release.description = q('.videocontent p', true);

    release.date = qd('.videodetails .date', 'MM/DD/YYYY');
    release.duration = ql('.videodetails .date');

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

    const posterPath = html.match(/poster="([\w-/.]+)"/)?.[1];
    [release.poster, release.photos] = extractPoster(posterPath, site, baseRelease);

    const trailerPath = html.match(/\/trailers\/.*.mp4/);
    if (trailerPath) release.trailer = { src: `${site.parameters?.media || site.url}${trailerPath}` };

    const stars = q('.modelrates + p', true).match(/\d.\d/)?.[0];
    if (stars) release.stars = Number(stars);

    return release;
}

function scrapeSceneT1({ html, q, qa, qd, ql, qtx }, site, url, baseRelease, channelRegExp) {
    const release = { url };

    release.title = q('.trailer-section-head .section-title', true);
    release.description = qtx('.row .update-info-block');

    release.date = qd('.update-info-row', 'MMM D, YYYY', /\w+ \d{1,2}, \d{4}/);
    release.duration = ql('.update-info-row:nth-child(2)');

    // release.entryId = q('.player-thumb img', 'id')?.match(/set-target-(\d+)/)[1];
    release.entryId = `${slugify(fd(release.date, 'YYYY-MM-DD'))}-${slugify(release.title)}`;

    release.actors = qa('.models-list-thumbs a').map(el => ({
        name: q(el, 'span', true),
        avatar: [
            q(el, 'img', 'src0_3x'),
            q(el, 'img', 'src0_2x'),
            q(el, 'img', 'src0_1x'),
        ].filter(Boolean).map(src => `${site.parameters?.media || site.url}${src}`),
    }));

    release.tags = qa('.tags a', true);

    // const posterPath = html.match(/poster="(.*\.jpg)/)?.[1];
    const posterPath = q('.player-thumb img', 'src0_1x');
    [release.poster, release.photos] = extractPoster(posterPath, site, baseRelease);

    const trailer = html.match(/<video.*src="(.*\.mp4)/)?.[1];
    if (trailer && /^http/.test(trailer)) release.trailer = { src: trailer, referer: url };
    else if (trailer) release.trailer = { src: `${site.parameters?.media || site.url}${trailer}`, referer: url };

    const stars = q('.update-rating', true).match(/\d.\d/)?.[0];
    if (stars) release.stars = Number(stars);

    if (channelRegExp) {
        const channel = release.tags.find(tag => channelRegExp.test(tag));

        if (channel) {
            release.channel = {
                force: true,
                slug: slugify(channel, { delimiter: '' }),
            };
        }
    }

    return release;
}

async function getChannelRegExp(site) {
    if (!['hushpass', 'interracialpass'].includes(site.network.slug)) return null;

    const sites = await knex('sites').where('network_id', site.network.id);

    return new RegExp(sites.map(channel => channel.parameters?.match || channel.name).join('|'), 'i');
}

async function fetchLatest(site, page = 1) {
    const url = (site.parameters?.latest && util.format(site.parameters.latest, page))
        || (site.parameters?.t1 && `${site.url}/t1/categories/movies_${page}_d.html`)
        || `${site.url}/categories/movies_${page}_d.html`;

    const qLatest = await geta(url, '.modelfeature, .item-video');

    if (!qLatest) return null;

    return site.parameters?.t1 ? scrapeLatestT1(qLatest, site) : scrapeLatest(qLatest, site);
}

async function fetchScene(url, site, baseRelease, beforeFetchLatest) {
    const channelRegExp = beforeFetchLatest || await getChannelRegExp(site);
    const qScene = await get(url);

    if (!qScene) return null;

    return site.parameters?.t1 ? scrapeSceneT1(qScene, site, url, baseRelease, channelRegExp) : scrapeScene(qScene, site, url, baseRelease);
}

module.exports = {
    beforeFetchLatest: getChannelRegExp,
    fetchLatest,
    fetchScene,
};
