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

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

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

    public readEvaluation(evaluation: ExplorationEvaluation, webSite: WebSite): void {
        if (webSite === undefined || evaluation === undefined) {
            return;
        }
        this.evaluation = {
            validated: evaluation.notValidatedStepList.length === 0,
            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 {
        if (webSite === undefined) {
            return null;
        }
        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)
    }
}

