'use strict';

const bhttp = require('bhttp');
const { ex, ctxa } = require('../utils/q');
// const slugify = require('../utils/slugify');

function getLicenseCode(html) {
    const licensePrefix = 'license_code: \'';
    const licenseStart = html.indexOf(licensePrefix);
    const licenseCode = html.slice(licenseStart + licensePrefix.length, html.indexOf('\'', licenseStart + licensePrefix.length));

    const c = '16px';
    let f;
    let g;
    let h;
    let i;
    let j;
    let k;
    let l;
    let m;
    let n;

    for (f = '', g = 1; g < licenseCode.length; g += 1) {
        f += parseInt(licenseCode[g], 10) ? parseInt(licenseCode[g], 10) : 1;
    }

    for (j = parseInt(f.length / 2, 10),
        k = parseInt(f.substring(0, j + 1), 10),
        l = parseInt(f.substring(j), 10),
        g = l - k,
        g < 0 && (g = -g),
        f = g,
        g = k - l,
        g < 0 && (g = -g),
        f += g,
        f *= 2,
        f = String(f),
        i = (parseInt(c, 10) / 2) + 2,
        m = '',
        g = 0; g < j + 1; g += 1) {
            for (h = 1; h <= 4; h += 1) {
                n = parseInt(licenseCode[g + h], 10) + parseInt(f[g], 10);

                if (n >= i) n -= i;
                m += n;
            }
    }

    return m;
}

function decodeTrailerUrl(html, encodedTrailerUrl) {
    const licenseCode = getLicenseCode(html);
    const i = licenseCode;

    let j;
    let k;
    let l;
    let m;
    let n;
    let o;

    const d = '16px';
    const g = encodedTrailerUrl.split('/').slice(2);

    let h = g[5].substring(0, 2 * parseInt(d, 10));

    for (j = h, k = h.length - 1; k >= 0; k -= 1) {
        for (l = k, m = k; m < i.length; m += 1) {
            l += parseInt(i[m], 10);
        }

        for (; l >= h.length;) {
            l -= h.length;
        }

        for (n = '', o = 0; o < h.length; o += 1) {
            if (o === k) {
                n += h[l];
            } else {
                n += (o === l ? h[k] : h[o]);
            }
        }

        h = n;
    }

    g[5] = g[5].replace(j, h);
    const trailer = g.join('/');

    return trailer;
}

function scrapeLatest(html) {
    const { document } = ex(html);

    return ctxa(document, '.video-post').map(({ q, qa, qd }) => {
        const release = {};

        // release.entryId = slugify(release.title);
        release.entryId = q('.ico-fav-0').dataset.favVideoId;

        const titleEl = q('.video-title-title');
        release.title = titleEl.title;
        release.url = titleEl.href;

        release.date = qd('.video-data em', 'MMM DD, YYYY');
        release.actors = qa('.video-model-list a', true);

        const posterData = q('img.thumb').dataset;
        release.poster = posterData.src;
        release.trailer = posterData.preview;

        return release;
    });
}

function scrapeScene(html, url) {
    const { q, qa, qd, qus, ql, qm } = ex(html);
    const release = { url };

    // release.entryId = slugify(release.title);
    [release.entryId] = q('link[rel="canonical"]').href.match(/\d+/);

    release.title = qm('meta[property="og:title"]') || q('.video-page-header h1', true);

    release.description = qm('meta[property="og:description"]') || q('.info-video-description', true);
    release.date = qd('.info-video-details li:first-child span', 'MMM DD, YYYY');
    release.duration = ql('.info-video-details li:nth-child(2) span');

    release.actors = qa('.info-video-models a', true);
    release.tags = qa('.info-video-category a', true);

    release.photos = qus('.swiper-wrapper .swiper-slide a').map(source => source.replace('.jpg/', '.jpg'));
    release.poster = qm('meta[property="og:image"');

    if (!release.poster) {
        const previewStart = html.indexOf('preview_url');
        release.poster = html.slice(html.indexOf('http', previewStart), html.indexOf('.jpg', previewStart) + 4);
    }

    const varsPrefix = 'flashvars = {';
    const varsStart = html.indexOf(varsPrefix);
    const varsString = html.slice(varsStart + varsPrefix.length, html.indexOf('};', varsStart));

    const vars = varsString.split(',').reduce((acc, item) => {
        const [prop, value] = item.split(': ');
        acc[prop.trim()] = value.trim().replace(/'/g, '');

        return acc;
    }, {});

    release.trailer = [
        {
            src: decodeTrailerUrl(html, vars.video_url),
            quality: parseInt(vars.video_url_text, 10),
        },
        {
            src: decodeTrailerUrl(html, vars.video_alt_url),
            quality: parseInt(vars.video_alt_url_text, 10),
        },
        {
            src: decodeTrailerUrl(html, vars.video_alt_url2),
            quality: parseInt(vars.video_alt_url2_text, 10),
        },
        {
            src: decodeTrailerUrl(html, vars.video_alt_url3),
            quality: parseInt(vars.video_alt_url3_text, 10),
        },
        {
            src: decodeTrailerUrl(html, vars.video_alt_url4),
            quality: parseInt(vars.video_alt_url4_text, 10),
        },
    ];

    return release;
}

async function fetchLatest(site, page = 1) {
    const url = `https://vogov.com/latest-videos/?sort_by=post_date&from=${page}`;
    const res = await bhttp.get(url);

    if (res.statusCode === 200) {
        return scrapeLatest(res.body.toString(), site);
    }

    return null;
}

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

    if (res.statusCode === 200) {
        return scrapeScene(res.body.toString(), url);
    }

    return null;
}

module.exports = {
    fetchLatest,
    fetchScene,
};
