import Comment from "./Comment";
import Action from "./Action";
import Answer from "./Answer";

export default class Exploration {

    startDate : Date;
    stopDate : Date;

    interactions: (Action | Comment | Answer)[];

    constructor() {
        this.interactions = [];
    }

    get length(): number {
        return this.interactions.length;
    }

    get isEmpty(): boolean {
        return this.interactions.length <= 1;
    }

    get actions(): Action[] {
        return this.interactions.filter(interaction => interaction instanceof Action) as Action[];
    }

    get evaluableInteractions(): (Action | Answer)[] {
        return this.interactions.filter(interaction => interaction instanceof Action || interaction instanceof Answer) as (Action | Answer)[];

    }

    addAction(kind: string, value?: string): void {
        this.interactions.push(new Action(kind, value, this.interactions.length));
    }

    addAnswer(text: string, value: string): void {
        this.interactions.push(new Answer(text, value, this.interactions.length))
    }

    addComment(kind: string, value: string) : void{
        this.interactions.push(new Comment(kind, value, this.interactions.length));
    }

    reset(): void {
        this.interactions = [];
        this.startDate = new Date();
    }

    stop(): void {
        this.stopDate = new Date();
    }

    interactionsToJSON() {
        return this.interactions.map(interaction => {
            if (interaction instanceof Comment) {
                return (
                    {
                        concreteType: "Comment",
                        kind: interaction.kind,
                        value: interaction.value,
                        date: interaction.date
                    }
                )
            } else if (interaction instanceof Action) {
                return (
                    {
                        concreteType: "Action",
                        kind: interaction.kind,
                        value: interaction.value,
                        date: interaction.date
                    }
                )
            }
            else if (interaction instanceof Answer) {
                return (
                    {
                        concreteType: "Answer",
                        kind: interaction.kind,
                        value: interaction.value,
                        date: interaction.date
                    }
                )
            }
        })
    }

}