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 other_book_controllers = require('../src/data_manager/Controllers/other_book_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 ,tg_platform_id:config.platform_fanqie});
        if(result.success){
            let tg_platform_id = result.data.tg_platform_id
            //判断是否存在这本书
            if(tg_platform_id==config.platform_fanqie){ //番茄
                //获取番茄书库的书
                let fq_book_result = null
                if(result.data.book_id.length>18){
                    fq_book_result =  await get_fq_book_tab_controllers.getDataByBookId({ book_id: result.data.book_id });
                }else{
                    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.info.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.words
                    //获取商品库的商品
                    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)
                        // }
                        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,material_sync_status:2})
                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()
            result = task_queue_item.result
            let other_book = await other_book_controllers.getData({product_id:task_queue_item.result.data.book_id})
            if(other_book.success){
                await filter_data_controllers.updateFilterData({id:result.data.id},{status:2,book_id:other_book.data.product_id,book_name:other_book.data.product_name,material_sync_status:2})
                throw "处理了一条长篇或者筛选不要的书"
            }
            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){
                let tg_platform_id = result.data.tg_platform_id
                if(tg_platform_id==config.platform_fanqie){
                    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']
                    if (Object.keys(obj['novel_data']).length === 0) {
                        result.data.words = 0
                        result.data.genre = 999
                        result.data.book_id = result.data.book_id
                        result.data.book_name = "没有找到这本书"
                    }else{
                        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)
                            // }
                            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;
    }else{
        return 3;
    }
    // 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
}

CMD.getPlatformInfo = 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
        }
    }
}
CMD.getSharedPlatform = function(PlatformShared,platform_shared_id){
    for (let index = 0; index < PlatformShared.length; index++) {
        const element = PlatformShared[index];
        if(element.id==platform_shared_id){
            return JSON.parse(element.info)
        }
    }
}

CMD.update_filter_data = async function(FilterConfig,data) {
    // let isGenre = CMD.isGenre(FilterConfig,data.genre)
    let isGenre = true
    // 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,material_sync_status:0})
        
        //创建一个小程序    
        let AppConfig = await redis_help.getKeyValue("AppConfig")

        AppConfig = JSON.parse(AppConfig)


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

        PlatformShared = JSON.parse(PlatformShared)

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

        PlatformConfig = JSON.parse(PlatformConfig)

        let PlatformInfo = CMD.getPlatformInfo(PlatformConfig,config.platform_fanqie)

        console.log("PlatformInfo:",PlatformInfo,data)

        let all_platform_list = CMD.getSharedPlatform(PlatformShared,PlatformInfo.platform_shared_id)

        for (let i = 0; i < all_platform_list.length; i++) {

            const tg_platform_id = all_platform_list[i];

            let AppInfoList = CMD.getAppInfoByPlatformId(AppConfig,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{
                        let mian_genre = main_info.genre
                        let book_genre = data.genre
                        if(!CMD.isBlackBook(black_books,data.book_id)&&mian_genre==book_genre){
                            //符合标准进行创建表
                            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:config.platform_fanqie,
                                    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 other_book_controllers.createData({product_id:data.book_id,product_name:data.book_name,tg_platform_id:data.tg_platform_id})
        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.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.words
    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.init()