
var Card = Namespace.declare("tla.EAM.layer3",
    class Card extends Interface {
        /*
        static get defaultImplementationClass() {
            return DefaultCard;
        }
        */
        static get dependencies() {
            return this.defaultImplementationClass.dependencies;
        }

        static get weBeanUnitPaths() {
            return this.defaultImplementationClass.weBeanUnitPaths;
        }

        static get overwriteServerDescriptor() {
            return this.defaultImplementationClass.overwriteServerDescriptor;
        }

        static get eamLayer() {
            return 3;
        }

        static get WORKFLOW_SELECT() { return "actionId:protected:Card.startSelectWorkflow[Select]"; }
        static get WORKFLOW_ASSIGN() { return "actionId:protected:Card.assign[Choose this]:success"; }
        static get ACTION_CLEAR() { return "actionId:protected:Card.setEmpty[Remove]:success"; }
    }
);

//                                                        js    src  version component
var DefaultCard = Namespace.declare("tla.EAM.layer5",
    class DefaultCard extends UcpComponent {
        static get implements() {
            return [Card];
        }

        static get overwriteServerDescriptor() {
            return true;
        }

        static get dependencies() {
            return [
                //"/EAMD.ucp/Components/com/twitter/Bootstrap/Accordion/CollapsiblePanel/3.3.7/CollapsiblePanel.component.xml",
                "/EAMD.ucp/Components/org/lesscss/Less/3.8.1/Less.component.xml"
            ];
        }

        static get weBeanUnitPaths() {
            return [
				"./src/html/weBeans/Card.weBean.html",
				//"./src/html/weBeans/CardItemView.weBean.html",
				//"./src/html/weBeans/CardDetailsView.weBean.html",
			];
        }

        static start() {
            UcpComponentSupport.importLink("/EAMD.ucp/Components/tla/EAM/layer5/Card/1.0.0/src/less/DefaultCard.less", "stylesheet/less", "text/css");
            //UcpComponentSupport.importLink(this.IOR.loader.basePath + "/src/less/"+this.name+".less", "stylesheet/less", "text/css");
        }

        constructor() {
            super();
        }

        init() {
            super.init();

            const demoModel = {
                //name: "New Card",
                description: "Simple Card Container",
                badge: "Card",
                cssClasses: "full-height",
                heading: "this.model.heading",
                footer: "this.model.footer"
            };
            this.model = demoModel;
            this.model.autoUpdate = true;

            Thinglish.defineAlias(this, "model.heading","name");
            //Thinglish.defineAlias(this.model, "itemViewModel.description");
            //Thinglish.defineAlias(this.model, "itemViewModel.badge");
            //inherit superModel ... until ManagedProperties will suport nested models
            //Object.keys(super.model).forEach(k => Thinglish.defineAlias(model, "defaultViewModel." + k));

            this.actionIndex = Thinglish.createActionIndexOf(Card, this);

            return this;
        }


		/*
        onModelChanged(changeEvent, updateObject) {
        }
        */


        onDomReady(view) {
        }


        /*
        loadWeBean() {
            if (Thinglish.lookupInObject(Relationship, "type.ucpComponentDescriptor.webBeanUnits"))
                return Relationship.type.ucpComponentDescriptor.webBeanUnits.find(u => u.model.name.startsWith(Relationship.name)).weBean;

            return null;
        }
        */

        updateView(view, updateObject) {
			/*
            console.info("ucpComponent updateView", this.id, ...arguments);

            console.info(`
           notifies you which view has to be rerendered.
           calling this.update() will force a complete rerendering.

           this implies loosing all DOM listeners. after rerendering onDomReady(view) will be called again to reinatialize DOM listeners

           calling tis.documentElement does the first rendering.
           the framework tries to postpone this step to the last possible point so that you can initialize the model first and 
           do not have to rerender many times. the last possible point to render, is if the element will be appended to to an element
           that is already in the DOM. The UcpView method checkTargetElement finds out if an element isInDom and ataches the boolean to the element.

           alternatively you can check the updateObject to see what in the model did change
           and update the dom manually here on view.documentElement or view.subViewElement

           view.subView gives you access to ucpViews declared as webeanProperties in the weBean
           `, this.id, ...arguments);
		   */

            if (!updateObject) {
                return;
            }
            Object.keys(updateObject).forEach(
                member => {
                    switch (member) {
                        case "attribute1":
                            //update something for attribute1
                            break;
                        case "property1":
                            //update something for property1
                            break;
                        default:
                            //update something in any case
                    }
                }
            );
        }

        handleSelection(event, item) {
            console.info(this.constructor.name, "selected", item.ucpComponent);
            Action.do(DetailsView.ACTION_SHOW, this);
            //Action.do(Workflow.ACTION_SELECT, item);
        }

        /*
            createOverView() {
                return new CardItemView();
            }
            createDetailsView() {
                return new CardDetailsView();
            }
        */
        startSelectWorkflow() {
            console.info(this.constructor.name, "startSelectWorkflow");
        }

        assign() {
            console.info(this.constructor.name, "assign");
        }

        setEmpty() {
            console.info(this.constructor.name, "setEmpty");
        }

        toggle() {
                console.log("toggle");
        }
        toggleFullHeight() {
            this.defaultView.toggleCssClass("full-height");
        }

        toggleHeader(hidden) {
            this.defaultView.toggleCssClass("hidden", this.headerElement, hidden);
        }
        toggleFooter(hidden) {
            this.defaultView.toggleCssClass("hidden", this.footerElement, hidden);
        }

        get headerElement() {
            if (!this.documentElement) {
                return null;
            }
            return this.documentElement.children[0];
        }

        get bodyElement() {
            if (!this.documentElement) {
                return null;
            }
            return this.documentElement.children[1];
        }

        get footerElement() {
            if (!this.documentElement) {
                return null;
            }
            return this.documentElement.children[2];
        }



    }
);

/*
var CardItemView = Namespace.declare("tla.EAM.layer5",
    class CardItemView extends UcpView {
        static get implements() { return [ItemView]; }

        init(ucpComponent) {
            super.init(ucpComponent);
            this.badge = "some Value";
            return this;
        }

        update(updateObject) {
            console.info("ucpView update", this.id, ...arguments);
            if (!updateObject) {
                return;
            }
            Object.keys(updateObject).forEach(
                member => {
                    switch (member) {
                        case "badge":
                            //update something for badge
                            break;
                        case "attribute1":
                            //update something for attribute1
                            break;
                        default:
                            //update something in any case
                    }
                }
            );
        }

    }
);


var CardDetailsView = Namespace.declare("tla.EAM.layer5",
    class CardDetailsView extends UcpView {
        static get implements() { return [DetailsView]; }

        init(ucpComponent) {
            super.init(ucpComponent);

            return this;
        }

        update(updateObject) {
            console.info("ucpView update", this.id, ...arguments);
        }

        onInitDetails() {
            console.info("ucpView onInitDetails", this.id, ...arguments);
            this.details.model.heading += " also from DetailsView";

        }

    }
);

*/