import Action from "./Action";
import ConnectionCode from "./ConnectionCode";
import ExplorationEvaluation from "./ExplorationEvaluation";
import PopupAction from "./PopupAction";
import WebSite from "./Website";

export default class StateForPopup {
    public connection : ConnectionCode | undefined;
    public followedConnection : ConnectionCode | undefined;
    public isRecording : boolean;
    public testerName : string;
    public isTabScriptDisplayingUserView : boolean;
    public hasBaseURL : boolean;
    public managedWindowId : number | undefined;
    public interactionList : string[];
    public isPreparedToRecordMedia : boolean;
    public shouldCreateNewWindowsOnConnect : boolean;
    public shouldCloseWindowOnDisconnect : boolean;
    public popupIsDetached: boolean;
    public evaluatorScenario: string | undefined;
    public evaluation: {
        validated: boolean
        enteringInteractionList: Action[]
        continuingActionList: Action[]
        finishingInteractionList: Action[]
    } | undefined;

    constructor() {
        this.isRecording = false;
        this.isTabScriptDisplayingUserView = false;
        this.hasBaseURL = false;
        this.interactionList = [];
        this.testerName = "MyTesterName";
        this.shouldCreateNewWindowsOnConnect = false;
        this.shouldCloseWindowOnDisconnect = false;
        this.isPreparedToRecordMedia = false;
        this.popupIsDetached = false;
    }

    public readEvaluation(evaluation: ExplorationEvaluation, webSite: WebSite): void {
        if (webSite === undefined || evaluation === undefined) {
            return;
        }
        this.evaluation = {
            validated: evaluation.isAccepted,
            continuingActionList: evaluation.continuingActionList.map(action => this.createPopupAction(action, webSite)),
            enteringInteractionList: evaluation.enteringInteractionList.map(action => this.createPopupAction(action, webSite)),
            finishingInteractionList: evaluation.finishingInteractionList.map(action => this.createPopupAction(action, webSite))
        };
    }

    private createPopupAction(action: Action, webSite: WebSite): PopupAction {
        const descriptions =  webSite.mappingList
        .filter(mapping => mapping.output.prefix === action.kind)
        .filter(rule => rule.description !== undefined)
        .map(rule => rule.description)
        return new PopupAction(action.kind, action.value, descriptions)
    }
}

