//执行一个创建黑岩链接任务
const CMD = {}
const axios = require('axios');
let get_hy_recharge_template = require('../src/api/hy/get_hy_recharge_template')
let get_hy_paid_point = require('../src/api/hy/get_hy_paid_point')
let get_hy_call_back_template = require('../src/api/hy/get_hy_call_back_template');
const tools = require('../tools');
const HttpClient = require('../src/HttpClient');
const helper = require('../src/helper');
//卡点
function get_paid_point(book_id,kadian_name,call_back){
  let chapter_num = parseInt(kadian_name)
  if(chapter_num==0||chapter_num>10){
    return call_back("")
  }
  get_hy_paid_point.get_hy_paid_point(book_id,(data)=>{
        if(chapter_num>=data.rows.length){
          call_back("")
        }else{
          call_back(""+chapter_num)
        }
  })
}
//回传
function get_call_back(huichuan_name,call_back){
  get_hy_call_back_template.get_callback_template((data)=>{
      if(data.code==200){
        let is_find = false
        for (let index = 0; index < data.data.length; index++) {
          const obj = data.data[index];
          if(obj.value==huichuan_name){
            is_find = true
            call_back(obj.key)
            break
          }
        }
        if(!is_find){
          call_back('6')
        }
      }else{
        call_back('6')
      }
  })
}

function get_recharge_template(book_id,chongzhi_name,call_back){
  get_hy_recharge_template.get_recharge_template(book_id,(data)=>{
      if(data.code==200){
        let is_find = false
        for (let index = 0; index < data.data.length; index++) {
          const obj = data.data[index];
          if(obj.value==chongzhi_name){
            is_find = true
            call_back(obj.key)
            break
          }
        }
        if(!is_find){
          call_back('0')
        }
      }else{
        call_back('0')
      }
  })
}
CMD.runTask = function(data,main_info,PlatformInfo,call_back){
  let group_id = main_info.group_id
  if(group_id==null||group_id==""){
    group_id = "35"
  }
  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 chongzhi_name = null
  let huichuan_name = null
  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
    }
  }

  let create_hy_link = async () =>{
      let topUpTemplateId = tg_config.chongzhi_key;  //充值模板
      let passBackTemplateId = tg_config.huichuan_key; //回传模板
      let paidPoint = tg_config.kadian_key; //卡点
      console.log("tg_config:",tg_config)
      try{
        let clinet = new HttpClient()
        const postData = {
          cmd:"create_link",
          name:data.product_name,
          bookId:data.product_id,
          topUpTemplateId:topUpTemplateId,
          passBackTemplateId:passBackTemplateId,
          paidPoint:paidPoint,
          token:await tools.getHyToken()
        };
        let t_data = {
          "name": postData.name,
          "bookId": postData.bookId,
          "passBackTemplateId":  postData.passBackTemplateId,
          "adPassbackTemplateId": "",
          "topUpTemplateId": postData.topUpTemplateId,
          "mediaChannel": "1",
          "group": group_id, 
          "paidPoint": postData.paidPoint,
          "site": "5" 
        }
        let res =  await clinet.otherPost( helper.getHyCreateLinkOpt(postData.token) ,t_data)
        if(res['code']==500){
            throw res['msg']
        }
        let response = {
            "message": "create_link successfully",
            "received_data":{
                            "appId":res["data"]["appId"],
                            "launchPath":res["data"]["launchPath"],
                            "fullPath":res["data"]["fullPath"],
                            "wxMountLink":res["data"]["wxMountLink"]}
        }
        let params = response.received_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) 
      }catch(e){
        call_back(null)
        console.error('There was an error!', e);
      }
  }

  get_paid_point(data.book_id,kadian_name,(kadian_key)=>{
    tg_config.kadian_key = kadian_key
    create_hy_link()
  })
}

module.exports = CMD;