console.log('bundle2!')

printable = (platform) => {
    return platform === "WEB";
}

request = async (method, url, data = null, headers = {}, platform) => {
    if (platform === "WEB") {
        url = url.replace("https://www.xvideos.com/", "http://127.0.0.1:82/");
    }
    console.log(`request url:${url}`)
    console.log(`request data:${data}`)
    console.log(`request method:${method}`)
    console.log(`request headers:${JSON.stringify((headers))}`)
    if (platform === "WEB") {
        const res = await fetch(url, {
            'mode': 'cors',
            'method': method,
            'headers': headers,
            'body': data
        })
        const resData = await res.text()
        return Promise.resolve({
            'data': resData,
            'headers': res.headers
        });
    }
    return new Promise((resolve, reject) => {
        AF.request(url, method, data, headers, (data, headers, err) => {
            if (err) {
                reject(err);
            } else {
                console.log(`response headers: ${headers}`);
                resolve({
                    'data': data,
                    'headers': JSON.parse(headers)
                });
            }
        });
    })
}


detail = async (url, platform) => {
    try {
        const htmlResp = await request('GET', url, null, {}, platform);
        let {data: html, headers: htmlHeaders} = htmlResp;

        const formats = [];
        formats.push({
            "url": html.match(/html5player.setVideoUrlLow\('(.*)'\);/)[1],
            "quality": "240"
        })
        formats.push({
            "url": html.match(/html5player.setVideoUrlHigh\('(.*)'\);/)[1],
            "quality": "1080"
        })

        const videoDetails = {
            "title": html.match(/html5player.setVideoTitle\('(.*)'\);/)[1],
            "thumbnail": html.match(/html5player.setThumbUrl\('(.*)'\);/)[1],
            "videoId": html.match(/html5player.setEncodedIdVideo\('(.*)'\);/)[1],
        }
        const ret = {
            "code": 200,
            "msg": "",
            "data": {
                "videoDetails": videoDetails,
                "streamingData": {
                    "formats": formats
                }
            },
            "id": "DetailViewModel_detail_url"
        }
        console.log(`detail result: ${JSON.stringify(ret)}`);
        return ret;
    } catch (e) {
        const ret = {
            "code": -1,
            "msg": e.toString()
        }
        console.log(`detail result error: ${JSON.stringify(ret)}`);
        console.log(e);
        return ret;
    }
}
