const crypto = require('crypto');
const config = require('./etc/config.json')
class tools {
    constructor(redis_help){
        this.redis_help = redis_help
    }

    init(){

    }

    distributorId = 1814786227164169;
    secretKey = 'CN6KQ8Bauo8JXg5fFPk86EHdRFIUVnyV';
    heiyan_config(){
        return {  //黑岩配置
            chang_pian_user:{
                userName:"康帅",
                password:"Ks25666"
            },
            duan_pian_user:{
                userName:"王海泉",
                password:"My20240088"
            },
            default_user:{
                userName:"zhuoyue003",
                password:"Xuan2026@123"
            },
        }
    }
    unixTimestampToDate = function(timestamp) {
        const date = new Date(timestamp * 1000); // Unix时间戳是秒，JavaScript的Date对象需要毫秒
        return date.getTime();
    }

    dateToUnixTimestamp = function (date) {
        return Math.floor(date.getTime() / 1000); // 将毫秒转换为秒
    }
    
    calculateTimestampDifference = function(timestamp1, timestamp2) {
        return Math.abs(timestamp1 - timestamp2);
    }

    formatUnixTimestamp = function(timestamp, format = 'YYYY-MM-DD HH:mm:ss') {
        const date = new Date(timestamp * 1000);
        const year = date.getFullYear();
        const month = String(date.getMonth() + 1).padStart(2, '0'); // 月份从0开始，需要+1
        const day = String(date.getDate()).padStart(2, '0');
        const hours = String(date.getHours()).padStart(2, '0');
        const minutes = String(date.getMinutes()).padStart(2, '0');
        const seconds = String(date.getSeconds()).padStart(2, '0');
    
        const formattedDate = format
            .replace('YYYY', year)
            .replace('MM', month)
            .replace('DD', day)
            .replace('HH', hours)
            .replace('mm', minutes)
            .replace('ss', seconds);
    
        return formattedDate;
    }

    getCurrentUnixTimestamp = function() {
        return Math.floor(Date.now() / 1000)
    }

    getCurrentUnixTimestamp = function() {
        return Math.floor(Date.now() / 1000)
    }

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

    async getHyToken (){
        return await this.redis_help.getKeyValue("hei_yan_token")
    }

    getSupdate(){ //插入素材域名
        return config.isDebug?config.debug_supdate_config:config.release_supdate_config
    }

    getDataBaseConfig(){
        return config.isDebug?config.debug_mysql:config.release_mysql
    }

    getTaskDataBaseConfig(){
        return config.isDebug?config.debug_task_mysql:config.release_task_mysql
    }

    async getFqSidtt() {
        let sidtt = 'b0390e26648a71801795b3b13c9d7d20'
        let list =  await this.redis_help.getKeyValue("all_fq_key")
        if(list == null){
            return sidtt
        }
        list = JSON.parse(list)
        if(list.length<=0){
            return sidtt
        }
        sidtt = list[0]
        return sidtt.sid_tt
    }
}

module.exports = new tools(require('./src/use_redis'));
