import { NewsItem } from "./news-item";
import { DefaultElement } from "../default-element";
import { GefestHeader } from "../../modules/Gefest/primitives/header";
import { UserType } from "../../api/user-info";
import { GefestElement } from "../../modules/Gefest/element";
import { GefestI } from "../../modules/Gefest/primitives/i";

export function isNewsUI (element : GefestElement): boolean {
    return element instanceof NewsUserInterfaceBody ||
        element instanceof NewsUserInterfaceCard;
}
class NewsUserInterfaceBody extends DefaultElement {
    constructor(userType : UserType) {
        super([]);
        this.addClass("card-body");
        this.addClass("central-navbar");
    }
}
class NewsUserInterfaceCard extends DefaultElement {
    constructor(userType : UserType) {
        super([
            new NewsUserInterfaceBody(userType)
        ]);
        this.addClass("card");
        this.addClass("news-ui");

        this.isHidden = true;
    }
}
// ^^ Card
class NewsUserInterfaceButton extends DefaultElement {
    constructor(userType : UserType, card : NewsUserInterfaceCard) {
        const uiIcon = new GefestI("");
        uiIcon.addClass({
            user: "fas fa-wrench",
            admin: "fas fa-plus",
        }[userType]);

        super([ uiIcon ]);
        this.setAttribute("type", "button");
        this.addClass("news-ui-action");
        this.addClass("btn");
        this.addClass("rounded-circle");
        this.addClass("border");

        this.onClick = () => {
            card.isHidden = !card.isHidden;
            card.update();
        };
    }

    protected wrapHTML(content: string): string {
        const attrString = this.attributesToString();
        return `<button ${attrString}>${content}</button>`;
    }
}
export class NewsUserInterface extends DefaultElement {
    constructor(userType : UserType) {
        const card = new NewsUserInterfaceCard(userType);
        super([
            card,
            new NewsUserInterfaceButton(userType, card),
        ]);
        this.addClass("news-ui-general");
        this.addClass("fixed-bottom");
    }
}

export class NewsFeed extends DefaultElement {
    constructor(items: NewsItem[]) {
        super([
            new NewsUserInterface("user"),
            ...items,
        ]);
    }
}

export interface NewsData {
    id: number;
    title : string;
    content : string;
    source : NewsSource;
    publishedAt : string;
}

export interface NewsSource {
    name: string;
    icon?: string;
    sourceURL?: string;
    itemURL?: string;
}