const fetch = require('node-fetch'); // Node.js 18以下版本需要安装 node-fetch
const tools = require('../../../tools');
const helper = require('../../helper');
const redis_help = require('../../use_redis');
const CMD = {}

CMD.search_test_id = async function (bookId) {
    try {
        let OPENSESSID = await redis_help.getKeyValue("OPENSESSID")
        let time = helper.getCurrentUnixTimestamp()
        const response = await fetch(`https://open.yuewen.com/api/wechatspread/bookSpread?cbid=${bookId}&page=1&version=2&order_field=allwords&order_type=0&content_type=1&category1=-1&allwords=-1&category2=-1&isfinish=-1&level=-1`, {
            "headers": {
              "accept": "application/json, text/plain, */*",
              "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
              "priority": "u=1, i",
              "sec-ch-ua": "\"Microsoft Edge\";v=\"131\", \"Chromium\";v=\"131\", \"Not_A Brand\";v=\"24\"",
              "sec-ch-ua-mobile": "?0",
              "sec-ch-ua-platform": "\"Windows\"",
              "sec-fetch-dest": "empty",
              "sec-fetch-mode": "cors",
              "sec-fetch-site": "same-origin",
              "cookie": `Hm_lvt_990f9ab9737a266517417cc2949bb3f4=${time}; csrfToken=9EeGTClKZ3EJFZjIQDcVozZj; OPENSESSID=${OPENSESSID}; yw_open_token=6784835e6c51a; is_read_notice=6784835e6c51a; sidebarStatus=0`,
              "Referer": "https://open.yuewen.com/new/library",
              "Referrer-Policy": "strict-origin-when-cross-origin"
            },
            "body": null,
            "method": "GET"
        });

        const data = await response.json();
        console.log('data:', data);
        if(data.code!=0){
            throw data
        }
        console.log('响应状态:', response.status);
        if(data.data.list.length<=0){
            return null
        }
        console.log('响应数据:', data.data.list[0]);
        let book_info = data.data.list[0]
        return book_info;
    } catch (error) {
        return null
    }
}

CMD.search_id = async function (bookId) {
    try {
        let OPENSESSID = await redis_help.getKeyValue("OPENSESSID")
        let time = helper.getCurrentUnixTimestamp()
        const response = await fetch(`https://open.yuewen.com/api/wechatspread/bookSpread?cbid=${bookId}&page=1&version=2&order_field=allwords&order_type=0&content_type=1&category1=-1&allwords=-1&category2=-1&isfinish=-1&level=-1`, {
            "headers": {
              "accept": "application/json, text/plain, */*",
              "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
              "priority": "u=1, i",
              "sec-ch-ua": "\"Microsoft Edge\";v=\"131\", \"Chromium\";v=\"131\", \"Not_A Brand\";v=\"24\"",
              "sec-ch-ua-mobile": "?0",
              "sec-ch-ua-platform": "\"Windows\"",
              "sec-fetch-dest": "empty",
              "sec-fetch-mode": "cors",
              "sec-fetch-site": "same-origin",
              "cookie": `Hm_lvt_990f9ab9737a266517417cc2949bb3f4=${time}; csrfToken=9EeGTClKZ3EJFZjIQDcVozZj; OPENSESSID=${OPENSESSID}; yw_open_token=6784835e6c51a; is_read_notice=6784835e6c51a; sidebarStatus=0`,
              "Referer": "https://open.yuewen.com/new/library",
              "Referrer-Policy": "strict-origin-when-cross-origin"
            },
            "body": null,
            "method": "GET"
        });

        const data = await response.json();
        console.log('data:', data);
        if(data.code!=0){
            throw data
        }
        console.log('响应状态:', response.status);
        if(data.data.list.length<=0){
            return null
        }
        console.log('响应数据:', data.data.list[0]);
        let book_info = data.data.list[0]
        let info = {}
        info.words =  book_info.WordsCntShow
        info.book_name =  book_info.BookName
        info.book_id =  book_info.cbid

        if(book_info.WordsCnt<100000){
            info.genre = 3;
        }else{
            info.genre = 1;
        }
        // info.genre = 999;
       
        // if(book_info.CategoryName=="短篇"){  
        //     info.genre = 3;
        // }else{
        //     info.genre = 1;
        // }
        return info;
    } catch (error) {
        return null
    }
}

// Node.js 18及以上版本可以直接运行
// Node.js 18以下版本需要先安装 node-fetch:
// npm install node-fetch@2

module.exports = CMD;