const helper = {}
const crypto = require('crypto');
helper.getSign = function(distributorId,secretKey) {
    const params = [distributorId, secretKey, helper.getCurrentUnixTimestamp()];
    // 将参数数组中的每个元素转换为字符串并连接成一个单一的字符串
    const paramStr = params.map(String).join('');
    
    // 使用 MD5 算法生成哈希值
    const hash = crypto.createHash('md5');
    hash.update(paramStr);
    
    // 返回哈希值的十六进制表示
    return hash.digest('hex');
}
helper.getCurrentUnixTimestamp = function() {
    return Math.floor(Date.now() / 1000)
}

helper.getFqRequestOpt = function(key,sid_tt='b0390e26648a71801795b3b13c9d7d20'){
    const options = {
        url: `https://api.whbfxb.cn/api/novelsale/reader/get/content/v1/?aid=40013183&device_brand=realme&device_platform=android&device_type=RMX2020&mp_sdk_version=3.21.0&novelsale_app_scene=023001&version_code=230&key=${key}&item_source=1&module_name=ad_link&click_id=__CLICKID__&clickid=__CLICKID__&creativetype=__CTYPE__&demand_id=0&item_id=&media_source=1&mid1=__MID1__&mid2=__MID2__&mid3=__MID3__&mid4=__MID4__&mid5=__MID5__&projectid=__PROJECT_ID__&promotionid=__PROMOTION_ID__&request_id=__REQUESTID__&book_id=&host_novelsale_app_id=40013183`,
        headers: {
            'cookie': `sid_tt=${sid_tt}; ` +
                     'ssid_ucp_v1=1.0.0-KDU0Nzc2NjZmZjRhNTZkYWM5YTMwN2ZmNzAyODMwNjFjZmQyMzA4OTYKFQjkktCcvMz_ARDGw7W6Bhjv6CA4CBoCbHEiIDhjOTg2NjgwY2Q4NzE2YmEzMmJhMjBjM2MwMmEwOTJk; ' +
                     'is_staff_user=false; ' +
                     'sid_ucp_v1=1.0.0-KDU0Nzc2NjZmZjRhNTZkYWM5YTMwN2ZmNzAyODMwNjFjZmQyMzA4OTYKFQjkktCcvMz_ARDGw7W6Bhjv6CA4CBoCbHEiIDhjOTg2NjgwY2Q4NzE2YmEzMmJhMjBjM2MwMmEwOTJk; ' +
                     `sessionid=${sid_tt}; ` +
                     'ttwid=1%7CdTM3VEQ9KwlMsV2FV6Gaxv5CBXmd5i44fjDoZV_a8g8%7C1733124580%7Cc0952b31723a2f5d5e4b9f84a1c29cb7cbde5592906de58798b92a695d3fb292; ' +
                     `sessionid_ss=${sid_tt}; ` +
                     `sid_guard=${sid_tt}%7C1733124550%7C5184000%7CFri%2C+31-Jan-2025+07%3A29%3A10+GMT; ` +
                     'uid_tt=3361cf574bbdd82820371b4667fc9b91; ' +
                     'passport_csrf_token=f7e1da8668d26b8509725bcf7fa95628; ' +
                     'uid_tt_ss=3361cf574bbdd82820371b4667fc9b91; ' +
                     'passport_csrf_token_default=f7e1da8668d26b8509725bcf7fa95628; ' +
                     'store-region=cn-gs; ' +
                     'store-region-src=uid; ' +
                     'n_mh=9-mIeuD4wZnlYrrOvfzG3MuT6aQmCUtmr8FxV8Kl8xY',
            'user-agent': 'Mozilla/5.0 (Linux; Android 9; Mi Note 3 Build/PKQ1.181007.001; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/120.0.6099.193 Mobile Safari/537.36 aweme/29.4.0 ToutiaoMicroApp/3.21.0 PluginVersion/29409006',
            'content-type': 'application/json',
            'referer': 'https://tmaservice.developer.toutiao.com/?appid=tt1b316d8c8401e42101&version=2.7.0'
        }
      };
    return options
}
helper.getHyCreateLinkOpt = function(token){
    const options = {
        url: `https://ms.zhangwenpindu.cn/manage/distribution/createLink`,
        headers:  {
            'Accept': 'application/json, text/plain, */*',
            'Accept-Language': 'zh-CN,zh;q=0.9',
            'Cache-Control': 'no-cache',
            'Connection': 'keep-alive',
            'Content-Type': 'application/x-www-form-urlencoded',
            'Origin': 'https://manage.zhangwenpindu.cn',
            'Pragma': 'no-cache',
            'Referer': 'https://manage.zhangwenpindu.cn/',
            'Sec-Fetch-Dest': 'empty',
            'Sec-Fetch-Mode': 'cors',
            'Sec-Fetch-Site': 'same-site',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36',
            'sec-ch-ua': '"Chromium";v="130", "Google Chrome";v="130", "Not?A_Brand";v="99"',
            'sec-ch-ua-mobile': '?0',
            'sec-ch-ua-platform': '"Windows"'
        }
    };
    options.headers['Authorization'] = `Bearer ${token}`
    return options
}

helper.getTimeStampByHourMinute = function(timeStr) {
    const [hours, minutes] = timeStr.split(':').map(Number);
    const today = new Date();
    today.setHours(hours, minutes, 0, 0);
    // return today.getTime();
    return today;
}

helper.getLocalDate = function() {
    const d = new Date();
    const year = d.getFullYear();
    const month = String(d.getMonth() + 1).padStart(2, '0');
    const day = String(d.getDate()).padStart(2, '0');
    return `${year}-${month}-${day}`;
}


helper.getPaginationParams = function(total, pageSize = 500) {
    const pages = Math.ceil(total / pageSize);
    const result = [];
    
    for(let page = 1; page <= pages; page++) {
        const offset = (page - 1) * pageSize;
        const limit = Math.min(pageSize, total - offset);
        result.push({
            page,
            offset,
            limit
        });
    }
    
    return result;
}
module.exports = helper;