Ext.define('CurrentStateManager', {
    constructor:function () {
        this.CurrentActionPosition = -1;
        this.CurrentElementPosition = -1;
        this.CurrentAction = null;
        this.CurrentList = null;
        this.CurrentResponse = null;
        this.CurrentElement = null;
        this.CurrentElementTimeoutTimer = null;
        this.CurrentLessonNode = null;
        this.CurrentActionList = new Ext.util.MixedCollection();
    },
    hasAction:function (actionID) {
        return this.CurrentActionList.containsKey(actionID);
    },
    addAction:function (actionObject) {
        if (this.hasAction(actionObject.id)) {
            Author.log(actionObject + ", action already exist.");
            return;
        }
        this.CurrentActionList.add(actionObject.id, actionObject);
    },
    removeAction:function (actionID) {
        if (this.hasAction(actionID)) {
            var removedAction = this.CurrentActionList.removeAtKey(actionID);
            removedAction.remove();
            return;
        }
        Author.log("Action [" + actionID + "] does not exist.");
    },
    getActionByID:function (actionID) {
        if (this.hasAction(actionID)) {
            return this.CurrentActionList.getByKey(actionID);
        }
        Author.log("CurrentStateManager.getActionByID:\nAction [" + actionID + "] does not exist.");
        return null;
    },
    getActionByName:function (actionName) {
        var found = null;
        this.CurrentActionList.each(function (action, index, length) {
           // Author.log("Name: "+action.Name.toLowerCase() +" vs "+actionName);
            if (action.Name.toLowerCase() == actionName.toLowerCase()) {
               // Author.log("Found Name: "+action.Name.toLowerCase() +" vs "+actionName);
                found = action;
                return false;
            }
        });
        return found;
    },
    getCurrentElementUserAnswer:function () {
        if (this.CurrentElement) {
            return this.CurrentElement.getGeneralAnswer();
        }
        return null;
    },
    getCurrentElementCorrectAnswer:function () {
        if (this.CurrentElement && typeof(this.CurrentElement.getCorrectAnswers) == 'function') {
            return this.CurrentElement.getCorrectAnswers();
        }
        return null;
    },
    getCurrentElementResult:function () {
        if (this.CurrentElement && typeof(this.CurrentElement.isAnswerCorrect) == 'function') {
            return this.CurrentElement.isAnswerCorrect();
        }
        return null;
    },
    resetList:function (interactionList) {
        interactionList.Actions.each(function (action, index, length) {
            if (typeof(action.reset) == 'function') {
                action.reset();
            } else {
                Author.log(Ext.String.format("{0}: Reset function is not defined.", action.ObjectType));
            }
        });
    },
    getCurrentActions:function () {
        var resultStr = "CurrentActionList:\n";
        this.CurrentActionList.each(function (action, index, length) {
            resultStr += ("[" + index + "]: " + action.getShortDescription() + "\n");
        }, this);
        return resultStr;
    },
    fixZIndexes:function () {
        this.CurrentActionList.sortBy(function (actionA, actionB) {
            return actionA.LogicalOrder.compareTo(actionB.LogicalOrder);
        });
        this.CurrentActionList.each(function (action, index, length) {
            if (action.IsDisplayable) {
                var zFix = 0;
                switch (action.ParentList.ListType) {
                    case "FOREGROUNDLIST":
                        zFix = 5000;
                        break;
                    case  "MENUINTERACTION":
                    case "INTERACTION"  :
                        zFix = 500;
                        if (action.ObjectType == 'DDBUTTON') {
                            zFix = 1000;
                        }
                        break;
                    case "RESPONSE" :
                        zFix = 3000;
                }

                action.setZIndex(index + 1 +zFix);
            }
        }, this);
    },
    getCurrentActionCount:function () {
        return this.CurrentActionList.getCount();
    },
    removeNonBackgroundOrForegroundActions:function () {
        for (var i = this.getCurrentActionCount() - 1; i >= 0; i--) {
            var action = this.CurrentActionList.getAt(i);
            if (!action.IsAddedToBackground && !action.IsAddedToForeground) {
                action.remove();
                this.CurrentActionList.removeAt(i);
                //alert("removeNonBackgroundOrForegroundActions:\n" + action);
            }
        }
    },
    removeActionsNotBeforeOrder:function (order) {
        for (var i = this.getCurrentActionCount() - 1; i >= 0; i--) {
            var action = this.CurrentActionList.getAt(i);
            var compareResult = action.LogicalOrder.compareTo(order);
            if (compareResult >= 0) {
                action.remove();
                this.CurrentActionList.removeAt(i);
                //alert("removeActionsNotBeforeOrder:\n" + action);
            }
        }
    },
    removeActionsInList:function (list) {
        for (var i = this.getCurrentActionCount() - 1; i >= 0; i--) {
            var action = this.CurrentActionList.getAt(i);
            if (list.containsAction(action)) {
                action.remove();
                this.CurrentActionList.removeAt(i);
                //alert("removeActionsInList:\n" + action);
            }
        }
        this.fixDisplayableActionsVisibility(list.LogicalOrder);
        this.fixChangeActions(list.LogicalOrder);
    },
    fixDisplayableActionsVisibility:function (order) {
        this.CurrentActionList.each(function (action, index, length) {
            if (action.IsDisplayable) {
                action.fixVisibilityAtOrder(order);
            }
        }, this);
    },
    fixChangeActions:function (order) {
        //Author.log("CurrentStateManager.fixChangeActions:\n" + order);
        this.CurrentActionList.each(function (action, index, length) {
            if (action.IsDisplayable && !action.IsChangeAction && action.isChangedByAnotherAction()) {
                action.fixActionChangeAtOrder(order);
            }
        }, this);
    },
    updateProgressBar:function () {
        this.CurrentActionList.each(function (action, index, length) {
            if (action.ObjectType == 'PROGRESSBAR') {
                var value = 100;
                switch (this.CurrentLessonNode.Type) {
                    case 'SEQUENCE':
                        value = ((Author.CurrentStateManager.CurrentElementPosition + 1) * 100 / Author.CurrentStateManager.CurrentLessonNode.getElementCount());
                        break;
                    case 'RANDOM':
                        value = ((Author.CurrentStateManager.CurrentElementPosition + 1) * 100 / Author.CurrentStateManager.CurrentLessonNode.getElementCount());
                        break;
                }
                action.setValue(value);
                return false;
            }
        }, this);
    },
    setAnswerBasedOnInteraction:function (selectedName, selectedValue, answer) {
        Author.VariableManager.setSystemVariableValue(AuthorSystemVariables.SELECTEDNAME, selectedName);
        Author.VariableManager.setSystemVariableValue(AuthorSystemVariables.SELECTEDVALUE, selectedValue);
        Author.VariableManager.setSystemVariableValue(AuthorSystemVariables.ANSWER, answer);
    },
    isNagivationToResponseAvailable:function () {
        if (!this.isCurrentResponseFinished()) {
            Author.log("Another response is still not finished:\n" + this.CurrentResponse);
            return false;
        }
        if (this.CurrentList.ListType.search(/interaction/gi) < 0) {
            Author.log("Not in interaction list" );
            return false;
        }
        /*if (this.CurrentElement) {
            if (this.CurrentElement.getResponsesCount) {
                return this.CurrentElement.getResponsesCount() > 0;
            }
            return false;
        }
        if (this.CurrentLessonNode) {
            if (this.CurrentLessonNode.getResponsesCount) {
                return this.CurrentLessonNode.getResponsesCount() > 0;
            }
            return false;
        }*/
        return true;
    },
    canRepeat:function () {
        return this.CurrentElement || this.CurrentLessonNode;
    },
    checkAndShowResponse:function (selectedName, selectedValue, answer) {
        if (arguments.length != 3) {
            Author.log("CurrentStateManager.checkAndShowResponse:\nBad Arguments");
            return;
        }
        if (!this.isCurrentResponseFinished()) {
            Author.log("Another response is still not finished:\n" + this.CurrentResponse);
            return;
        }
        if (this.CurrentList.ListType.search(/interaction/gi) < 0) {
            Author.log("Should not go to response. Not in interactionlist.");
            return;
        }
        this.setAnswerBasedOnInteraction(selectedName, selectedValue, answer);
        if (this.CurrentElement && this.CurrentElement.Type != 'INFORMATION') {
            this.CurrentElement.checkAndShowResponse();
            return;
        }
        if (this.CurrentLessonNode && this.CurrentLessonNode.Type == 'MENU') {
            this.CurrentLessonNode.checkAndShowResponse();
            return;
        }
        Author.log("CurrentStateManager.checkAndShowResponse:\nResponse not available.");
    },
    isCurrentResponseFinished:function () {
        if (this.CurrentResponse) {
            return this.CurrentResponse.IsFinished;
        }
        return true;
    }
});
