import Structure from "../Structure";
import StructureFactory, {StructureImplConfig} from "../StructureFactory";

export default class QuestionCluster extends Structure{
    public name: string = '';
    public description: string = '';

    constructor(params: Partial<QuestionCluster> = {}) {
        super(params);
        Object.assign(this, params);
    }

    public placeHtml(container: HTMLElement): void {
        const structureDiv = document.createElement("div");
        this.addStandardContainerProperties(structureDiv);
        structureDiv.classList.add("pad-question-cluster");

        const name = document.createElement("span");
        const description = document.createElement("p");
        const questionsDiv = document.createElement("div");

        name.classList.add("pad-name");
        name.innerText = this.name;
        description.classList.add("pad-description");
        description.innerText = this.description;
        questionsDiv.classList.add("pad-questions");

        structureDiv.append(name);
        structureDiv.append(description);
        structureDiv.append(questionsDiv);

        container.append(structureDiv);
        this.placedHtml.push(structureDiv);
        this.contents.forEach(c => c.placeHtml(questionsDiv));
    }
}

export function addQuestionClusterToFactory(factory: StructureFactory): void {
    factory.registerConstructor("questionCluster", (config, params) => {
        const impl = config.impl;
        if (!isQuestionClusterConfig(impl)) return null;

        const clusterParam: Partial<QuestionCluster> = params as Partial<QuestionCluster>;

        clusterParam.name = impl.name;
        clusterParam.description = impl.description;

        return new QuestionCluster(clusterParam);
    });
}

function isQuestionClusterConfig(config:StructureImplConfig): config is QuestionClusterConfig{
    return config.type === "questionCluster";
}

interface QuestionClusterConfig extends StructureImplConfig{
    type: "questionCluster";
    name: string;
    description: string;
}