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> {
        //console.log("refresh view manager");
        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 => {
                        //console.log('probaMap:', probabilityMap);
                        let elementActionListMap = probabilityMap.getActionsForElement(this._application.getRuleService());
                        //console.log('elementActionListMap:', elementActionListMap);
                        this._actionProbabilityView.show(ruleService, elementActionListMap);
                        this._actionPopupView.show(elementActionListMap);

                        this._application.getExplorationEvaluation().then((evaluation) => {
                            this._evaluationActionsBorderView.show(ruleService, evaluation)
                            if (state.displayUserView) {
                                this._application.getCommentList().then((commentList) => {
                                    this._userView.show(commentList, state.confirmedComments, evaluation, state.userTabPosition);
                                })            
                            } else {
                                this._userView.hide();
                            }
                        })
                    })  
                }                    
            })
    }
 
}