const CMD = {}
const time_count = 2000;
const redis_help = require('../src/use_redis');
const video_applet_product_controllers = require('../src/data_manager/Controllers/video_applet_product_controllers');
var hy_create_link_task_list = []
var fq_create_link_task_list = []
let finish_call_back = async function(new_data,error_info=null){
    try{
        if(new_data.start_page != null && error_info==null){
            const result = await video_applet_product_controllers.updateData({id:new_data.id},{
                dy_small_program_start:new_data.start_page,
                dy_small_program_start_data:new_data.start_param,
                promotion_id:new_data.promotion_id,
                wait_status:1,
                status:0
            })
        }else{
            const result = await video_applet_product_controllers.updateData({id:new_data.id},{
                status:2,
                wait_status:2,
                error_info:JSON.stringify(error_info)
            })
        }
  
    }catch(e){
        console.log("finish_call_back error:",e)
    }
   
}
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
}
async function processTask(){
    try{
        let MainConfig = await redis_help.getKeyValue("MainConfig")
        MainConfig = JSON.parse(MainConfig)
        let PlatformConfig = await redis_help.getKeyValue("PlatformConfig")
        PlatformConfig = JSON.parse(PlatformConfig)


        let black_books = await redis_help.getKeyValue("all_black_books")
        
        if(black_books!=null){
            black_books = JSON.parse(black_books)
        }
       
        let result = await video_applet_product_controllers.getAppletProductDataByStatus({status:0,wait_status:0})

        if(result.success){

            if(CMD.isBlackBook(black_books,result.data.product_id)){
                await video_applet_product_controllers.updateData({id:result.data.id},{
                    status:2,
                    wait_status:2,
                    error_info:JSON.stringify({msg:"当前书在黑名单"})
                })
                throw "当前书在黑名单"
            }

            let PlatformInfo = null
            PlatformInfo = CMD.getPlatformInfoById(PlatformConfig,result.data.book_platform)
            if(PlatformInfo==null){
                await video_applet_product_controllers.updateData({id:result.data.id},{
                    status:2,
                    wait_status:2,
                    error_info:JSON.stringify({msg:"平台配置错误："})
                })
                console.error("平台配置错误：",result)
                throw "平台配置错误："
            }
            let main_info = CMD.getMainInfoById(MainConfig,result.data.main_id)
            if(main_info==null){
                await video_applet_product_controllers.updateData({id:result.data.id},{
                    status:2,
                    wait_status:2,
                    error_info:JSON.stringify({msg:"主体配置错误："})
                })
                console.error("主体配置错误：",result)
                throw "主体配置错误："
            }
            if(result.data.book_platform==1){
                hy_create_link_task_list.push({
                    n_data:result.data,
                    main_info:main_info,
                    PlatformInfo:PlatformInfo
                })
            }else if(result.data.book_platform==2){
                fq_create_link_task_list.push({
                    n_data:result.data,
                    main_info:main_info,
                    PlatformInfo:PlatformInfo
                })
            }
        }

        if(hy_create_link_task_list.length>0){
            let hy_create_link_task = hy_create_link_task_list.pop()
            require('./hy_create_link').runTask(hy_create_link_task.n_data
                ,hy_create_link_task.main_info,
                hy_create_link_task.PlatformInfo,finish_call_back)
        }

        if(fq_create_link_task_list.length>0){
            let fq_create_link_task = fq_create_link_task_list.pop()
            require('./fq_create_link').runTask(fq_create_link_task.n_data
                ,fq_create_link_task.main_info,
                fq_create_link_task.PlatformInfo,finish_call_back)
        }
    }catch(e){
        console.log("CREATE_LINK_FACTORY error:",e)
        finish_call_back(null,{"MESSAGE":"CREATE_LINK_FACTORY error:"})
    } finally{
        global.setTimeout(processTask,time_count)
    }
}
CMD.init = function(){
    redis_help.connect((results)=>{
        if(results){
        }
    })
    processTask()
}



CMD.create_link = function(n_data){
    if(n_data.book_platform==1){
        hy_create_link_task_list.push({
            n_data:n_data,
            main_info:main_info,
            PlatformInfo:PlatformInfo
        })
    }else if(n_data.book_platform==1){
        fq_create_link_task_list.push({
            n_data:n_data,
            main_info:main_info,
            PlatformInfo:PlatformInfo
        })
    }
}
CMD.getPlatformInfoById = function(PlatformConfig,tg_platform_id){
    for (let index = 0; index < PlatformConfig.length; index++) {
        const element = PlatformConfig[index];
        if(element.tg_platform_id==tg_platform_id){
            return element
        }
    }
    return null
}

CMD.getMainInfoById = function(MainConfig,main_id){
    // console.log("MainConfig:",MainConfig)
    for (let index = 0; index < MainConfig.length; index++) {
        const element = MainConfig[index];
        if(element.id==main_id){
            return element
        }
    }
    return null
}
CMD.init()