import { INewsItem, IAggregator } from "./aggregator";
import { getRepository } from "typeorm";
import { AggroNewsItem } from "@shared/db/entity/AggroNewsItem";
import cheerio from "cheerio";
import { logger } from "src/logging";
import got from "got";

const urlPattern = /diary\.php\?no=(\d+)/i;
const kissDiaryRoot = "http://www.kisskiss.tv/kiss";
const FEED_NAME = "kisskisstv-diary";

async function aggregate() {
    const repo = getRepository(AggroNewsItem);

    let lastPost = await repo.findOne({
        select: [ "newsId" ],
        where: { feedName: FEED_NAME },
        order: { newsId: "DESC" }
    });

    if(!lastPost)
        lastPost = repo.create({
            newsId: 0
        });
    
    try {
        const mainPageRes = await got.get(`${kissDiaryRoot}/diary.php`);
        
        if(mainPageRes.statusCode != 200) {
            logger.error("[KISS DIARY] Failed to load page. Got response code: %s", mainPageRes.statusCode);
            return [];
        }

        const rootNode = cheerio.load(mainPageRes.body);
        const diaryEntryNames = rootNode("table.blog_frame_top");
        
        if(diaryEntryNames.length == 0) {
            logger.error("[KISS DIARY] Failed to find listing!");
            return [];
        }

        const diaryTexts = rootNode("div.blog_frame_middle");
        const items = diaryEntryNames.map((i, e) => ({ table: e, content: diaryTexts.get(i) }));
        const result : INewsItem[] = [];
        let latestEntry = lastPost.newsId;

        for(const {table, content} of items.get() as {table: CheerioElement, content: CheerioElement}[]) {
            const a = cheerio(table).find("a");
            const link = a.attr("href");
            const matches = link ? urlPattern.exec(link) : false;
            if(!matches)
                continue;
            
            const id = +matches[1];
            if(id <= lastPost.newsId)
                continue;

            if(id > latestEntry)
                latestEntry = id;

            const diaryLink = `${kissDiaryRoot}/${link}`;
            const contentCh = cheerio(content);
            const title = a.text();
            
            const bottomFrame = contentCh.find("div.blog_data");
            bottomFrame.remove();

            result.push({
                newsId: id,
                feedId: FEED_NAME,
                link: diaryLink,
                title: title,
                author: "KISS BLOG",
                contents: contentCh.html() ?? "",
                embedColor: 0xf4c100,
                needsTranslation: true
            });
        }

        return result;
    } catch(err) {
        logger.error("Failed to parse KISS Diary news: %s", err);
        return [];
    }
}

export default {
    aggregate: aggregate
} as IAggregator;