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 get_fq_book_tab_controllers = require('../src/data_manager/Controllers/get_fq_book_tab_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 HttpClient = require('../src/HttpClient');
const helper = require('../src/helper');
const tools = require('../tools');
const time_count = 2000;
const task_queue = []

async function processTask(){
    let result = null
    try{
        let isPauseTask =  await redis_help.getKeyValue("isPauseTask")
        if(isPauseTask=="true"){
            return
        }
        let isInsertTask = false
        let FilterConfig = await redis_help.getKeyValue("FilterConfig")
        FilterConfig = JSON.parse(FilterConfig)
        result = await filter_data_controllers.getFilterDataByStatus({ status: 0 });
        if(result.success){
            let tg_platform_id = result.data.tg_platform_id
            //判断是否存在这本书
            if(tg_platform_id==1){
                let hy_book_result = await video_product_controllers.getProductData({ book_id: result.data.book_id });
                if(hy_book_result.success){
                    result.data.words = hy_book_result.data.words
                    result.data.genre =  hy_book_result.data.genre
                    result.data.book_id = hy_book_result.data.product_id
                    result.data.book_name = hy_book_result.data.product_name
                }else{
                    isInsertTask = true
                }
            }else if(tg_platform_id==2){ //番茄
                //获取番茄书库的书
                let fq_book_result = await get_fq_book_tab_controllers.getData({ book_id: result.data.book_id });
                if(fq_book_result.success){//如果番茄书库里有这本书
    
                    result.data.genre = fq_book_result.data.genre
                    result.data.book_id = fq_book_result.data.book_id
                    result.data.book_name = fq_book_result.data.info.book_name
                    result.data.words = fq_book_result.data.info.word_number
                    //获取商品库的商品
                    let product_result = await video_product_controllers.getProductData({ book_id: fq_book_result.data.book_id });
                    //如果商品库没有这个商品
                    if(!product_result.success){ //如果只是 get_fq_book 有， 书库里没有
                        fq_book_result.data.tg_platform_id = result.data.tg_platform_id
                        //如果是长篇短篇符合
                        if(CMD.isGenre(FilterConfig,result.data.genre)){
                            await CMD.insert_product(result.data)
                        }
                    }
                }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, 500);
    }

}

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==1){
                    let hy_book_data = await require('./collect_hy_book').runTask(result.data)
                    if(CMD.isGenre(FilterConfig,hy_book_data.genre)){
                        await CMD.insert_product(hy_book_data)
                    }
                    result.data.words = hy_book_data.words
                    result.data.genre = hy_book_data.genre
                    result.data.book_id = hy_book_data.book_id
                    result.data.book_name = hy_book_data.book_name
                }else if(tg_platform_id==2){
                    let client =  new HttpClient()
                    let sid_tt = await tools.getFqSidtt()
                    let response =  await client.proxyRequest( helper.getFqRequestOpt(result.data.book_id,sid_tt))
                    let obj = response['data']
                    let book_name =  obj['novel_data'].book_name
                    let book_id = obj['novel_data'].book_id
                    let genre = CMD.getFqGenre(obj['novel_data'].genre)
                    let words = obj['novel_data'].word_number
                    let fq_book_data = {book_name:book_name,book_id:book_id,genre:genre,words:words,info:JSON.stringify(obj['novel_data'])}
                    let fq_book = {} 
                    fq_book.key = result.data.book_id
                    // console.log("response:",obj,sid_tt)
                    await CMD.add_fq_book(fq_book,fq_book_data)
                    let product_result = await video_product_controllers.getProductData({ book_id: fq_book_data.book_id });
                    if(!product_result.success){ //如果书库也没有
                        fq_book.tg_platform_id = result.data.tg_platform_id
                    
                        if(CMD.isGenre(FilterConfig,fq_book_data.genre)){
                            fq_book_data.tg_platform_id = result.data.tg_platform_id
                            await CMD.insert_product(fq_book_data)
                        }
                    }
                    result.data.words = words
                    result.data.genre = fq_book_data.genre
                    result.data.book_id = fq_book_data.book_id
                    result.data.book_name = fq_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.getFqGenre = function(genre){
    if(genre=="0"){
        return 1;
    }
    if(genre=="8"){
        return 3;
    }

    if(genre=="202"){
        return 202;
    }
    return 999;
}

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
}

function deepCopy3(obj) {
    return JSON.parse(JSON.stringify(obj));
}

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){
        try{
            await CMD.video_product_material(data)
        }catch(e){
            console.log("video_product_material error:",e)
        }
        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})
    }
}

CMD.video_product_material = async function(data){
    let url = config.isDebug?config.debug_supdate_config.host:config.release_supdate_config.host 
    url = url+'/sapi/video_product_material/supdate';
    const formData = new FormData();
    formData.append('data',JSON.stringify({
        dy_id:data.video_id,
        dy_url:data.video_link,
        product_id:data.book_id,
        product_name:data.book_name,
        d_z_number:data.like_num,
        collect_number:data.kepp_num,
        comment_number: data.comment_num,
        forward_number: data.shared_num,
        book_platform: data.tg_platform_id,
        title: data.title,
        yun_fabu_time:data.publish_time
    }))
    return await axios.post(url, formData, {
        headers: {
            'Content-Type': 'multipart/form-data'
        }
    })
    .then(response => {
        if(response.data.code===10000){
            console.log("插入任务表成功:", response.data);
        }else{
            console.log("插入任务表失败:", formData);
        }

    })
    .catch(error => {
        console.error("插入任务表失败:", error,formData);
    });
}

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.add_fq_book = async function(data,response){
    let book_name =  response.book_name
    let book_id = response.book_id
    let genre = response.genre
    let words = response.word_number
    data.genre = 0
    if(genre=="0"){
        data.genre = 1;
    }
    if(genre=="8"){
        data.genre = 3;
    }

    if(genre=="202"){
        data.genre = 202;
    }
    data.words = words
    data.book_name = book_name
    data.book_id = book_id
    let tab = {
        book_key:data.key,
        book_id:data.book_id,
        info:JSON.stringify(response)
    }
    if(tab.book_id==undefined){
        throw "tab.book_id==undefined"
    }
    // console.log("tab:",tab,data)
    return await get_fq_book_tab_controllers.createData( tab)
}


CMD.update_video_applet_product = function(data){
     sql_task_help.update('UPDATE video_applet_product SET status = ?, wait_status = ?  , check_url = ?   WHERE promotion_id = ? and product_id = ?', 
        [1,1,data.check_url,data.promotion_id,data.product_id],
        ()=>{
            console.log("update_video_applet_product:更新成功！")
        })
}
CMD.init()