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

import {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);
                return Promise.all(sequenceEvaluatorData.stepList.map((stepData) => stepFactory.createStep(stepData.name, stepData.expression, stepData.fobiddenInteractionList)))
                    .then((values) => {
                        const steplist = values as Step[];
                        evaluator.stepList = steplist;
                        return evaluator;
                    });
            }
        })
        .then((evaluator) => {
            if (!evaluator) {
                return null;
            }
            return evaluator;
        });
    }

    public createSequenceEvaluator(webSiteId: string, stepFactory: StepFactory, stepList: Array<{name: string, expression: string, forbiddenInteractionList: string[]}>): Promise<SequenceEvaluator> {
        let evaluator;
        return sequenceEvaluatorModel.create({
            webSiteId,
            stepList,
        }).then((createdSequenceEvaluator) => {
            evaluator = new SequenceEvaluator(createdSequenceEvaluator._id);
            return Promise.all(createdSequenceEvaluator.stepList.map((stepData) => stepFactory.createStep(stepData.name, stepData.expression, stepData.forbiddenInteractionList)));
        }).then(() => {
            return evaluator;
        })
    }

    public updateSequenceEvaluator(webSiteId: string, stepList: Array<{name: string, expression: string, forbiddenInteractionList: string[]}>): Promise <void> {
        return sequenceEvaluatorModel.update({
            webSiteId,
        }, {
            stepList,
        })
    }

    public removeSequenceEvaluator(sequenceEvaluator: SequenceEvaluator): Promise<void> {
        return sequenceEvaluatorModel.removeOne({_id: sequenceEvaluator.id});
    }

}
