var DetailsPanel = Namespace.declare("com.ceruleanCircle.EAM.5_ux.WODA",
    class DetailsPanel extends UcpComponent {

        static start() {}
        static dependencies() {
            return [
                "/EAMD.ucp/Components/com/twitter/Bootstrap/Panel/3.3.7/Panel.component.xml"
            ];
        }

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

        }

        init() {
            super.init();
            Container.register(DetailsView, this);
            ONCE.eventSupport.addEventListener("newInstance", this, this.newInstance.bind(this));
            return this;
        }

        newInstance(event, thing) {
            if (window.What && thing instanceof What && thing != What.testInstance) {
                thing.eventSupport.addEventListener("selected", this, this.handleSelection.bind(this));
            }
        }

        add(object) {
            if (this.tabPanel && object != this.tabPanel) {
                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 = "Details";
                    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.add = this.add.bind(this);

                    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 = "DefaultDetailView";
                    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);
                */
            }
        }

        show(object) {
            UcpComponentSupport.getUcpComponent4Object(object).then(
                ucpComponent => {
                    var current = this.tabPanel.current;
                    if (current) current.close();

                    var detailsView = null;
                    var model = ucpComponent.model;
                    var ucpModel = ucpComponent.ucpModel;

                    detailsView = ucpModel.ucpComponent.detailsView;



                    if (!detailsView) {
                        UcpComponentSupport.getUcpComponent4Object(ucpModel.ucpComponent).then(
                            ucpComponent => {
                                detailsView = ucpComponent.detailsView;
                            }
                        );
                    }
                    if (Object.getOwnPropertyNames(detailsView.eventSupport.registry).length == 0)
                        detailsView.eventSupport.addEventListener("selected", this, this.handleSelection.bind(this));

                    this.tabPanel.add(detailsView);
                });
        }


        handleSelection(event, item) {
            console.debug(this.constructor.name + ": handleSelection", item);
            this.show(item);


            /*
            console.debug(this.constructor.name + ": handleSelection", item);
            var i = null;
            if (Array.isArray(item) && item.length > 0)
                i = item[0];
            else
                i = item;

            const detailsType = i.constructor.name + "Details";
            if (detailsType.startsWith("Promise")) {
                i.then(promissedItem => {
                    this.handleSelection(event, promissedItem)
                });
            } else {
                console.debug(this.constructor.name + ": handleSelection: checking for special Details:", detailsType);
                var aClass = null;
                try {
                    aClass = eval(detailsType);
                } catch (error) {};
                if (aClass) {
                    this.detailsTab.defaultView.remove();
                    this.detailsTab = new aClass();
                    this.add(this.detailsTab);
                }

                this.detailsTab.handleSelection(event, item);
            }
            */
        }

    }
);