
import SequenceEvaluator from "../domain/SequenceEvaluator";
import SequenceEvaluatorRepository from "../domain/SequenceEvaluatorRepository";
import Step from "../domain/Step";
import StepFactory from "../domain/StepFactory";

import {EvaluatorDocument, sequenceEvaluatorModel} from "./SequenceEvaluatorSchema";

export default class SequenceEvaluatorRepositoryMongo implements SequenceEvaluatorRepository {

    public getSequenceEvaluatorByWebSiteId(webSiteId: string, stepFactory: StepFactory): Promise<SequenceEvaluator | null> {
        return sequenceEvaluatorModel.findOne({webSiteId})
        .then((sequenceEvaluatorData) => {
            if (sequenceEvaluatorData) {
                const evaluator = new SequenceEvaluator(sequenceEvaluatorData._id, sequenceEvaluatorData.description);
                return Promise.all(sequenceEvaluatorData.stepList
                    .map((stepData) => stepFactory.createStep(stepData.name, stepData.expression, stepData.forbiddenInteractionList)))
                    .then((values) => {
                        const steplist = values as Step[];
                        evaluator.stepList = steplist;
                        return evaluator;
                    });
            }
        })
        .then((evaluator) => {
            if (!evaluator) {
                return null;
            }
            return evaluator;
        });
    }

    public createSequenceEvaluator(webSiteId: string, description: string, stepFactory: StepFactory, stepList: {name: string, expression: string, forbiddenInteractionList: string[]}[]): Promise<SequenceEvaluator> {
        let evaluator;
        console.log(stepList[0].expression, stepList[0].expression.length)
        return sequenceEvaluatorModel.create({
            webSiteId,
            stepList: stepList.filter(step => step.expression && step.expression.length > 0),
            description,
        } as EvaluatorDocument).then((createdSequenceEvaluator) => {
            evaluator = new SequenceEvaluator(createdSequenceEvaluator._id, createdSequenceEvaluator.description);
            return Promise.all(createdSequenceEvaluator.stepList
                .map((stepData) => stepFactory.createStep(stepData.name, stepData.expression, stepData.forbiddenInteractionList)));
        }).then(() => {
            return evaluator;
        })
    }

    public updateSequenceEvaluator(webSiteId: string, description: string, stepList: {name: string, expression: string, forbiddenInteractionList: string[]}[]): Promise <void> {
        stepList = stepList.filter(step => step.expression && step.expression.length > 0)
        return sequenceEvaluatorModel.updateOne({
            webSiteId,
        }, {
            description,
            stepList: stepList.filter(step => step.expression && step.expression.length > 0),
        }).then(() => {return})
    }

    public removeSequenceEvaluator(sequenceEvaluator: SequenceEvaluator): Promise<void> {
        return sequenceEvaluatorModel.remove({_id: sequenceEvaluator.id}).then(() => {return})

    }
}
