/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License.
 */

package org.apache.isis.core.metamodel.facetapi;

import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

import org.apache.isis.commons.internal.collections._Sets;
import org.apache.isis.core.metamodel.context.HasMetaModelContext;
import org.apache.isis.core.metamodel.context.MetaModelContext;

import lombok.Getter;
import lombok.NonNull;

public abstract class FacetAbstract
implements Facet, HasMetaModelContext {

    private final @NonNull Class<? extends Facet> facetType;
    private Set<Facet> contributedFacets; // lazy init

    @Getter(onMethod_ = {@Override}) private final @NonNull Facet.Precedence precedence;

    @Getter(onMethod_ = {@Override})
    private final @NonNull FacetHolder facetHolder;

    public FacetAbstract(
            final Class<? extends Facet> facetType,
            final FacetHolder facetHolder,
            final Facet.Precedence precedence) {

        this.facetType = facetType;
        this.facetHolder = facetHolder;
        this.precedence = precedence;
    }

    protected FacetAbstract(
            final Class<? extends Facet> facetType,
            final FacetHolder facetHolder) {

        this(facetType, facetHolder, Facet.Precedence.DEFAULT);
    }

    @Override
    public final Class<? extends Facet> facetType() {
        return facetType;
    }

    @Override
    public MetaModelContext getMetaModelContext() {
        return facetHolder.getMetaModelContext();
    }

    protected String toStringValues() {
        return "";
    }

    @Override
    public String toString() {
        String details = "";
        if (isValidating()) {
            details += "Validating";
        }
        if (isDisabling()) {
            details += (details.length() > 0 ? ";" : "") + "Disabling";
        }
        if (isHiding()) {
            details += (details.length() > 0 ? ";" : "") + "Hiding";
        }
        if (!"".equals(details)) {
            details = "interaction=" + details + ",";
        }

        final String className = getClass().getName();
        final String stringValues = toStringValues();
        if (getClass() != facetType()) {
            final String facetType = facetType().getName();
            details += "type=" + facetType.substring(facetType.lastIndexOf('.') + 1);
        }
        if (!"".equals(stringValues)) {
            details += ",";
        }
        return className.substring(className.lastIndexOf('.') + 1) + "[" + details + stringValues + "]";
    }

    private boolean isHiding() {
        return Hiding.class.isAssignableFrom(getClass());
    }

    private boolean isDisabling() {
        return Disabling.class.isAssignableFrom(getClass());
    }

    private boolean isValidating() {
        return Validating.class.isAssignableFrom(getClass());
    }

    @Override
    public void visitAttributes(final BiConsumer<String, Object> visitor) {
        visitor.accept("fqcn", this.getClass().getName());
        visitor.accept("precedence", getPrecedence().name());
        if(isHiding()) {
            visitor.accept("hiding", isHiding());
        }
        if(isDisabling()) {
            visitor.accept("disabling", isDisabling());
        }
        if(isValidating()) {
            visitor.accept("validating", isValidating());
        }
    }

    /**
     * Marker interface used within {@link #toString()}.
     */
    public static interface Hiding {
    }

    /**
     * Marker interface used within {@link #toString()}.
     */
    public static interface Disabling {
    }

    /**
     * Marker interface used within {@link #toString()}.
     */
    public static interface Validating {
    }

    // -- CONTRIBUTED FACET SUPPORT

    @Override
    public void addContributedFacet(final Facet contributedFacet) {
        if(contributedFacets==null) {
            contributedFacets = _Sets.newHashSet();
        }
        contributedFacets.add(contributedFacet);
    }

    @Override
    public void forEachContributedFacet(final Consumer<Facet> onContributedFacet) {
        if(contributedFacets!=null) {
            contributedFacets.forEach(onContributedFacet);
        }
    }


}

@@ -965,16 +965,84 @@
 onsumer;
+%0Aimport java.util.stream.Collectors;%0Aimport java.util.stream.Stream;
 %0A%0Aimport
@@ -1270,16 +1270,35 @@
 NonNull;
+%0Aimport lombok.val;
 %0A%0Apublic
@@ -2519,335 +2519,62 @@
 s = 
-%22%22;%0A        if (isValidating()) %7B%0A            details += %22Validating%22;%0A        %7D%0A        if (isDisabling()) %7B%0A            details += (details.length() %3E 0 ? %22;%22 : %22%22) + %22Disabling%22;%0A        %7D%0A        if (isHiding()) %7B%0A            details += (details.length() %3E 0 ? %22;%22 : %22%22) + %22Hiding%22;%0A        %7D%0A        if (!%22%22.equals(details
+interactionDetails(%22;%22);%0A        if (!details.isEmpty(
 )) %7B
@@ -3139,316 +3139,8 @@
  %7D%0A%0A
-    private boolean isHiding() %7B%0A        return Hiding.class.isAssignableFrom(getClass());%0A    %7D%0A%0A    private boolean isDisabling() %7B%0A        return Disabling.class.isAssignableFrom(getClass());%0A    %7D%0A%0A    private boolean isValidating() %7B%0A        return Validating.class.isAssignableFrom(getClass());%0A    %7D%0A%0A
     
@@ -3254,11 +3254,12 @@
 t(%22f
-qcn
+acet
 %22, t
@@ -3351,210 +3351,145 @@
 ));%0A
-        if(isHiding()) %7B%0A            visitor.accept(%22hiding%22, isHiding());%0A        %7D%0A        if(isDisabling()) %7B%0A            visitor.accept(%22disabling%22, isDisabling());%0A        %7D%0A        if(isValidating
+%0A        val interactionDetails = interactionDetails(%22, %22);%0A%0A        // suppress 'details' if none%0A        if(!interactionDetails.isEmpty
 ()) 
@@ -3522,35 +3522,40 @@
 pt(%22
-validating%22, isValidating()
+interaction%22, interactionDetails
 );%0A 
@@ -3872,32 +3872,32 @@
 ing()%7D.%0A     */%0A
-
     public stati
@@ -3924,24 +3924,348 @@
 ng %7B%0A    %7D%0A%0A
+    private String interactionDetails(final String delimiter) %7B%0A        return Stream.of(Validating.class, Hiding.class, Disabling.class)%0A        .filter(marker-%3Emarker.isAssignableFrom(getClass()))%0A        .map(Class::getSimpleName)%0A        .map(String::toLowerCase)%0A        .collect(Collectors.joining(delimiter));%0A    %7D%0A%0A
     // -- CO

remove facet attributes 'hiding', 'disabling' 'validating'