/* global Folder, Action, OverView, Breadcrumb, StructrObject, StructrComponent, DetailsView, Version */
var DefaultFolderNG = Namespace.declare("org.structr.layer2",
    class DefaultFolderNG extends UcpComponent {
        static get implements() { return [Folder]; }
        static start() {
            const namespaceClass = Namespace.declare("tla.EAM.layer2", Namespace);
            const n = new namespaceClass();
            Thinglish.implement(n, Folder);

            Namespace.defaultImplementationClass = StructrFolder;
            UcpComponent.defaultImplementationClass = StructrFolder;
            StructrComponent.defaultImplementationClass = StructrFolder;
            Version.defaultImplementationClass = StructrFolder;
            //EAMLayer.defaultImplementationClass = StructrFolder;
            Folder.defaultImplementationClass = StructrFolder;
        }
        static get dependencies() {
            return [
                "/EAMD.ucp/Components/tla/EAM/layer2/DefaultFile/1.0.0/DefaultFile.component.xml"
            ];
        }


        constructor() {
            super();

            //Thinglish.implement(this, Folder);

            //Thinglish.implement(new UcpComponent(), Folder);

        }

        init(structrObject) {
            if (this._private.isInitialized) {
                return;
            }

            //super.init();
            //this.ucpModel = structrObject;

            structrObject.properties.description = this.type.name;
            structrObject.ucpComponent = this;
            this.properties.badge = structrObject.model.files.length + structrObject.model.folders.length;
            this._private.isInitialized = true;

            return this;
        }

        get properties() {
            return super.properties;
        }
        set properties(structrObject) {
            this.ucpModel = structrObject;
        }

        createDetailsView() {
            return new DefaultFolderOverView();
        }

        createOverView() {
            return new DefaultFolderOverView();
        }

        handleSelection(event, item) {
            console.log(this.constructor.name, "selected", item);
            //Action.do(Workflow.ACTION_SELECT, item, DetailsView.ACTION_SHOW, [What.ACTION_PUSH, What.ACTION_POP, OverView.ACTION_SHOW]);
            Action.do(OverView.ACTION_SHOW, this);
            //Action.do(ActionsPanel.ACTION_SET_PRIMARY, DetailsView.ACTION_SHOW);
        }

    });

var DefaultFolderNGOverView = Namespace.declare("tla.EAM.layer5",
    class DefaultFolderNGOverView extends OverView {
        constructor() {
            super();
        }
        init(object) {
            super.init(object);
            return this;
        }

        loadWeBean() {
            if (Thinglish.lookupInObject(DefaultFolderNG, "type.ucpComponentDescriptor.webBeanUnits")) {
                return DefaultFolderNG.type.ucpComponentDescriptor.webBeanUnits.find(u => u.model.name.startsWith(this.constructor.name)).weBean;
            }

            //if (this.controller.ucpComponentClass.type.ucpComponentDescriptor)
            //return this.controller.ucpComponentClass.type.ucpComponentDescriptor.defaultWebBeanUnit.weBean;
            return null;
        }

        get description() {
            let description = this.properties.description;
            if (!description) {
                description = Thinglish.lookupInObject(this, "ucpComponent.type.ucpComponentDescriptor.properties.description");
            }
            if (!description) {
                description = "";
            }
            if (Thinglish.lookupInObject(this, "model.type.name")) {
                description = this.model.type.name + ": " + description;
            }

            if (this.ucpModel instanceof StructrObject) {
                description = "StructrObject: " + this.model.structrType;
            }

            return description;
        }

        get typeName() {
            return Thinglish.getTypeName(this.model);
        }

        select() {
            console.info("select", this.ucpModel._private.model);
        }

        handleSelection(event, item) {
            console.log(this.constructor.name, "selected", item);
            Action.do(Workflow.ACTION_SELECT, item, DetailsView.ACTION_SHOW, [What.ACTION_PUSH, What.ACTION_POP]);
            Action.do(OverView.ACTION_SHOW, item);
            //this.eventSupport.fire("selected", this, item);
        }

        async updateView( /* view */ ) {
            if (!this.breadcrumb) {
                this.breadcrumb = Breadcrumb.getInstance().init();
                this.breadcrumb.model = this.model;
            }
            this.breadcrumb.defaultView.prepend(this.documentElement);

            let list = this.model.folders.concat(this.model.files);
            list = await Promise.all(list.map(file => (file.load && !file.isLoaded) ? file.load() : file));
            list = list.sort((a, b) =>
                (String((a.model.type.startsWith("File")) ? "0" : "") + a.model.name).localeCompare(((b.model.type.startsWith("File")) ? "0" : "") + b.model.name)
            );
            this.slipList.properties.items = list;

            this.slipList.eventSupport.addEventListener("selected", this.ucpComponent, this.ucpComponent.handleSelection.bind(this.ucpComponent));
            this.slipList.eventSupport.addEventListener("selected", this, this.handleSelection.bind(this));

        }


        append(element) {
            super.append(element);
        }

        replace(element) {
            super.replace(element);
        }

        get slipList() {
            return this.documentElement.children[2].ucpView.ucpComponent;
        }
    }
);

var DefaultFolderNGDetailsView = Namespace.declare("tla.EAM.layer5",
    class DefaultFolderNGDetailsView extends DetailsView {
        constructor() {
            super();
        }
        init(object) {
            super.init(object);
            return this;
        }

        loadWeBean() {
            if (Thinglish.lookupInObject(DefaultFolderNG, "type.ucpComponentDescriptor.webBeanUnits")) {
                return DefaultFolderNG.type.ucpComponentDescriptor.webBeanUnits.find(u => u.name.startsWith(this.constructor.name)).weBean;
            }

            //if (this.controller.ucpComponentClass.type.ucpComponentDescriptor)
            //return this.controller.ucpComponentClass.type.ucpComponentDescriptor.defaultWebBeanUnit.weBean;
            return null;
        }

        get description() {
            let description = this.properties.description;
            if (!description) {
                description = Thinglish.lookupInObject(this, "ucpComponent.type.ucpComponentDescriptor.properties.description");
            }
            if (!description) {
                description = "";
            }
            if (Thinglish.lookupInObject(this, "model.type.name")) {
                description = this.model.type.name + ": " + description;
            }

            if (this.ucpModel instanceof StructrObject) {
                description = "StructrObject: " + this.model.structrType;
            }

            return description;
        }

        get typeName() {
            return Thinglish.getTypeName(this.model);
        }

        select() {
            console.info("select", this.ucpModel._private.model);
        }

        handleSelection(event, item) {
            console.log(this.constructor.name, "selected", item);
            Action.do(Workflow.ACTION_SELECT, item, DetailsView.ACTION_SHOW, [What.ACTION_PUSH, What.ACTION_POP]);
            Action.do(OverView.ACTION_SHOW, item);
            //this.eventSupport.fire("selected", this, item);
        }

        async updateView(view) {
            if (!this.breadcrumb) {
                this.breadcrumb = Breadcrumb.getInstance().init();
                this.breadcrumb.model = this.model;
            }
            this.breadcrumb.defaultView.prepend(this.documentElement);

            let list = this.model.folders.concat(this.model.files);
            list = await Promise.all(list.map(file => (file.load && !file.isLoaded) ? file.load() : file));
            list = list.sort((a, b) =>
                (String((a.model.type.startsWith("File")) ? "0" : "") + a.model.name).localeCompare(((b.model.type.startsWith("File")) ? "0" : "") + b.model.name)
            );
            this.slipList.properties.items = list;

            this.slipList.eventSupport.addEventListener("selected", this.ucpComponent, this.ucpComponent.handleSelection.bind(this.ucpComponent));
            this.slipList.eventSupport.addEventListener("selected", this, this.handleSelection.bind(this));

        }


        append(element) {
            super.append(element);
        }

        replace(element) {
            super.replace(element);
        }

        get slipList() {
            return this.documentElement.children[2].ucpView.ucpComponent;
        }
    }
);


var StructrFolderNG = Namespace.declare("org.structr.layer2",
    class StructrFolderNG extends StructrComponent {
        static get implements() { return [Folder]; }
        static start() {}
        static get dependencies() {
            return [
                "/EAMD.ucp/Components/com/twitter/Bootstrap/Breadcrumb/1.0.0/Breadcrumb.component.xml"
            ];
        }

        constructor() {
            super();
            //Thinglish.implement(this, Folder);
        }

        init(structrObject) {
            if (this._private.isInitialized) {
              return;
            }
            //super.init();
            //this.ucpModel = structrObject;

            structrObject.properties.description = this.type.name;
            structrObject.ucpComponent = this;
            this._private.isInitialized = true;

            return this;
        }

        get properties() {
            return super.properties;
        }
        set properties(structrObject) {
            this.ucpModel = structrObject;
        }

        get overView() {
            return this.controller.getOverView(this.ucpModel);
        }

        createDetailsView() {
            return new DefaultFolderNGOverView();
        }

        createOverView() {
            return new DefaultFolderNGOverView();
        }

        handleSelection(event, item) {
            console.log(this.constructor.name, "selected", item);
            //Action.do(Workflow.ACTION_SELECT, item, DetailsView.ACTION_SHOW, [What.ACTION_PUSH, What.ACTION_POP, OverView.ACTION_SHOW]);
            Action.do(OverView.ACTION_SHOW, this);
            //Action.do(ActionsPanel.ACTION_SET_PRIMARY, DetailsView.ACTION_SHOW);
        }

    }
);


/*
var EAMLayer = Namespace.declare("tla.EAM.layer3",
    class EAMLayer extends Folder {
        static get dependencies() {
            return [
                "/EAMD.ucp/Components/com/ceruleanCircle/EAM/5_ux/StructrES6Client/0.5.0/StructrES6Client.component.xml"
            ];
        }
        static get defaultImplementationClass() { return EAMLayerFolder; }
        constructor() {
            super();
        }

        get eamLayer() { return 3; }

        get isFolder() {
            return true;
        }

    }
);

var EAMLayerFolder = Namespace.declare("org.structr.layer2",
    class EAMLayerFolder extends StructrFolder {
        static get implements() { return [Folder]; }
        static start() {}
        static get dependencies() {
            return [
                "/EAMD.ucp/Components/com/twitter/Bootstrap/Breadcrumb/1.0.0/Breadcrumb.component.xml"
            ];
        }

        constructor() {
            super();
            //Thinglish.implement(this, Folder);
        }

        init(structrObject) {
            if (this._private.isInitialized)
                return;
            super.init();

            return this;
        }

    }
);


var UcpComponentInterface = Namespace.declare("tla.EAM.layer3",
    class UcpComponentInterface extends Folder {
        static get dependencies() {
            return [
                "/EAMD.ucp/Components/com/ceruleanCircle/EAM/5_ux/StructrES6Client/0.5.0/StructrES6Client.component.xml"
            ];
        }
        static get defaultImplementationClass() { return UcpComponentFolder; }
        constructor() {
            super();
        }

        get eamLayer() { return 3; }

        get isFolder() {
            return true;
        }

    }
);


var UcpComponentFolder = Namespace.declare("org.structr.layer2",
    class UcpComponentFolder extends StructrFolder {
        static get implements() { return [Folder]; }
        static start() {}
        static get dependencies() {
            return [
                "/EAMD.ucp/Components/com/twitter/Bootstrap/Breadcrumb/1.0.0/Breadcrumb.component.xml"
            ];
        }

        constructor() {
            super();
            //Thinglish.implement(this, Folder);
        }

        init(structrObject) {
            if (this._private.isInitialized)
                return;
            super.init(structrObject);

            return this;
        }

    }
);
*/