
let ViewManager = (
    function()
    {
        var _viewArray = [];
        var _viewMap = {};

        return {
            registerView(viewName,viewNode) {
                _viewMap[viewName] = viewNode;
            },

            replaceView(viewName,hideViewName) { 
                if (!_viewMap[viewName]) {
                    return null;
                }
                if (hideViewName) {
                    var view1 = _viewMap[hideViewName];
                    if (view1) {
                        for (var i = 0; i < _viewArray.length; i++) {
                            if (_viewArray[i].nodeName == hideViewName) {
                                _viewArray[i].nodeView.active = false;
                                _viewArray.splice(i, 1);
                                break;
                            }
                        } 
                    }
                }
                var view2 = _viewMap[viewName];
                if (_viewArray.length > 0) {
                    var i = 0;
                    var viewShowed = false;
                    for (i = 0; i < _viewArray.length; i++) {
                        if (_viewArray[i].nodeName == viewName && _viewArray[i].nodeView.active == true) {
                            viewShowed = true;
                            break;
                        }
                    }
                    if (!viewShowed) {
                        for (var i = 0; i < _viewArray.length; i++) {
                            _viewArray[i].nodeView.active = false;
                        }
                        _viewArray = [];
                        _viewArray[0] = {'nodeView': view2,'nodeName': viewName};
                        _viewArray[0].nodeView.active = true;
                    }
                } else {
                    _viewArray[0] = {'nodeView': view2, 'nodeName': viewName}
                    _viewArray[0].nodeView.active = true;
                }
                return view2;
            },

            PushView(viewName) {
                if (!_viewMap[viewName]) {
                    return null;
                }
                var view = _viewMap[viewName];
                _viewArray[_viewArray.length] = {'nodeView':view,'nodeName':viewName} 
                _viewArray[_viewArray.length - 1].nodeView.active = true;
                return view;
            }, 

            PopView(viewName) {
                if (viewName == undefined) {
                    var view = _viewArray[_viewArray.length - 1].nodeView.active = false;
                    _viewArray.splice(_viewArray.length - 1, 1);
                    return view;
                } else {
                    var view = _viewMap[viewName];
                    if (view) {
                        for (var i = 0; i < _viewArray.length; i++) {
                            if (_viewArray[i].nodeName == viewName) {
                                _viewArray[i].nodeView.active = false;
                                _viewArray.splice(i,1);
                                break;
                            }
                        } 
                        return view;
                    }
                }
            },

            PopAllView() {
                for (var i = 0; i < _viewArray.length; i++) {
                    this.PopView();
                }
            },

            TopView() {
                if (_viewArray.length == 0) {
                    return null;
                }
                return _viewArray[_viewArray.length - 1];
            },

            CurrentView() {
                if (_viewArray.length == 0) {
                    return null;
                }
                return _viewArray[_viewArray.length - 1];
            }
        };
    }
)();

export default ViewManager;