/*
 *  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.viewer.wicket.ui.components.layout.bs;

import java.util.Optional;

import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.repeater.RepeatingView;

import org.apache.isis.applib.layout.grid.bootstrap.BSGrid;
import org.apache.isis.applib.layout.grid.bootstrap.BSRow;
import org.apache.isis.core.metamodel.object.ManagedObject;
import org.apache.isis.core.metamodel.spec.feature.MixedInMember;
import org.apache.isis.core.metamodel.util.Facets;
import org.apache.isis.viewer.wicket.model.models.ActionModel;
import org.apache.isis.viewer.wicket.model.models.EntityModel;
import org.apache.isis.viewer.wicket.ui.components.layout.bs.row.Row;
import org.apache.isis.viewer.wicket.ui.panels.PanelAbstract;
import org.apache.isis.viewer.wicket.ui.util.Wkt;

import lombok.val;

public class BSGridPanel
extends PanelAbstract<ManagedObject, EntityModel> {

    private static final long serialVersionUID = 1L;

    private static final String ID_ROWS = "rows";

    private final BSGrid bsPage;

    public static Optional<BSGridPanel> extraContentForMixin(final String id, final ActionModel actionModel) {

        val action = actionModel.getAction();
        if(action.isMixedIn()) {

            val mixinSpec = ((MixedInMember)action).getMixinType();
            val targetAdapterForMixin = action.realTargetAdapter(actionModel.getActionOwner());

            // if we can bootstrap a grid, use it
            return Facets.bootstrapGrid(mixinSpec, targetAdapterForMixin)
            .map(bsGrid->{
                val commonContext = actionModel.getMetaModelContext();
                val entityModelForMixin =
                        EntityModel.ofAdapter(commonContext, targetAdapterForMixin);
                return new BSGridPanel(id, entityModelForMixin, bsGrid);
            });
        }
        return Optional.empty();
    }


    public BSGridPanel(final String id, final EntityModel entityModel, final BSGrid bsGrid) {
        super(id, entityModel);
        this.bsPage = bsGrid;
        buildGui();
    }

    private void buildGui() {

        Wkt.cssAppend(this, bsPage.getCssClass());

        final RepeatingView rv = new RepeatingView(ID_ROWS);

        for(final BSRow bsRow: this.bsPage.getRows()) {
            final String id = rv.newChildId();
            final WebMarkupContainer row = new Row(id, getModel(), bsRow);
            rv.add(row);
        }
        add(rv);
    }

}

@@ -1208,125 +1208,8 @@
 ct;%0A
-import org.apache.isis.core.metamodel.spec.feature.MixedInMember;%0Aimport org.apache.isis.core.metamodel.util.Facets;%0A
 impo
@@ -1517,28 +1517,8 @@
 t;%0A%0A
-import lombok.val;%0A%0A
 publ
@@ -1838,24 +1838,168 @@
 onModel) %7B%0A%0A
+        //%5BISIS-3210%5D EntityModel.ofAdapter(commonContext, targetAdapterForMixin); not supported for mixins%0A        return Optional.empty();%0A%0A//
         val 
@@ -2028,24 +2028,26 @@
 etAction();%0A
+//
         if(a
@@ -2063,25 +2063,29 @@
 ixedIn()) %7B%0A
-%0A
+//%0A//
             
@@ -2136,24 +2136,26 @@
 ixinType();%0A
+//
             
@@ -2234,25 +2234,29 @@
 onOwner());%0A
-%0A
+//%0A//
             
@@ -2293,16 +2293,18 @@
  use it%0A
+//
         
@@ -2369,16 +2369,18 @@
 rMixin)%0A
+//
         
@@ -2398,16 +2398,18 @@
 Grid-%3E%7B%0A
+//
         
@@ -2467,24 +2467,26 @@
 lContext();%0A
+//
             
@@ -2515,16 +2515,18 @@
 Mixin =%0A
+//
         
@@ -2602,16 +2602,18 @@
 Mixin);%0A
+//
         
@@ -2677,16 +2677,18 @@
 sGrid);%0A
+//
         
@@ -2695,16 +2695,18 @@
     %7D);%0A
+//
         
@@ -2699,32 +2699,34 @@
 %7D);%0A//        %7D%0A
+//
         return O

revert recent change for mixin extra content