const origin_data_controllers = require('../src/data_manager/Controllers/origin_data_controllers');
const redis_help = require('../src/use_redis');
async function processTask(){
    const result = await origin_data_controllers.getOriginDataByStatus({status:1})
    if(result.success){
        let FilterConfig = await redis_help.getKeyValue("FilterConfig")
        FilterConfig = JSON.parse(FilterConfig)
        let d =  filter_and_insert(result.data,FilterConfig)
        if(d==null){
            origin_data_controllers.updateOriginData({id:result.data.id},{
                status:3
            })
            setTimeout(processTask,1000);
        }else{
            if(d.channel=="七猫"){
                setTimeout(processTask,1000);
            }else{
                console.log("result:",result.data)
            }
        }
 
    }
}
let main = async function(){
    setTimeout(processTask,1000);
}

function filter_and_insert(origin_data,tg_config){

    if(origin_data.guajian_link.length<=0){
        return null
    }

    let obj = parsePromotion(origin_data.guajian_link,tg_config.source_filter,tg_config.platform_select)
    // console.log("obj::",obj)
    if(obj==null){
        console.log("xml[挂件地址] error 2:",obj)
        return null
    }
    let tg_platform_id = 0
    if(obj.channel=="七猫"){
        tg_platform_id = 3
        obj.book_id = obj.key
    }else if(obj.channel=="点众"){
        tg_platform_id = 4
    }
    // console.log("评论数正确")
    return {video_id:origin_data.video_id,book_id:obj.book_id,channel:obj.channel,source:obj.source,key:obj.key,video_link:origin_data.video_link,
        like_num:origin_data.like_num,kepp_num:origin_data.kepp_num ,shared_num:origin_data.shared_num,comment_num:origin_data.comment_num,tg_platform_id:tg_platform_id}
}


//解析挂件
function parsePromotion(promotion,source_list,platform_select){
    let list = promotion.split('&')
    if(list.length!=undefined){
        if(list.length>2){
            let params = {}
            for (let index = 0; index < list.length; index++) {
                const element = list[index];
                let obj = parsePromotionItem(element)
                if(obj!=null){
                    if(obj.key=="start_page"&&obj.bookId!=null&&obj.bookId!=undefined){
                        params['bookId'] = obj.bookId
                    }
                    params[obj.key] = obj.value
                }
            }
            let channel = getChannel(params['start_page'],platform_select,list)
            // console.log("source_list:",source_list,platform_select,channel)
            if(channel!=null){
                let item_info = {}
                item_info['book_id'] = params['bookId'];
                item_info['channel'] = channel;

                const str = params['x-signature']
                const regex = /"name":"([^"]+)"/;
                const match = str.match(regex);
                let source = ''
                if (match && match[1]) {
                    source = match[1]
                }
                for (let j = 0; j < source_list.length; j++) {
                    const source_item = source_list[j];
                    if(source_item==source){
                        return null
                    }
                }
                item_info['source'] = source;
                item_info['key'] = params['key']
                return item_info
            }
            return null
        }else{
            return null
        }
    }else{
        return null
    }
}

function getChannel(start_page,platform_select,list){
    console.log("start_page:",start_page)
    if(start_page=="pages/reader/index?bookId"){ //点众
        return "点众"
    }else if(start_page=="channel=rd"){ //七猫
        return "七猫"
    }else if(start_page=="pages/bookstore/index?aid"){
        console.log("移除番茄")
        return null
    }else if(start_page=="pages/readPage/chapter?bookId"){
        console.log("移除黑岩")
        return null
    }else if(start_page=="pages/index/index?clickid"){
        console.log("未知：",list)
    }
    console.log("未知2：",list)
    return null
}

function parsePromotionItem(item){
    let list = item.split('=')
    if(list.length>1){
        let key = list[0]
        let value = list[1]
        if(key==="start_page"&&list.length>2){
            return {key:key,value:value,bookId:list[2]}
        }
        return {key:key,value:value}
    }
    return null
}

redis_help.connect(()=>{
    main()
})