const {getHtml} = $.require('./_lib.request.js')
var rule = {
    title: '哔哩直播[官]',
    desc: '搜索需要配置cookie，并且最好是从网页上复制的cookie，扫码入库的好像没用',
    host: 'https://api.live.bilibili.com',
    homeUrl: '/xlive/web-interface/v1/second/getUserRecommend?page=1&page_size=30&platform=web',//用于"分类获取"和"推荐获取"
    url: '/xlive/web-interface/v1/second/getList?platform=web&parent_area_id=fyclass&area_id=fyfilter&sort_type=online&page=fypage',
    class_name: '生活&网游&手游&单机游戏&娱乐&电台&虚拟主播&聊天室&知识&赛事&帮我玩&互动玩法&购物',
    class_url: '10&2&3&6&1&5&9&14&11&13&301&15&300',
    filterable: 1,
    filter_url: '{{fl.area}}',
    filter_def: {
        10: {area: '624'},
        2: {area: '86'},
        3: {area: '35'},
        6: {area: '236'},
        1: {area: '21'},
        5: {area: '190'},
        9: {area: '744'},
        14: {area: '818'},
        11: {area: '376'},
        13: {area: '562'},
        301: {area: '301000'},
        15: {area: ''},
        300: {area: '300000'}
    },
    filter: '[encoded data, 5472 characters]',
    detailUrl: 'https://live.bilibili.com/fyid',
    searchUrl: 'https://api.bilibili.com/x/web-interface/search/type?search_type=live&keyword=**&page=fypage',
    searchable: 2,
    quickSearch: 0,
    headers: {
        "User-Agent": PC_UA,
        "Referer": "https://www.bilibili.com",
    },
    timeout: 5000,
    limit: 8,
    play_parse: true,
    lazy: async function () {
        let {input} = this;
        let ids = input.split('_');
        let dan = 'https://api.bilibili.com/x/v1/dm/list.so?oid=' + ids[1];
        let result = {};
        let iurl = 'https://api.live.bilibili.com/room/v1/Room/playUrl?cid=' + ids[1] + '&' + ids[0];
        let jRoot = (await getHtml(iurl)).data;
        let jo = jRoot['data'];
        let ja = jo['durl'];
        let purl = '';
        if (ja.length > 0) {
            purl = ja[0]['url']
        }
        result['parse'] = 0;
        result['playUrl'] = '';
        result['url'] = unescape(purl);
        result['header'] = {
            Referer: 'https://live.bilibili.com',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36'
        };
        result['danmaku'] = dan;
        if (/h5/.test(ids[0])) {
            result['contentType'] = '';
            input = result
        } else {
            result['contentType'] = 'video/x-flv';
            input = result
        }
        return result
    },
    double: false,
    一级: async function () {
        let {input} = this;
        var d = [];
        let html = (await getHtml(input)).data;
        html = html.data.list;
        html.forEach(it => {
            let pic = it.system_cover;
            if (pic.startsWith('/')) {
                pic = urljoin2(input, pic)
            }
            d.push({
                title: it.title,
                img: pic,
                desc: '👁' + it.watched_show.text_small + '  🆙' + it.uname,
                url: it.roomid
            })
        });
        return setResult(d);
    },
    二级: async function (ids) {
        let {input} = this;
        let aid = input.match(/\d+/g)[0];
        let html = (await getHtml('https://api.live.bilibili.com/room/v1/Room/get_info?room_id=' + aid)).data;
        let jo = html.data;
        let title = jo['title'];
        let pic = jo['keyframe'];
        let desc = jo['description'];
        let dire = jo['uid'];
        let typeName = jo['area_name'];
        let remark = '在线人数:' + jo['online'];
        let vod = {
            vod_id: aid,
            vod_name: title,
            vod_pic: pic,
            type_name: typeName,
            vod_area: 'bililivedanmu',
            vod_remarks: remark,
            vod_actor: '直播间id-' + aid, vod_director: dire, vod_content: desc
        };
        vod['vod_play_from'] = 'B站';
        vod['vod_play_url'] = 'flv线路原画$platform=web&quality=4_' + aid + '#flv线路高清$platform=web&quality=3_' + aid + '#h5线路原画$platform=h5&quality=4_' + aid + '#h5线路高清$platform=h5&quality=3_' + aid;
        return vod
    },
    搜索: async function (wd, quick, pg) {
        let {input, publicUrl} = this;
        let html = (await getHtml({
            method: 'Get',
            url: input,
            headers: Object.assign({}, rule.headers, {"Cookie": ENV.get('bili_cookie')})
        })).data;
        let msg = html.message;
        if (msg !== "0") {
            log(html);
            let VODS = [{
                vod_name: "➢" + msg,
                vod_id: "no_data",
                vod_remarks: "别点,缺少bili_cookie",
                vod_pic: urljoin(publicUrl, './images/404.jpg'),
            }];
            return VODS
        } else {
            let videos = [];
            let vodList = html.data.result.live_room;
            vodList.forEach(function (vod) {
                let aid = vod["roomid"];
                let title = "直播间：" + vod["title"].replace(/<em class=\"keyword\">/, "").replace("</em>", "");
                let img = "https:" + vod["user_cover"];
                let remark = vod["watched_show"]["text_small"] + "  " + vod["uname"];
                videos.push({
                    vod_id: aid,
                    vod_name: title,
                    vod_pic: img,
                    vod_remarks: remark
                })
            });
            return videos
        }
    },
}
