const axios = require('axios');
const helper = require('../../helper');
const CMD = {}
//book_id=书籍id
//index=第几章开始生成推广链。
//promotion_name=推广链接名称
//media_source=快应用：1 字节、2 腾讯、6 vivo，不填默认字节；H5：不需要填写 短剧：1 字节、3 快手 4 微信
CMD.send = function(book_id,index=1,promotion_name="",media_source=1,recharge_template_id,call_back){
    var distributorId = 1814786227164169;
    var secretKey = 'CN6KQ8Bauo8JXg5fFPk86EHdRFIUVnyV';
    const postData = {
        distributor_id: distributorId,
        ts: helper.getCurrentUnixTimestamp(),
        sign: helper.getSign(distributorId,secretKey),
        book_id: book_id,
        index:index,
        promotion_name:promotion_name,
        media_source:media_source,
        recharge_template_id:recharge_template_id
    };
    var url = 'https://www.changdunovel.com/novelsale/openapi/promotion/create/v1'
    var base_params = `/?distributor_id=${postData.distributor_id}&ts=${postData.ts}&sign=${postData.sign}`
    var other_params = `&book_id=${postData.book_id}&index=${postData.index}&promotion_name=${postData.promotion_name}&media_source=${postData.media_source}&recharge_template_id=${postData.recharge_template_id}`
    axios.get(`${url}${base_params}${other_params}`)
        .then(response => {
            if(response.data.promotion_url==undefined){
                if(response.data.code==4000){
                    console.log("response promotion_name:",promotion_name)
                }else{
                    console.log("response :",response)
                }
     
            }else{
                let params = response.data.promotion_url.split('?')
                // let promotion_id = response.data.promotion_id
                let start_page = params[0]
                let start_param = params[1]
                if(call_back!=null){
                    call_back(start_page,start_param, response.data)
                }
            }
       
            // console.log('Response data:', response.data );
        })
        .catch(error => {
            console.error('Error:', error);
    });
}


module.exports = CMD;