import html, { HTMLElement } from "node-html-parser";
import request from "request-promise-native";
import { db } from "../../db";
import { Response } from "request";
import { INewsItem, IAggregator } from "./aggregator";
const kissDiaryRoot = "https://com3d2.world/r18/notices.php";
async function aggregate() {
    let lastDiary = db.get("latestCom3D2WorldDiaryEntry").value() as number;
    
    try {
        let mainPageRes = await request(kissDiaryRoot, {resolveWithFullResponse: true}) as Response;
        
        if(mainPageRes.statusCode != 200)
            return [];
        let rootNode = html.parse(mainPageRes.body, {
                pre: true,
                script: false,
                style: false
        });
        if(!(rootNode instanceof HTMLElement))
            return;
        let diaryEntries = rootNode.querySelectorAll("div.frame a");
        if(!diaryEntries) {
            console.log("[COM3D2 WORLD BLOG] Failed to find listing!");
        }
        let result : INewsItem[] = [];
        let latestEntry = lastDiary;
        for(let a of diaryEntries) {
            if(!a.rawAttributes.id)
                continue;
            
            let id = +a.rawAttributes.id;
            if(id <= lastDiary)
                continue;
            if(id > latestEntry)
                latestEntry = id;
            let diaryLink = `${kissDiaryRoot}?no=${id}`;
            let res = await request(diaryLink, {resolveWithFullResponse: true}) as Response;
            if(res.statusCode != 200)
                continue;
            let node = html.parse(res.body, {
                pre: true,
                script: false,
                style: false
            });
            if(!(node instanceof HTMLElement))
                continue;
            let title = node.querySelector("div.frame div.notice_title th");
            let contents = node.querySelectorAll("div.frame div")[1];
            result.push({
                id: `com3d2-world-notices-${id}`,
                link: diaryLink,
                title: title.text,
                author: "com3d2.world",
                contents: contents.outerHTML,
                embedColor: 0xa39869
            });
        }
        db.set("latestCom3D2WorldDiaryEntry", latestEntry).write();
        return result;
    } catch(err) {
        return [];
    }
}
export default {
    aggregate: aggregate
} as IAggregator;