// 处理更改配置后的处理
const { parentPort } = require('worker_threads');
const redis_help = require('../use_redis');
const video_applet_product_controllers = require('../data_manager/Controllers/video_applet_product_controllers');

var find_video_applet_product_task_list = []
var hy_create_link_task_list = []
var fq_create_link_task_list = []
var intervalId = 0
const CMD = {}
// 接收主线程消息
parentPort.on('message', async (data) => {
    // 处理数据的逻辑
    const result = processData(data);
    
    // 发送结果回主线程
    parentPort.postMessage(result);
});
CMD.find_video_applet_product = async function(data,call_back){
    try{
        const result = await video_applet_product_controllers.getAppletProductData({book_id:data.book_id,main_id:data.main_id})
        if(result.success){
            call_back(true,result.data,data.n_data,data.main_info,data.PlatformInfo)
        }else{
            call_back(false,null,data.n_data,data.main_info,data.PlatformInfo)
        }
    }catch(e){
        console.log("error:",e)
    }
}

CMD.runFqTask = async function(){
    let fq_create_link_task = fq_create_link_task_list.pop()
    require('../3collect_and_create_data/fq_create_link').runTask(fq_create_link_task.n_data
        ,fq_create_link_task.main_info,
        fq_create_link_task.PlatformInfo,finish_call_back)
}

CMD.runHyTask = function(){
    let hy_create_link_task = hy_create_link_task_list.pop()
    require('../3collect_and_create_data/hy_create_link').runTask(hy_create_link_task.n_data
        ,hy_create_link_task.main_info,
        hy_create_link_task.PlatformInfo,finish_call_back)
}

CMD.update = function(){
    if(intervalId!=0){
        clearInterval()
    }
    intervalId = setInterval(() => {
        try{
            if(find_video_applet_product_task_list.length>0){
                let find_video_applet_product = find_video_applet_product_task_list.pop()
                CMD.find_video_applet_product(find_video_applet_product,finish_find_video_applet_product)
            }
        
            if(hy_create_link_task_list.length>0){
                CMD.runHyTask()
            }
        
            if(fq_create_link_task_list.length>0){
                CMD.runFqTask()
            }
        }catch(e){
            console.error("CMD.update error:",e)
        }

    }, 2000);
}


let finish_create_link = function(data){
   
}

let finish_call_back = async (new_data)=>{
    try{
        const result = await video_applet_product_controllers.createAppletProductData({
            book_platform:new_data.tg_platform_id,
            product_name:new_data.book_name,
            product_id:new_data.book_id,
            dy_small_program_start:new_data.start_page,
            dy_small_program_start_data:new_data.start_param,
            dy_small_applet_app_id:new_data.app_id,
            status:0,
            main_id:new_data.main_id,
            promotion_id:new_data.promotion_id,
            wait_status:0,
        })
        if(result.success){
            finish_create_link(new_data)
        }
    }catch(e){
        console.log("finish_call_back error:",e)
    }
}

let finish_find_video_applet_product = (is_find,video_applet_product_item,n_data,main_info,PlatformInfo)=>{
    if(is_find){
        n_data.start_page = video_applet_product_item.dy_small_program_start
        n_data.start_param = video_applet_product_item.dy_small_program_start_data
        finish_create_link(n_data)
    }else{
        if(n_data.tg_platform_id==1){ //"黑岩"
            hy_create_link_task_list.push({
                n_data:n_data,
                main_info:main_info,
                PlatformInfo:PlatformInfo
            })
        }else if(n_data.tg_platform_id==2){ //"番茄"
            fq_create_link_task_list.push({
                n_data:n_data,
                main_info:main_info,
                PlatformInfo:PlatformInfo
            })
        }

    }

}
function processData(data) {

    let res = data.data
    if(res.cmd=="addMain"){
        redis_help.connect(async (results)=>{
            if(results){
                let msgBody = res.data
                let PlatformConfig = JSON.parse(await redis_help.getKeyValue("PlatformConfig"))
                let main_info = msgBody.main_info
                let list  =  msgBody.list
                let PlatformInfo = null
                for (let index = 0; index < PlatformConfig.length; index++) {
                    const element = PlatformConfig[index];
                    if(element.tg_platform_id == main_info.tg_platform_id){
                        PlatformInfo = element
                        break
                    }
                }
                if(PlatformInfo==null){
                    console.log("平台配置错误：",msgBody)
                }else{
                    for (const product_id in list) {
                        if (Object.prototype.hasOwnProperty.call(list, product_id)) {
                            let n_data = {book_id:product_id,
                                book_name:list[product_id].product_name,
                                tg_platform_id:list[product_id].book_platform,
                                app_id:list[product_id].dy_small_applet_app_id,
                                main_id:main_info.id
                            }
                            n_data.tg_link_config = main_info.tg_link_config
                            find_video_applet_product_task_list.push({book_id:product_id,main_id:main_info.id,n_data:n_data,main_info:main_info,PlatformInfo:PlatformInfo})
                        }
                    }
                    CMD.update()
                }
       

            }
        })
  
    }
    return data;
}