import { DefaultElement } from "../default-element";
import { GefestHeader } from "../../modules/Gefest/primitives/header";
import { GefestSmall } from "../../modules/Gefest/primitives/small";
import { GefestElement } from "../../modules/Gefest/element";
import { GefestP } from "../../modules/Gefest/primitives/p";

function dateItemFormater (value: number | string, isMonth = false) {
    const MONTHS = [
        "01", "02", "03",
        "04", "05", "06",
        "07", "08", "09",
        "10", "11", "12"
    ];

	if (isMonth) {
		return MONTHS[+value];
	}
	value = value.toString();
	if (value.length === 1) value = '0' + value;
	return value;
}

function formatDate(date: Date) {
    return `${
        dateItemFormater(date.getDate())
    }.${
        dateItemFormater(date.getMonth(), true)
    }.${
        date.getFullYear()
    } в ${
        date.getHours()
    }:${dateItemFormater(date.getMinutes())}`;
}

class CardBodyBS5 extends DefaultElement {
    constructor (content : GefestElement[]) {
        super(content);
        this.addClass("card-body");
    }
}

export class NewsItem extends DefaultElement {
    constructor(title: string, publishDate: Date, content: string) {
        const header = new GefestHeader(title, 2);
        header.addClass("card-title");
        const published = new GefestSmall(formatDate(publishDate));
        published.addClass("published-date");
        const contentParagraph = new GefestP(content);
        contentParagraph.addClass("card-text");

        super([
            new CardBodyBS5([
                header,
                published,
                contentParagraph
            ])
        ]);

        this.addClass("card");
        this.addClass("feed-item");
    }
}