
var Offer = Namespace.declare("tla.EAM.layer3",
    class Offer extends Interface {
        /*
        static get defaultImplementationClass() {
            return DefaultOffer;
        }
        */
        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:Offer.startSelectWorkflow[Select]"; }
        static get WORKFLOW_ASSIGN() { return "actionId:protected:Offer.assign[Choose this]:success"; }
        static get ACTION_CLEAR() { return "actionId:protected:Offer.setEmpty[Remove]:success"; }
    }
);

//                                                        js    src  version component
var DefaultOffer = Namespace.declare("com.sixt.EAM.3_services",
    class DefaultOffer extends UcpComponent {
        static get implements() {
            return [Offer];
        }

        static get overwriteServerDescriptor() {
            return true;
        }

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

        static get weBeanUnitPaths() {
            return [
				"./src/html/weBeans/Offer.weBean.html",
				//"./src/html/weBeans/OfferItemView.weBean.html",
				//"./src/html/weBeans/OfferDetailsView.weBean.html",
			];
        }

        static start() {
			//UcpComponentSupport.importLink(this.IOR.loader.basePath + "/src/less/"+this.name+".less", "stylesheet/less", "text/css");		
		}

        constructor() {
            super();
        }

        init() {
            super.init();

            const demoModel = {
                //itemViewModel: {
                name: "Offer",
                description: "ideal minimal implementaion of a ucpComponent",
                badge: "ideal", //"has to be copied on UcpView.init"
                //},

                attribute1: "anAttribute",
                property1: "property1 storage point like an attribute",
                relationship1: EmptyRelationship.of(User),
                collection1: [1, 2, 3, 4, 5],

                subComponent: undefined

            };
            this.model = demoModel;
            this.model.autoUpdate = true;

            //Thinglish.defineAlias(this.model, "itemViewModel.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(Offer, this);

            return this;
        }


        get property1() {
                return this.ucpModel.value.property1;
        }
        set property1(newValue) {
                this.ucpModel.value.property1 = newValue;
        }


        get subComponent() {
            if (this.model && this.ucpModel.value.subComponent) {
                return this.ucpModel.value.subComponent;
            }
            const newComponent = new CollapsiblePanel().init();
            //configure subComponent
            newComponent.name = "new Subcomponent";
            this.model.subComponent = newComponent;

            return newComponent;
        }

        set subComponent(newComponent) {
            this.ucpModel.value.subComponent = newComponent;
        }

        /*
        onInitSubComponentList(view) {
           console.info("ucpComponent onInitDetails",view.id,  ...arguments);
           this.details.model.heading = "Details initialized";

           console.info(`
           In the DefaultView WeBean IdealComponent.weBean.html a webean-property="details" ist declared on
                <bootstrap-collapsible-panel heading="Details" webean-property="details">

           this.details gives you access to the ucpView of that CollapsiblePanel
           this.detailsElement gives you access to  this.details.documentElement in the DOM          
           
           initalize any DOM listeners here.
           the method will be called after every new rendering
           `);
        }
        */

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

        onDomReady(view) {
			/*
            console.info("ucpComponent onDomReady", this.id, ...arguments);
            console.info("ucpComponent onDomReady - view:", this.id, view.id, view.documentElement);
            console.info(`
           the view is fully rendered and available in the DOM.

           initalize any DOM listeners here.
           the method will be called after every new rendering
           `);
		   */
        }


        /*
        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 (!(view instanceof DefaultView)) return;
			
            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 OfferItemView();
            }
            createDetailsView() {
                return new OfferDetailsView();
            }
        */
        startSelectWorkflow() {
            console.info(this.constructor.name, "startSelectWorkflow");
        }

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

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

/*
var OfferItemView = Namespace.declare("tla.EAM.layer5",
    class OfferItemView 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 OfferDetailsView = Namespace.declare("tla.EAM.layer5",
    class OfferDetailsView 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";

        }

    }
);

*/