/* global Folder, Action, OverView, Breadcrumb, StructrObject, StructrComponent, DetailsView */
var DefaultFolder = Namespace.declare("org.structr.layer2",
    class DefaultFolder 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 DefaultFolderDetailsView();
      }

        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 DefaultFolderOverView = Namespace.declare("tla.EAM.layer5",
    class DefaultFolderOverView extends OverView {
        constructor() {
            super();
        }
        init(object) {
            super.init(object);
            return this;
        }

        loadWeBean() {
            if (Thinglish.lookupInObject(DefaultFolder, "type.ucpComponentDescriptor.webBeanUnits")) {
                return DefaultFolder.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 DefaultFolderDetailsView = Namespace.declare("tla.EAM.layer5",
  class DefaultFolderDetailsView extends DetailsView {
    constructor() {
      super();
    }
    init(object) {
      super.init(object);
      return this;
    }

    loadWeBean() {
      if (Thinglish.lookupInObject(DefaultFolder, "type.ucpComponentDescriptor.webBeanUnits")) {
        return DefaultFolder.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);

      var 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) =>
        ('' + ((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 StructrFolder = Namespace.declare("org.structr.layer2",
    class StructrFolder 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);
        }
        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);
        }

    }
);