const CMD = {}
const video_product_controllers = require('../src/data_manager/Controllers/video_product_controllers');
const filter_data_controllers = require('../src/data_manager/Controllers/filter_data_controllers');
const video_applet_product_controllers = require('../src/data_manager/Controllers/video_applet_product_controllers');
const redis_help = require('../src/use_redis');
const axios = require('axios');
const config = require('../etc/config.json');
const time_count = 1000;
const task_queue = []

function getProductById(product_list,product_id){
    for (let index = 0; index < product_list.length; index++) {
        const product_item = product_list[index];
        if(product_item.product_id==product_id){
            return product_item
        }
    }
    return null
}
async function processTask(){
    let result = null
    try{
        let isPauseTask =  await redis_help.getKeyValue("isPauseTask")
        if(isPauseTask=="true"){
            return
        }
        let FilterConfig = await redis_help.getKeyValue("FilterConfig")
        FilterConfig = JSON.parse(FilterConfig)
    //     result = await filter_data_controllers.getFilterDataByStatusAndLimit({ status: 0 , tg_platform_id: config.platform_dianzhong },500);
    //     if(result.success){
    //         let ProductIds = []
    //         let FilterList = []
    //         for (let index = 0; index < result.data.length; index++) {
    //             let filter_item = result.data[index];
    //             ProductIds.push(filter_item.book_id)
    //             FilterList[filter_item.book_id] = filter_item
    //         }

    //         console.log("ProductIds:",ProductIds)

    //         let product_list_result = await video_product_controllers.findNonExistentProductIds(ProductIds);

    //         if(product_list_result.success){

    //             for (let index = 0; index < result.data.length; index++) {
    //                 let filter_item = result.data[index];
    //                 if(product_list_result.data.existingProductIds.has(filter_item.book_id)){
    //                     let info = FilterList[filter_item.book_id];
    //                     let product_item = getProductById(product_list_result.data.existingRecords,filter_item.book_id)
    //                     if (!info) {
    //                         throw `No filter info found for book_id: ${filter_item.book_id}`
    //                     }
    //                     if (!product_item) {
    //                         throw `product_item is null: ${filter_item.book_id}`
    //                     }
    //                     info.words = product_item.words
    //                     info.genre =  product_item.genre
    //                     info.book_id = product_item.product_id
    //                     info.book_name = product_item.product_name
    //                     await CMD.update_filter_data(FilterConfig,info)
    //                     await new Promise(resolve => setTimeout(resolve,100));
    //                 } 
    //             }

    //             let Ids = []
    //             for (let index = 0; index < product_list_result.data.nonExistentIds.length; index++) {
    //                 const product_id = product_list_result.data.nonExistentIds[index];
    //                 Ids.push(FilterList[product_id].id)
    //                 task_queue.push({result:{data:FilterList[product_id]}})
    //             }
    //             console.log("Ids：",Ids)
    //             if(Ids.length>0){
    //                 await filter_data_controllers.updateFilterDataByIds({ids:Ids},{status:1})
    //             }
              
    //         }
            
    //     }
    // }catch(e){
    //     console.error("processTask:",e)
    // } finally{
    //     global.setTimeout(processTask, 100);
    // }

        let isInsertTask = false
        result = await filter_data_controllers.getFilterDataByStatus({ status: 0 , tg_platform_id: config.platform_dianzhong });
        if(result.success){
            let tg_platform_id = result.data.tg_platform_id
            //判断是否存在这本书
            if(tg_platform_id==config.platform_dianzhong){ //点众
                let dz_book_result = await video_product_controllers.getProductData({ book_id: result.data.book_id });
                if(dz_book_result.success){
                    result.data.words = dz_book_result.data.words
                    result.data.genre =  dz_book_result.data.genre
                    result.data.book_id = dz_book_result.data.product_id
                    result.data.book_name = dz_book_result.data.product_name
                }else{
                    isInsertTask = true
                }
            }
            if(!isInsertTask){
                await CMD.update_filter_data(FilterConfig,result.data)
            }else{
                await filter_data_controllers.updateFilterData({id:result.data.id},{status:1})
                task_queue.push({result:result})
            }
        }
    }catch(e){
        console.error("processTask error:",e)
        // if(result!=null&&result!=undefined){
        //     await filter_data_controllers.updateFilterData({id:result.data.id},{status:2})
        // }
    } finally{
        global.setTimeout(processTask, 100);
    }

}

async function processCreateTask(){
    let result = null
    try{
        if(task_queue.length>0){
            let task_queue_item = task_queue.pop()
            let product = await video_product_controllers.getProductData({ book_id: task_queue_item.result.data.book_id });
            let FilterConfig = await redis_help.getKeyValue("FilterConfig")
            FilterConfig = JSON.parse(FilterConfig)
            if(!product.success){
                result = task_queue_item.result
                let tg_platform_id = result.data.tg_platform_id
                if(tg_platform_id==config.platform_dianzhong){ //点众
                    let dz_book_data = await require('../src/api/dz/dz_search_book').search_id(result.data.book_id)
                    if(dz_book_data==null){
                        result.data.genre = 999
                    }else{
                        dz_book_data.tg_platform_id = result.data.tg_platform_id
                        if(CMD.isGenre(FilterConfig,dz_book_data.genre)){
                            await CMD.insert_product(dz_book_data)
                        }
                        result.data.words = dz_book_data.words
                        result.data.genre = dz_book_data.genre
                        result.data.book_id = dz_book_data.book_id
                        result.data.book_name = dz_book_data.book_name
                    }
                
                }
            }
            await CMD.update_filter_data(FilterConfig,result.data)
        }
    }catch(e){
        console.log("error::",e)
        if(result!=null){
            await filter_data_controllers.updateFilterData({id:result.data.id},{status:2})
        }
        console.log("processCreateTask:",e)
    } finally{
        global.setTimeout(processCreateTask, time_count);
    }
}
CMD.init = async function(){
    redis_help.connect((results)=>{
        if(results){
        }
    })
    processTask();
    processCreateTask();
}


CMD.getAppInfoByPlatformId = function(AppConfig,tg_platform_id){
    let temp = []
    for (let index = 0; index < AppConfig.length; index++) {
        const element = AppConfig[index];
        if(element.tg_platform_id==tg_platform_id){
            temp.push(element)
        }
    }
    return temp
}
CMD.isBlackBook = function(all_black_books,book_id){
    if(all_black_books==null){
        return false
    }
    for (let index = 0; index < all_black_books.length; index++) {
        const black_book = all_black_books[index];
        if(black_book.product_id==book_id){
            return true
        }
    }
    return false
}
CMD.getMainInfoByAppid = function(MainConfig,app_id){
    let list = []
    for (let index = 0; index < MainConfig.length; index++) {
        const element = MainConfig[index];
        if(element.app_id==app_id){
            list.push(element)
        }
    }
    return list
}
CMD.isGenre = function(FilterConfig,genre){
    let is_need  = false
    //判断长篇短篇是否是要的数据
    for (let index = 0; index < FilterConfig.genre.length; index++) {
        const element = FilterConfig.genre[index];
        if(genre==element){
            is_need = true
            break
        }
    }
    return is_need
}


CMD.update_filter_data = async function(FilterConfig,data) {
    let isGenre = CMD.isGenre(FilterConfig,data.genre)
    // console.log("data.genre:",data.genre,data,isGenre)
    if(isGenre){
        await filter_data_controllers.updateFilterData({id:data.id},{status:1,book_id:data.book_id,book_name:data.book_name,genre:data.genre,words:data.words})
        //创建一个小程序    
        let AppConfig = await redis_help.getKeyValue("AppConfig")

        AppConfig = JSON.parse(AppConfig)

        let AppInfoList = CMD.getAppInfoByPlatformId(AppConfig,data.tg_platform_id)

        for (let j = 0; j < AppInfoList.length; j++) {
            const AppInfo = AppInfoList[j];
            data.app_id = AppInfo.app_id

            let PlatformInfo = await redis_help.getKeyValue("PlatformConfig")

            PlatformInfo = JSON.parse(PlatformInfo)

            if(PlatformInfo==null){
                return console.error("平台配置错误：",data)
            }

            let MainConfig = await redis_help.getKeyValue("MainConfig")

            MainConfig = JSON.parse(MainConfig)

            let list = CMD.getMainInfoByAppid(MainConfig,data.app_id)

            if(list.length<0){
                return console.error("获取主体错误：",data)
            }

            let black_books = await redis_help.getKeyValue("all_black_books")
            
            if(black_books!=null){
                black_books = JSON.parse(black_books)
            }
       
            for (let index = 0; index < list.length; index++) {
                const main_info = list[index];
                data.main_id = main_info.id
                data.main_name = main_info.main_name
                if(main_info.tg_link_config==undefined||main_info.tg_link_config==null){
                    console.error("主体配置推广链接错误：",main_info)
                }else{
                    if(!CMD.isBlackBook(black_books,data.book_id)){
                        //符合标准进行创建表
                        const result = await video_applet_product_controllers.getAppletProductData({book_id:data.book_id,main_id:data.main_id})
                        if(!result.success){
                            //没有找到 可以创建
                            await video_applet_product_controllers.createAppletProductData({
                                book_platform:data.tg_platform_id,
                                product_name:data.book_name,
                                product_id:data.book_id,
                                dy_small_program_start:"",
                                dy_small_program_start_data:"",
                                dy_small_applet_app_id:data.app_id,
                                status:0,
                                main_id:data.main_id,
                                promotion_id:"",
                                wait_status:0,
                            })
                        }
                    }
                }
            }
        }
        
    }else{
        await filter_data_controllers.updateFilterData({id:data.id},{status:2,book_id:data.book_id,book_name:data.book_name,genre:data.genre,material_sync_status:2})
    }
}


CMD.insert_product = async function(data){
    return await video_product_controllers.createProductData({
        book_platform:data.tg_platform_id,
        product_name:data.book_name,
        genre:data.genre,
        product_id:data.book_id,
        words:data.words
    })
}

CMD.init()