const CMD = {}
const HttpClient = require('../../../src/HttpClient')
const config = require('../../../etc/config.json')
const helper = require('../../helper')
const dns = require('dns').promises;
const https = require('https');
const crypto = require('crypto');
// 获取七猫充值模板
CMD.login = async function(call_back){
    const result = await CMD.login_qm();
    console.log("result:",result)
    call_back(result.data,result.headers)
}

function encodePassword(password) {
    // const encoded = Buffer.from(password).toString('base64')
    //   .replace(/Z/g, 'W')
    //   .replace(/y/g, 'n')
    //   .replace(/w/g, 'l')
    //   .replace(/z/g, 'e');
    return Buffer.from(password).toString('base64');
}

function generateSignature(params) {
    const timestamp = Date.now();
    // 假设签名规则为: md5(账号 + 密码 + 时间戳 + 密钥)
    const signStr = `${params.account_name}${params.password}${timestamp}your_secret_key`;
    return crypto.createHash('md5').update(signStr).digest('hex');
  }
  
CMD.login_qm =async function() {
    let timestamp = Date.now();
    timestamp = "1745997864484"
    const postData = JSON.stringify({
        "account_name": "zhuoyue",
        "password": "Wnl3ejAzOTYz",
        "env": ""
    });

    // const signature = crypto.createHash('sha256')
    //     .update(str)
    //     .digest('hex');
    
    // console.log('输入字符串:', str);
    // console.log('生成的签名:', signature);

    const options = {
        hostname: 'new-media-fx.qimao.com',
        path: `/api/account/login?t=${timestamp}&env=&qm_csrf_backend=undefined`,
        method: 'POST',
        headers: {
            'Host': 'new-media-fx.qimao.com',
            'Connection': 'keep-alive',
            'Content-Length': Buffer.byteLength(postData),
            'sec-ch-ua-platform': '"Windows"',
            'signature': '87c88d2b321fd439c756847c7348a198be6abf769a46f34dbc83d9a4fec34567',
            'sec-ch-ua': '"Microsoft Edge";v="135", "Not-A.Brand";v="8", "Chromium";v="135"',
            'project': '',
            'sec-ch-ua-mobile': '?0',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36 Edg/135.0.0.0',
            'Accept': 'application/json, text/plain, */*',
            'target-id': '',
            'Content-Type': 'application/json;charset=UTF-8',
            'app-external-id': '',
            'Origin': 'https://new-media-fx.qimao.com',
            'Sec-Fetch-Site': 'same-origin',
            'Sec-Fetch-Mode': 'cors',
            'Sec-Fetch-Dest': 'empty',
            'Referer': 'https://new-media-fx.qimao.com/backend/login',
            'Accept-Encoding': 'gzip, deflate, br, zstd',
            'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
            'Cookie': 'sensorsdata2015jssdkcross=%7B%22%24device_id%22%3A%22193bdc124645e2-0cbeaee172d4ce8-4c657b58-921600-193bdc12465567%22%7D; sa_jssdk_2015_new-media-fx_qimao_com=%7B%22distinct_id%22%3A%22193bdc124645e2-0cbeaee172d4ce8-4c657b58-921600-193bdc12465567%22%2C%22first_id%22%3A%22%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E5%BC%95%E8%8D%90%E6%B5%81%E9%87%8F%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC%22%2C%22%24latest_referrer%22%3A%22http%3A%2F%2F120.79.128.142%3A4999%2F%22%7D%2C%22identities%22%3A%22eyIkaWRlbnRpdHlfY29va2llX2lkIjoiMTkzYmRjMTI0NjQ1ZTItMGNiZWFlZTE3MmQ0Y2U4LTRjNjU3YjU4LTkyMTYwMC0xOTNiZGMxMjQ2NTU2NyJ9%22%2C%22history_login_id%22%3A%7B%22name%22%3A%22%22%2C%22value%22%3A%22%22%7D%7D'
        }
    };

    return new Promise((resolve, reject) => {
        const req = https.request(options, (res,_) => {
            let data = '';
            
            res.on('data', (chunk) => {
                data += chunk;
            });
            res.on('end', () => {
                try {
                    resolve({data:JSON.parse(data),headers:res.headers});
                } catch (e) {
                    resolve(data);
                }
            });
        });

        req.on('error', (error) => {
            reject(error);
        });

        req.write(postData);
        req.end();
    });
    
    
}
module.exports = CMD;