Ext.define('ListClass', {
    extend:'Ext.util.Observable',
    constructor:function (cfgXML, o, config) {
        Ext.apply(this, config);
        this.addEvents({
            "actionsLoaded":true,
            "listCompleted":true,
            "listTravelled":true
        });
        this.callParent([]);
        this.IsActionLoaded = false;
        this.ListType = cfgXML.getAttribute('type');
        this.LogicalOrder = new OrderClass(o.NodePath, 'List', o.ElementSequence, o.ResponseID, this.ListType);
        this.id = cfgXML.getAttribute('id');
        this.Actions = new Ext.util.MixedCollection();
        this.loadActions(cfgXML);
    },
    travelList:function () {

        Author.CurrentStateManager.CurrentActionPosition = 0;
        Author.CurrentStateManager.CurrentAction = null;
        Author.CurrentStateManager.CurrentList = this;
        this.Actions.each(function (action, index, length) {
          //  Author.log(Ext.String.format("Deciding processing action: {0}",action.ObjectType));
            Author.CurrentStateManager.CurrentActionPosition = index;
            var processTheAction = false;
            switch (action.ObjectType) {
                case "VARIABLE":
                    processTheAction = true;
                    break;
                case "VISIBLEACTION":
                    processTheAction = true;
                    break;
            }
            if (action.IsDisplayable && (action.IsAddedToForeground || action.IsAddedToBackground)) {
                processTheAction = true;
            }
            if (processTheAction && action.isConditionMatched()) {
              //  Author.log(Ext.String.format('TravelList.ProcessAction: {0}', action.getShortDescription()));
                Author.DisplayManager.processAction(action);
            }
        }, this);
        this.onListTravelled();
    },
    onListTravelled:function () {
        if (this.hasListener('listTravelled')) {
            this.fireEvent('listTravelled', this);
        }
    },
    onStartShowList:function (startActionPosition) {
        if (typeof(startActionPosition) == 'undefined') {
            startActionPosition = 0;
        }
        if (startActionPosition > this.getActionsCount()) {
            Author.log("ListClass.onStartShowList:\nBad startActionPosition: " + startActionPosition);
            return false;
        }
        Author.LessonManager.refreshVariableMonitor();
        Author.CurrentStateManager.CurrentActionPosition = startActionPosition;
        Author.CurrentStateManager.CurrentAction = null;
        Author.CurrentStateManager.CurrentList = this;
        return true;
    },
    onListCompleted:function () {
        Author.LessonManager.refreshVariableMonitor();
        Author.CurrentStateManager.fixZIndexes();
        if (this.hasListener('listCompleted')) {
            this.fireEvent('listCompleted', this);
        }
    },
    getCurrentAction:function () {
        if (Author.CurrentStateManager.CurrentList.id == this.id && Author.CurrentStateManager.CurrentActionPosition < this.getActionsCount()) {
            return this.Actions.getAt(Author.CurrentStateManager.CurrentActionPosition);
        }
        return null;
    },
    getEndListLogicalOrder:function () {
        if (this.getActionsCount() == 0) {
            return this.LogicalOrder;
        }
        else {
            return this.Actions.getAt(this.getActionsCount() - 1).LogicalOrder;
        }
    },
    containsAction:function (action) {
        return this.Actions.containsKey(action.id);
    },
    loadActions:function (cfgXML) {
        var startTime = new Date().getTime();
        if (this.IsActionLoaded) {
            alert("Action Already Loaded");
            return;
        }
        var actionNodes = cfgXML.getElementsByTagName('action');
        for (var i = 0; i < actionNodes.length; i++) {
            var action = this.addNewAction(actionNodes[i], this.LogicalOrder, i);
            if (action) {
                action.ParentList = this;
                this.Actions.add(action.id, action);
            }
        }
        this.IsActionLoaded = true;
        var endTime = new Date().getTime();
        if (this.hasListener('actionsLoaded')) {
            this.fireEvent('actionsLoaded', this, (endTime - startTime));
        }
    },
    addNewAction:function (action, order, actionSequence) {
        var actionType = action.getAttribute('type');


        var newAction = null;
        switch (actionType) {
            case "PROGRESSBAR":
                newAction = new ProgressbarClass15(action, order, actionSequence);
                break;
            case "BACKGROUND":
                newAction = new BackgroundClass15(action, order, actionSequence);
                break;
            case "FLVPLAYERACTION":
                newAction = new FLVVideoClass15(action, order, actionSequence);
                break;
            case "SOUNDACTION":
                newAction = new SoundClass15(action, order, actionSequence);
                break;
            case "PAUSE":
                newAction = new PauseClass15(action, order, actionSequence);
                break;
            case "VARIABLE":
                newAction = new VariableClass15(action, order, actionSequence);
                break;
            case "VISIBLEACTION":
                newAction = new VisibleClass15(action, order, actionSequence);
                break;
            case "WEBACTION":
                newAction = new WebElementClass15(action, order, actionSequence);
                break;
            case "TXTEXTACTION":
                newAction = new TXTextClass15(action, order, actionSequence);
                break;
            case "TEXTACTION":
                newAction = new TextClass15(action, order, actionSequence);
                break;
            case "IMAGEACTION":
                newAction = new ImageClass15(action, order, actionSequence);
                break;
            case "CUSTOMBUTTON":
                newAction = new CustomButtonClass15(action, order, actionSequence);
                break;
            case "DDBUTTON":
                newAction = new DDButtonClass15(action, order, actionSequence);
                break;
            case "TEXTFIELD":
                newAction = new TextFieldClass15(action, order, actionSequence);
                break;
            case "RADIOBUTTON":
                newAction = new RadioButtonClass15(action, order, actionSequence);
                break;
            case "COMBOBOX":
                newAction = new ComboboxClass15(action, order, actionSequence);
                break;
            case "CHECKBOX":
                newAction = new CheckboxClass15(action, order, actionSequence);
                break;
            case "HOTSPOT":
                newAction = new HotSpotClass15(action, order, actionSequence);
                break;
            case "SPECIAL":
                newAction = SpecialClassHelper.loadSpacialAction(action, order, actionSequence);
                break;
            case "DRAWING":
                newAction = new DrawingClass15(action, order, actionSequence);
                break;
            default:
                   //Author.log("ListClass:addNewAction: " + actionType + " is not added to Library");
                return null;
        }
        newAction.ParentList = this;
        Ext.ComponentManager.register(newAction);
        return newAction;
        //if (actionType == "DRAWINGACTION") {
        //   var actionObject = new DrawingClass(action, order);
        //}
        //if (actionType == "FLASHACTION") {
        //    var actionObject = new FlashClass(Author.backgroundObject.getContent(), action, order);
        //}
    },
    removeListActions:function () {
        Author.DisplayManager.removeActionsInList(this);
    },
    getActionsCount:function () {
        return this.Actions.getCount();
    },
    toString:function () {
        var str = Ext.String.format("List ID: {0}\nListType: {1}\nActionCount: {2}", this.id, this.ListType, this.getActionsCount());
        this.Actions.each(function (action, index, length) {
            str += Ext.String.format("\n[{0}]: {1}", index, action.getShortDescription());
        }, this);
        return str;
    }
});
