var express = require("express");
var path = require("path");
var fs = require("fs");
var axios = require('axios');
const tools = require("../../tools");
// const config = require("../config");
const jl_get_auth_account_list = require('./jl_get_auth_account_list')
const jl_get_account_list = require('./jl_get_account_list');
// var api = [
//     // ["get_book_info"] = require("./get_book_info"),
// ]
const api ={
    
}
// console.log('Server started on localhost:9877; press Ctrl-C to terminate...!');
// api.test = function(){
//     const postData = {
//         distributor_id: tools.distributorId,
//         ts: tools.getCurrentUnixTimestamp(),
//         sign: tools.getSign(),
//         book_id: '7426993954299579401',
//     };
//     var url = `https://www.changdunovel.com/novelsale/openapi/content/book_meta/v1/?distributor_id=${postData.distributor_id}&ts=${postData.ts}&sign=${postData.sign}&book_id=${postData.book_id}`;
//     console.log("url",url)
//     axios.get(url)
//         .then(response => {
//             console.log('Response data:', response.data);
//         })
//         .catch(error => {
//             console.error('Error:', error);
//         });
// }

function getToken(call_back){
    let getTokenUrl = "https://clipvideoup.s6kuwan.com/adoce/advertiser/get_access_token"
    axios.get(getTokenUrl)
    .then(response => {
        console.log('Response data:', response.data.content['access_token']);
        call_back(response.data.content['access_token'])
    })
    .catch(error => {
        call_back(null)
        console.error('Error:', error);
    });
}

//获取主账户列表
function getAdvertiserList(call_back){
    if(global.config.advertiser_list.length<=0){
        jl_get_auth_account_list.send((list)=>{
            call_back(list)
        })
    }else{
        call_back(global.config.advertiser_list)
    }
}

//获取账号工作台下账户列表
function getAppAccountList(cc_account_id ,call_back){
    if(global.config.account_list.length<=0){
        jl_get_account_list.send(cc_account_id,(list)=>{
            call_back(list)
        })
    }else{
        call_back(global.config.account_list)
    }
}


function createLink(obj){
    // console.log("global.config:",global.config)
    for (const key in global.config.app_list) {
        if (Object.prototype.hasOwnProperty.call( global.config.app_list, key)) {
            const app_list =  global.config.app_list[key];
            if(app_list.length>0){
                for (let index = 0; index < app_list.length; index++) {
                    const app_item = app_list[index];
                    console.log("createLink:",obj.channel_type,app_item.remark)
                    if(obj.channel_type==app_item.remark){
                        var jl_add_tg_link = require('./jl_add_tg_link')
                        let advertiser_id = parseInt(key)
                        jl_add_tg_link.send(advertiser_id,app_item.instance_id,obj.app_page)
                    }
                }
             
            }
        }
    }
    
}

function getAppIdByChannel(channel_type){
    let temp = []
    for (const key in global.config.app_list) {
        if (Object.prototype.hasOwnProperty.call( global.config.app_list, key)) {
            const app_list =  global.config.app_list[key];
            if(app_list.length>0){
                for (let index = 0; index < app_list.length; index++) {
                    const app_item = app_list[index];
                    if(channel_type==app_item.remark){
                        temp.push(app_item.app_id)
                    }
                }
             
            }
        }
    }
    return temp;
}
module.exports = {
    getToken,
    getAdvertiserList,
    getAppAccountList,
    createLink
}