import Replayer from "../domain/replayer";
import RuleRepository from "../domain/ruleRepository";
import TraceRepository from "../domain/traceRepository";
import Word from "../domain/word";

export default class ReplayerApplication {

    public ruleRepository: RuleRepository;
    public traceRepository: TraceRepository;

    constructor(ruleRepository: RuleRepository, traceRepository: TraceRepository) {
        this.ruleRepository = ruleRepository;
        this.traceRepository = traceRepository;
    }

    public initForWebsite(ruleSetName: string, baseURL: string): Replayer {
        const rules = this.ruleRepository.getRulesByName(ruleSetName);
        const replayer = new Replayer(rules, baseURL);
        return replayer;
    }

    public async playTraceSet(traceSetName: string, ruleSetName: string, baseURL: string): Promise<Word[][]> {
        const traces = this.traceRepository.getTraces(traceSetName);
        const replay = this.initForWebsite(ruleSetName, baseURL);
        const executedTraces: Word[][] = [];
        for (const trace of traces) {
            console.log(`${executedTraces.length}/${traces.length} executed` );
            const executedTrace = await replay.executeTrace(trace);
            executedTraces.push(executedTrace);
        }
        return executedTraces;
    }

    /*
    return the list of word successfully executed
    */
    public async playTrace(trace: Word[], ruleSetName: string, baseURL: string): Promise<Word[]> {
        const replay = this.initForWebsite(ruleSetName, baseURL);
        return await replay.executeTrace(trace);
    }

}
