MWF.xDesktop.requireApp("portal.PortalManager", "package", null, false);
MWF.xDesktop.requireApp("portal.PortalManager", "Actions.RestActions", null, false);
MWF.xDesktop.requireApp("Organization", "Selector.package", null, false);
MWF.require("MWF.xAction.org.express.RestActions", null,false);
MWF.require("MWF.widget.Identity", null,false);
MWF.xDesktop.requireApp("process.ProcessManager", "", null, false);
MWF.xApplication.portal = MWF.xApplication.portal || {};
MWF.xApplication.portal.PortalManager.Main = new Class({
	Extends: MWF.xApplication.process.ProcessManager.Main,
	Implements: [Options, Events],

	options: {
        "application": null,
		"style": "default",
		"name": "portal.PortalManager",
		"icon": "icon.png",
		"width": "1100",
		"height": "700",
		"title": MWF.xApplication.portal.PortalManager.LP.title
	},
	onQueryLoad: function(){
		this.lp = MWF.xApplication.portal.PortalManager.LP;
		this.currentContentNode = null;
        this.restActions = new MWF.xApplication.portal.PortalManager.Actions.RestActions();
	},
    loadStartMenu: function(callback){
        this.startMenuNode = new Element("div", {
            "styles": this.css.startMenuNode
        }).inject(this.node);

        this.menu = new MWF.xApplication.portal.PortalManager.Menu(this, this.startMenuNode, {
            "onPostLoad": function(){
                if (this.status){
                    if (this.status.navi!=null){
                        this.menu.doAction(this.menu.startNavis[this.status.navi]);
                    }else{
                        this.menu.doAction(this.menu.startNavis[0]);
                    }
                }else{
                    this.menu.doAction(this.menu.startNavis[0]);
                }
            }.bind(this)
        });
        this.addEvent("resize", function(){
            if (this.menu) this.menu.onResize();
        }.bind(this));
    },
    clearContent: function(){
        if (this.pageConfiguratorContent){
            if (this.pageConfigurator) delete this.pageConfigurator;
            this.pageConfiguratorContent.destroy();
            this.pageConfiguratorContent = null;
        }
        if (this.menuConfiguratorContent){
            if (this.menuConfigurator) delete this.menuConfigurator;
            this.menuConfiguratorContent.destroy();
            this.menuConfiguratorContent = null;
        }
        if (this.propertyConfiguratorContent){
            if (this.property) delete this.property;
            this.propertyConfiguratorContent.destroy();
            this.propertyConfiguratorContent = null;
        }
        if (this.sourceConfiguratorContent){
            if (this.sourceConfigurator) delete this.sourceConfigurator;
            this.sourceConfiguratorContent.destroy();
            this.sourceConfiguratorContent = null;
        }
        if (this.scriptConfiguratorContent){
            if (this.scriptConfigurator) delete this.scriptConfigurator;
            this.scriptConfiguratorContent.destroy();
            this.scriptConfiguratorContent = null;
        }
    },

    applicationProperty: function(){
        this.clearContent();
        this.propertyConfiguratorContent = new Element("div", {
            "styles": this.css.rightContentNode
        }).inject(this.node);
        this.property = new MWF.xApplication.portal.PortalManager.ApplicationProperty(this, this.propertyConfiguratorContent);
        this.property.load();
    },

    pageConfig: function(){
        this.clearContent();
        this.pageConfiguratorContent = new Element("div", {
            "styles": this.css.rightContentNode
        }).inject(this.node);
        this.loadPageConfig();
    },
    loadPageConfig: function(){
        MWF.xDesktop.requireApp("portal.PortalManager", "PageExplorer", function(){
            this.pageConfigurator = new MWF.xApplication.portal.PortalManager.PageExplorer(this.pageConfiguratorContent, this.restActions);
            this.pageConfigurator.app = this;
            this.pageConfigurator.load();
        }.bind(this));
    },

    menuConfig: function(){
        this.clearContent();
        this.menuConfiguratorContent = new Element("div", {
            "styles": this.css.rightContentNode
        }).inject(this.node);
        this.loadMenuConfig();
    },
    loadMenuConfig: function(){
        MWF.xDesktop.requireApp("portal.PortalManager", "MenuExplorer", function(){
            MWF.xDesktop.requireApp("portal.PortalManager", "Actions.RestActions", function(){
                if (!this.restActions) this.restActions = new MWF.xApplication.portal.PortalManager.Actions.RestActions();
                this.menuConfigurator = new MWF.xApplication.portal.PortalManager.MenuExplorer(this.menuConfiguratorContent, this.restActions);
                this.menuConfigurator.app = this;
                this.menuConfigurator.load();
            }.bind(this));
        }.bind(this));
    },
    sourceConfig: function(){
        this.clearContent();
        this.sourceConfiguratorContent = new Element("div", {
            "styles": this.css.rightContentNode
        }).inject(this.node);
        this.loadSourceConfig();
    },
    loadSourceConfig: function(){
        MWF.xDesktop.requireApp("portal.PortalManager", "SourceExplorer", function(){
            this.sourceConfigurator = new MWF.xApplication.portal.PortalManager.SourceExplorer(this.sourceConfiguratorContent, this.restActions);
            this.sourceConfigurator.app = this;
            this.sourceConfigurator.load();
        }.bind(this));
    },
    scriptConfig: function(){
        this.clearContent();
        this.scriptConfiguratorContent = new Element("div", {
            "styles": this.css.rightContentNode
        }).inject(this.node);
        this.loadScriptConfig();
    },
    loadScriptConfig: function(){
        MWF.xDesktop.requireApp("portal.PortalManager", "ScriptExplorer", function(){
            MWF.xDesktop.requireApp("portal.PortalManager", "Actions.RestActions", function(){
                if (!this.restActions) this.restActions = new MWF.xApplication.portal.PortalManager.Actions.RestActions();
                this.scriptConfigurator = new MWF.xApplication.portal.PortalManager.ScriptExplorer(this.scriptConfiguratorContent, this.restActions);
                this.scriptConfigurator.app = this;
                this.scriptConfigurator.load();
            }.bind(this));
        }.bind(this));
    }
});

MWF.xApplication.portal.PortalManager.Menu = new Class({
    Extends: MWF.xApplication.process.ProcessManager.Menu,
    Implements: [Options, Events]
});

MWF.xApplication.portal.PortalManager.ApplicationProperty = new Class({
    Extends: MWF.xApplication.process.ProcessManager.ApplicationProperty,

    createPropertyContentNode: function(){
        this.propertyContentNode = new Element("div", {"styles": {"overflow": "hidden"}}).inject(this.contentAreaNode);

        var html = "<table cellspacing='0' cellpadding='0' border='0' width='95%' align='center' style='margin-top: 20px'>";
        html += "<tr><td class='formTitle'>"+this.app.lp.application.name+"</td><td id='formApplicationName'></td></tr>";
        html += "<tr><td class='formTitle'>"+this.app.lp.application.alias+"</td><td id='formApplicationAlias'></td></tr>";
        html += "<tr><td class='formTitle'>"+this.app.lp.application.description+"</td><td id='formApplicationDescription'></td></tr>";
        html += "<tr><td class='formTitle'>"+this.app.lp.application.type+"</td><td id='formApplicationType'></td></tr>";
        html += "<tr><td class='formTitle'>"+this.app.lp.application.firstPage+"</td><td id='formApplicationFirstPage'></td></tr>";
        //     html += "<tr><td class='formTitle'>"+this.app.lp.application.icon+"</td><td id='formApplicationIcon'></td></tr>";
        html += "</table>";
        this.propertyContentNode.set("html", html);
        this.propertyContentNode.getElements("td.formTitle").setStyles(this.app.css.propertyBaseContentTdTitle);

        this.nameInput = new MWF.xApplication.portal.PortalManager.Input(this.propertyContentNode.getElement("#formApplicationName"), this.data.name, this.app.css.formInput);
        this.aliasInput = new MWF.xApplication.portal.PortalManager.Input(this.propertyContentNode.getElement("#formApplicationAlias"), this.data.alias, this.app.css.formInput);
        this.descriptionInput = new MWF.xApplication.portal.PortalManager.Input(this.propertyContentNode.getElement("#formApplicationDescription"), this.data.description, this.app.css.formInput);
        this.typeInput = new MWF.xApplication.portal.PortalManager.Input(this.propertyContentNode.getElement("#formApplicationType"), this.data.applicationCategory, this.app.css.formInput);
        this.firstPageInput = new MWF.xApplication.portal.PortalManager.Select(this.propertyContentNode.getElement("#formApplicationFirstPage"), this.data.firstPage, this.app.css.formInput, function(){
            var pages = {};
            debugger;
            this.app.restActions.listPage(this.app.options.application.id, function(json){
                json.data.each(function(page) {
                    pages[page.id] = page.name;
                }.bind(this));
            }.bind(this), null, false);
            return pages;
        }.bind(this));
    },
    editMode: function(){
        this.nameInput.editMode();
        this.aliasInput.editMode();
        this.descriptionInput.editMode();
        this.typeInput.editMode();
        this.firstPageInput.editMode();
        this.isEdit = true;
    },
    readMode: function(){
        this.nameInput.readMode();
        this.aliasInput.readMode();
        this.descriptionInput.readMode();
        this.typeInput.readMode();
        this.firstPageInput.readMode();
        this.isEdit = false;
    },
    save: function(callback, cancel) {
        this.data.name = this.nameInput.input.get("value");
        this.data.alias = this.aliasInput.input.get("value");
        this.data.description = this.descriptionInput.input.get("value");
        this.data.applicationCategory = this.typeInput.input.get("value");
        this.data.firstPage = this.firstPageInput.input.get("value");

        this.app.restActions.saveApplication(this.data, function (json) {
            this.propertyTitleBar.set("text", this.data.name);
            this.data.id = json.data.id;
            this.nameInput.save();
            this.aliasInput.save();
            this.descriptionInput.save();
            this.typeInput.save();
            this.firstPageInput.save();

            if (callback) callback();
        }.bind(this), function (xhr, text, error) {
            if (cancel) cancel(xhr, text, error);
        }.bind(this));
    }
});

MWF.xApplication.portal.PortalManager.Input = new Class({
    Extends: MWF.xApplication.process.ProcessManager.Input,
    Implements: [Events]
});
MWF.xApplication.portal.PortalManager.Select = new Class({
    Extends: MWF.xApplication.portal.PortalManager.Input,
    Implements: [Events],
    initialize: function(node, value, style, select){
        this.node = $(node);
        this.value = (value) ? value: "";
        this.style = style;
        this.select = select;
        this.selectList = null;;
        this.load();
    },
    getSelectList: function(){
        if (this.select){
            return this.select();
        }
        return [];
    },
    getText: function(value){
        if (value){
            if (this.selectList){
                return this.selectList[value] || "";
            }
        }
        return "";
    },
    load: function(){
        this.selectList = this.getSelectList();
        this.content = new Element("div", {
            "styles": this.style.content,
            "text": this.getText(this.value)
        }).inject(this.node);
    },
    editMode: function(){
        this.content.empty();
        this.input = new Element("select",{
            //"styles": this.style.input,
            //"value": this.value
        }).inject(this.content);

        Object.each(this.selectList, function(v, k){
            new Element("option", {
                "value": k,
                "text": v,
                "selected": (this.value==v)
            }).inject(this.input);
        }.bind(this));

        //this.input.addEvents({
        //    //"focus": function(){
        //    //    this.input.setStyles(this.style.input_focus);
        //    //}.bind(this),
        //    //"blur": function(){
        //    //    this.input.setStyles(this.style.input);
        //    //}.bind(this),
        //    //"change": function(){
        //    //    this.input.setStyles(this.style.input);
        //    //}.bind(this)
        //});
    },
    readMode: function(){
        this.content.empty();
        this.input = null;
        this.content.set("text", this.getText(this.value));
    },
    save: function(){
        if (this.input) this.value = this.input.options[this.input.selectedIndex].get("value");
        return this.value;
    }
});