//执行一个创建番茄链接任务
const axios = require('axios');
const helper = require('../src/helper');
const config = require('../etc/config.json')
const CMD = {}

CMD.runTask = async function(data,main_info,PlatformInfo,call_back){

    try {
        console.log("main_info:",main_info)
        let  tg_link_config = JSON.parse(main_info.tg_link_config)
        let kadian_id = tg_link_config['kadian_id']
        let chongzhi_id = tg_link_config['chongzhi_id']
        let huichuan_id = tg_link_config['huichuan_id']
        // let chongzhi_list = JSON.parse(PlatformInfo.chongzhi)
        let huichuan_list = JSON.parse(PlatformInfo.huichuan)
        let kadian_list = JSON.parse(PlatformInfo.kadian)
        let kadian_name = null
        let tg_config = {
          chongzhi_key:'',
          huichuan_key:'',
          kadian_key:''
        }
        // for (let index = 0; index < chongzhi_list.length; index++) {
        //   const cz_obj = chongzhi_list[index];
        //   if(cz_obj.id==chongzhi_id){
        //     chongzhi_name = cz_obj.name
        //     tg_config.chongzhi_key = cz_obj.value
        //     break
        //   }
        // }
      
      
        for (let index = 0; index < huichuan_list.length; index++) {
          const hc_obj = huichuan_list[index];
          if(hc_obj.id==huichuan_id){
            huichuan_name = hc_obj.name
            tg_config.huichuan_key = hc_obj.value
            break
          }
        }
      
        for (let index = 0; index < kadian_list.length; index++) {
          const kd_obj = kadian_list[index];
          if(kd_obj.id==kadian_id){
            kadian_name = kd_obj.name
            break
          }
        }
        tg_config.kadian_key = kadian_name
        let name  =   `${data.product_name}_${main_info.id}`
        let accountId = main_info.hy_accountId
        let linkType = 2
        let bookId = data.product_id
        let mediaChannel = 1
        let group = main_info.group_id
        let site = PlatformInfo.mini_program_platform_id==config.wx?"7": "5" 
        let paidPoint = tg_config.kadian_key; //卡点
        let adPassbackTemplateId = tg_config.huichuan_key; //回传模板
        let timestamp = helper.getCurrentUnixTimestamp()
        let sign = helper.generateSign({
            "pid":config.heiyan_config.pid,
            "timestamp":timestamp,
            "sign":"",
            "name":name,
            "accountId":accountId,
            "linkType":linkType,
            "bookId":bookId,
            "mediaChannel":mediaChannel,
            "group":group,
            "site":site,
            "adPassbackTemplateId":adPassbackTemplateId,
            "paidPoint":paidPoint,
        },config.heiyan_config.key)
        var url = config.heiyan_config.createLink
        var base_params = `/?sign=${sign}&pid=${config.heiyan_config.pid}&timestamp=${timestamp}`
        var other_params = `&accountId=${accountId}&name=${name}&linkType=${linkType}&bookId=${bookId}&mediaChannel=${mediaChannel}&group=${group}&site=${site}&adPassbackTemplateId=${adPassbackTemplateId}&paidPoint=${paidPoint}`
        url = `${url}${base_params}${other_params}`
        console.log("url:",url)
        let response = await axios.post(url)
        let link_data  = response.data
        console.log("response:",response)
        if(link_data.code==1){
            let params = PlatformInfo.mini_program_platform_id==config.wx?link_data.data.wxFullPath.split('?'):link_data.data.fullPath.split('?')
            let start_page = params[0]
            let start_param = params[1]
            const linkId = start_param.match(/linkId=([^&]*)/)[1];
            data.promotion_id = linkId
            data.start_page = start_page
            data.start_param = start_param
            call_back(data) 
        }else{
            call_back(data,link_data)
        }
    } catch (error) {
        call_back(data,error)
        console.error('There was an error!', error);
    }

}

module.exports = CMD;