import Interface4TabScriptView from "../application/Interface4ViewManager";
import State from "../domain/State";
import ViewManagerService from "../domain/ViewManagerService";
import ActionProbabilityView from "./ActionProbabilityView";
import ActionsPopupView from "./ActionsPopupView";
import UserView from "./UserView";
import EvaluationActionsBorderView from "./EvaluationActionsBorderView";

export default class ViewManager implements ViewManagerService {
    private _application : Interface4TabScriptView
    private _actionProbabilityView : ActionProbabilityView;
    private _actionPopupView : ActionsPopupView;
    private _userView : UserView;
    private _evaluationActionsBorderView: EvaluationActionsBorderView;

    constructor(application : Interface4TabScriptView) {
        this._application = application;
        this._actionPopupView = new ActionsPopupView();
        this._actionProbabilityView = new ActionProbabilityView();
        this._userView = new UserView(application);
        this._evaluationActionsBorderView = new EvaluationActionsBorderView();
    }

    refresh() : Promise<void> {
        return this._application.getState()
            .then((state: State) => {
                if (!state.isActive) {
                    this._actionPopupView.hide();
                    this._actionProbabilityView.hide();
                    this._userView.hide();
                    return Promise.resolve();
                } else {
                    const ruleService = this._application.getRuleService();
                    return this._application.getProbabilityMap()
                    .then(probabilityMap => {
                        let elementActionListMap = probabilityMap.getActionsForElement(this._application.getRuleService());
                        this._actionProbabilityView.show(ruleService, elementActionListMap);
                        this._actionPopupView.show(elementActionListMap);
                        if (state.displayTab) {
                            return Promise.all([this._application.getCommentList(), this._application.getExplorationEvaluation()])  
                                .then(([commentList, evaluation]) => {
                                    console.log("then")
                                    this._userView.show(commentList, state.confirmedComments, evaluation, state.userTabPosition);
                                    this._evaluationActionsBorderView.show(ruleService, evaluation);
                                })            
                        }
                    })  
                }                    
            })
    }
 
}