var DefaultDetails = Namespace.declare("com.ceruleanCircle.EAM.5_ux.WODA.DetailsPanel",
    class DefaultDetails extends UcpComponent {
        static start() {
            //document.weBeans.find();
            //document.weBeans.overwriteAllBeans();
            //this.getInstance().defaultView.append();
        }
        static dependencies() {
            return [
                "/EAMD.ucp/Components/com/twitter/Bootstrap/Panel/1.0.0/Panel.component.xml",
                "/EAMD.ucp/Components/com/twitter/Bootstrap/TabPanel/1.0.0/TabPanel.component.xml",
                "/EAMD.ucp/Components/ski/kornel/SlipJS/2.1.1/SlipJS.component.xml",
                "/EAMD.ucp/Components/com/twitter/Bootstrap/Accordion/1.0.0/Accordion.component.xml",
                "/EAMD.ucp/Components/com/twitter/Bootstrap/Accordion/CollapsiblePanel/1.0.0/CollapsiblePanel.component.xml"
            ];
        }

        constructor() {
            super();
            this.cssClasses = "col-sm-4";

        }

        init() {
            super.init();
            UcpComponentSupport.Store.register(DetailsView, this);
            //ONCE.eventSupport.addEventListener("newThing", this, this.newThing.bind(this));
            return this;
        }

        newThing(event, thing) {
            if (Thinglish.isInstanceOf(thing, DetailsView)) {
                var theDetailViewPanelList = UcpComponentSupport.Store.lookup(DetailsView);
                var firstDetailViewPanel = null;
                if (theDetailViewPanelList)
                    firstDetailViewPanel = theDetailViewPanelList[0];

                if (firstDetailViewPanel)
                    firstDetailViewPanel.add(thing);

            }
        }

        add(object) {
            if (object instanceof DetailsView) {
                this.tabPanel.add(object);
            } else {
                super.add(object);
            }
        }


        updateView(view) {
            if (view instanceof DefaultView) {
                if (!this.panel) {
                    this.panel = Panel.getInstance().init();
                    Thinglish.defineAlias(this, "panel.properties.heading", "heading");
                    Thinglish.defineAlias(this, "panel.properties.fullHeight", "fullHeight");
                    Thinglish.defineAlias(this, "panel.properties.footer", "footer");
                    this.heading = "Detailview";
                    this.footer = "All the Attributes and Properties";
                    this.panel.properties.cssClasses = "";
                    this.panel.overflow = "auto";

                    this.add(this.panel);
                    // @todo encapsulate in nicer method e.g. setContainer
                    Thinglish.defineAccessors(this.defaultView, "container",
                        new Function("{ return this.panel.container; }").bind(this), null);

                    this.panel.toggleFullHeight(this.panel.fullHeight);
                }
                if (!this.tabPanel) {
                    this.tabPanel = TabPanel.getInstance().init();
                    this.add(this.tabPanel);

                    this.defaultDetails = Panel.getInstance().init();
                    this.defaultDetails.heading = "DefaultDetailviewPanel";
                    this.defaultDetails.footer = "shown when there is no dedicated DetailView";
                    this.defaultDetails.cssClasses = "";

                    this.tabPanel.add(this.defaultDetails);

                }
                this.tabPanel.activateTab(0);
                this.defaultDetails.toggleFullHeight();
                this.defaultDetails.toggleHeading(true);

            }
        }


        handleSelection(event, item) {
            console.debug(this.constructor.name + ": handleSelection", item);
            if (Thinglish.lookupInObject(event, "source.ucpComponentClass") != this.methodsList.constructor) {
                var i = null;
                if (Array.isArray(item) && item.length > 0)
                    i = item[0];
                else
                    i = item;


                var type = Thinglish.lookupInObject(i, "type.name");
                if (Thinglish.isClass(i))
                    type = "Classes";

                this.properties.heading = "List of " + type;

                this.propertiesList.clearList();
                this.relationsList.clearList();
                this.methodsList.clearList();

                const properties = [];
                const relationships = [];
                const methods = [];
                const types = [];
                const collections = [];

                var props = Thinglish.getOwnPropertyDescriptorArray(i);

                props.forEach(property => {        
                    if (property.value != undefined && property.value != null) {        
                        if (Thinglish.isPrimitive(property.value)) {              
                            properties.push({
                                value: property.value,
                                                  
                                name: property.propertyName,
                                type: { name: i[property.propertyName] }                  
                            });               
                        } else {        

                            if (Thinglish.isFunction(property.value)) {              
                                methods.push({
                                    value: property.value,
                                                      
                                    name: property.propertyName,
                                    type: { name: i[property.propertyName] }                  
                                });            
                            } else {
                                var checkType = i[property.propertyName];
                                if (!Thinglish.lookupInObject(checkType, "type.name"))
                                    checkType = checkType.constructor.name;

                                if (property.value instanceof Array) {
                                    collections.push({
                                        name: property.propertyName,
                                        type: { name: i[property.propertyName] }  
                                    });


                                    property.value.forEach(entry => { 
                                        types.push({
                                            name: entry.propertyName,
                                            type: { name: i[entry.propertyName] }  
                                        });
                                    });


                                } else {
                                    relationships.push({
                                        name: property.propertyName,
                                        type: { name: checkType }              
                                    });
                                }           

                                relationships.push({
                                    value: property.value,
                                                      
                                    name: property.propertyName,
                                    type: { name: checkType }              
                                });            
                            }
                        }          

                    }
                });

                this.methodsList.appendItem(methods);
                this.propertiesList.appendItem(properties);
                this.relationsList.appendItem(relationships);
                this.typeList.appendItem(types);
                this.collectionList.appendItem(collections);
            }
        }
    }
);