package jetbrains.mps.testHybridEditor.diagram.generated.editor;

/*Generated by MPS */

import jetbrains.mps.editor.runtime.descriptor.AbstractEditorBuilder;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.mps.openapi.model.SNode;
import jetbrains.mps.openapi.editor.EditorContext;
import jetbrains.mps.openapi.editor.cells.EditorCell;
import jetbrains.mps.nodeEditor.cells.jetpad.BlockCell;
import jetbrains.mps.nodeEditor.cells.jetpad.PropertyMapperCell;
import jetbrains.mps.nodeEditor.cells.jetpad.ReadableModelProperty;
import jetbrains.jetpad.model.collections.list.ObservableList;
import jetbrains.jetpad.model.collections.list.ObservableArrayList;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SPropertyOperations;
import jetbrains.mps.util.Pair;
import org.jetbrains.mps.openapi.model.SNodeReference;
import jetbrains.mps.smodel.SNodePointer;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SLinkOperations;
import java.util.HashSet;
import jetbrains.jetpad.mapper.Mapper;
import jetbrains.jetpad.projectional.diagram.view.DiagramNodeView;
import jetbrains.jetpad.mapper.Synchronizers;
import jetbrains.jetpad.mapper.MapperFactory;
import jetbrains.jetpad.projectional.view.View;
import jetbrains.jetpad.projectional.view.RectView;
import jetbrains.jetpad.values.Color;
import jetbrains.jetpad.geometry.Vector;
import jetbrains.mps.nodeEditor.cells.jetpad.JetpadUtils;
import jetbrains.jetpad.model.property.WritableProperty;
import jetbrains.mps.editor.runtime.selection.SelectionUtil;
import jetbrains.mps.lang.editor.figures.sandbox.PolygonContentView;
import jetbrains.mps.lang.editor.diagram.runtime.jetpad.views.MovableContentView;
import jetbrains.jetpad.geometry.Rectangle;
import jetbrains.mps.internal.collections.runtime.Sequence;
import jetbrains.mps.nodeEditor.cells.jetpad.DiagramCell;
import jetbrains.mps.lang.editor.diagram.runtime.jetpad.views.NodeDecoratorView;
import jetbrains.jetpad.model.property.Properties;
import jetbrains.mps.lang.editor.diagram.runtime.jetpad.views.ResizableContentView;
import jetbrains.mps.lang.editor.diagram.runtime.jetpad.views.PortDecoratorView;
import java.util.Set;
import jetbrains.jetpad.model.property.ReadableProperty;
import org.jetbrains.mps.openapi.language.SProperty;
import jetbrains.mps.smodel.adapter.structure.MetaAdapterFactory;
import org.jetbrains.mps.openapi.language.SReferenceLink;
import org.jetbrains.mps.openapi.language.SContainmentLink;

/*package*/ class BlockInstance_diagram_EditorBuilder_a extends AbstractEditorBuilder {
  @NotNull
  private SNode myNode;

  public BlockInstance_diagram_EditorBuilder_a(@NotNull EditorContext context, @NotNull SNode node) {
    super(context);
    myNode = node;
  }

  @NotNull
  @Override
  public SNode getNode() {
    return myNode;
  }

  /*package*/ EditorCell createCell() {
    return createDiagramNode_0();
  }

  private EditorCell createDiagramNode_0() {
    BlockCell editorCell = new BlockCellImpl_79747v_a(getEditorContext(), myNode);
    editorCell.setCellId("DiagramNode_79747v_a");
    editorCell.setBig(true);
    setCellContext(editorCell);
    BlockActionMap.setCellActions(editorCell, myNode, getEditorContext());
    return editorCell;
  }
  private class BlockCellImpl_79747v_a extends BlockCell {
    private final PropertyMapperCell<String> myPropertyCell_79747v_a0a;
    private final ReadableModelProperty<String> myProperty_79747v_a1a;
    private final PropertyMapperCell<Integer> myPropertyCell_79747v_a2a;
    private final PropertyMapperCell<Integer> myPropertyCell_79747v_a3a;
    private final PropertyMapperCell<Integer> myPropertyCell_79747v_a4a;
    private final PropertyMapperCell<Integer> myPropertyCell_79747v_a5a;
    private final ObservableList<SNode> myInputPorts = new ObservableArrayList<SNode>();
    private final ObservableList<SNode> myOutputPorts = new ObservableArrayList<SNode>();
    private BlockCellImpl_79747v_a(EditorContext editorContext, final SNode node) {
      super(editorContext, node);
      myPropertyCell_79747v_a0a = new PropertyMapperCell<String>(editorContext, node) {
        protected String getModelPropertyValueImpl() {
          return SPropertyOperations.getString(node, PROPS.name$MnvL);
        }
        protected void setModelPropertyValueImpl(String value) {
          SPropertyOperations.assign(node, PROPS.name$MnvL, value);
        }
      };
      addEditorCell(myPropertyCell_79747v_a0a);
      myPropertyCell_79747v_a0a.getEditorComponent().getUpdater().getCurrentUpdateSession().registerCleanDependency(myPropertyCell_79747v_a0a, new Pair<SNodeReference, String>(new SNodePointer(node), "name"));
      myProperty_79747v_a1a = new ReadableModelProperty<String>(editorContext) {
        protected String getModelPropertyValue() {
          return SPropertyOperations.getString(SLinkOperations.getTarget(getSNode(), LINKS.metaBlock$G_NX), PROPS.name$MnvL);
        }
      }.init();
      addModelProperty(myProperty_79747v_a1a);
      myPropertyCell_79747v_a2a = new PropertyMapperCell<Integer>(editorContext, node) {
        protected Integer getModelPropertyValueImpl() {
          return SPropertyOperations.getInteger(node, PROPS.x$Gfxu);
        }
        protected void setModelPropertyValueImpl(Integer value) {
          SPropertyOperations.assign(node, PROPS.x$Gfxu, value);
        }
      };
      addEditorCell(myPropertyCell_79747v_a2a);
      myPropertyCell_79747v_a2a.getEditorComponent().getUpdater().getCurrentUpdateSession().registerCleanDependency(myPropertyCell_79747v_a2a, new Pair<SNodeReference, String>(new SNodePointer(node), "x"));
      myPropertyCell_79747v_a3a = new PropertyMapperCell<Integer>(editorContext, node) {
        protected Integer getModelPropertyValueImpl() {
          return SPropertyOperations.getInteger(node, PROPS.y$GfKv);
        }
        protected void setModelPropertyValueImpl(Integer value) {
          SPropertyOperations.assign(node, PROPS.y$GfKv, value);
        }
      };
      addEditorCell(myPropertyCell_79747v_a3a);
      myPropertyCell_79747v_a3a.getEditorComponent().getUpdater().getCurrentUpdateSession().registerCleanDependency(myPropertyCell_79747v_a3a, new Pair<SNodeReference, String>(new SNodePointer(node), "y"));
      myPropertyCell_79747v_a4a = new PropertyMapperCell<Integer>(editorContext, node) {
        protected Integer getModelPropertyValueImpl() {
          return SPropertyOperations.getInteger(node, PROPS.width$BHuw);
        }
        protected void setModelPropertyValueImpl(Integer value) {
          SPropertyOperations.assign(node, PROPS.width$BHuw, value);
        }
      };
      addEditorCell(myPropertyCell_79747v_a4a);
      myPropertyCell_79747v_a4a.getEditorComponent().getUpdater().getCurrentUpdateSession().registerCleanDependency(myPropertyCell_79747v_a4a, new Pair<SNodeReference, String>(new SNodePointer(node), "width"));
      myPropertyCell_79747v_a5a = new PropertyMapperCell<Integer>(editorContext, node) {
        protected Integer getModelPropertyValueImpl() {
          return SPropertyOperations.getInteger(node, PROPS.height$ChnT);
        }
        protected void setModelPropertyValueImpl(Integer value) {
          SPropertyOperations.assign(node, PROPS.height$ChnT, value);
        }
      };
      addEditorCell(myPropertyCell_79747v_a5a);
      myPropertyCell_79747v_a5a.getEditorComponent().getUpdater().getCurrentUpdateSession().registerCleanDependency(myPropertyCell_79747v_a5a, new Pair<SNodeReference, String>(new SNodePointer(node), "height"));
      synchronize();
    }
    public void synchronize() {
      super.synchronizeViewWithModel();
      myPropertyCell_79747v_a0a.synchronize();
      myPropertyCell_79747v_a2a.synchronize();
      myPropertyCell_79747v_a3a.synchronize();
      myPropertyCell_79747v_a4a.synchronize();
      myPropertyCell_79747v_a5a.synchronize();
      syncPortObjects(SLinkOperations.getChildren(SLinkOperations.getTarget(getSNode(), LINKS.metaBlock$G_NX), LINKS.inMetaPorts$BclS), myInputPorts.listIterator(), new HashSet<SNode>(myInputPorts));
      syncPortObjects(SLinkOperations.getChildren(SLinkOperations.getTarget(getSNode(), LINKS.metaBlock$G_NX), LINKS.outMetaPorts$BzNf), myOutputPorts.listIterator(), new HashSet<SNode>(myOutputPorts));
    }
    public Mapper<SNode, DiagramNodeView> createMapper() {
      return new Mapper<SNode, DiagramNodeView>(getSNode(), createDiagramNodeView()) {
        @Override
        protected void registerSynchronizers(Mapper.SynchronizersConfiguration configuration) {
          super.registerSynchronizers(configuration);
          configuration.add(Synchronizers.forObservableRole(this, myInputPorts, getTarget().inputs.children(), new MapperFactory<SNode, View>() {
            public Mapper<? extends SNode, ? extends View> createMapper(SNode id) {
              return new Mapper<SNode, RectView>(id, createPortView(id)) {
                @Override
                protected void registerSynchronizers(Mapper.SynchronizersConfiguration configuration) {
                  super.registerSynchronizers(configuration);
                  getTarget().background().set(Color.LIGHT_GRAY);
                  getTarget().dimension().set(new Vector(10, 10));
                  getTarget().prop(JetpadUtils.CONNECTABLE).set(Boolean.TRUE);
                  configuration.add(Synchronizers.forProperty(getTarget().focused(), new WritableProperty<Boolean>() {
                    public void set(Boolean isFocused) {
                      if (isFocused && !(isSelected())) {
                        SelectionUtil.selectCell(getContext(), getSNode(), getCellId());
                      }
                    }
                  }));

                }
              };
            }
            private RectView createPortView(SNode id) {
              RectView view = new RectView();
              view.prop(JetpadUtils.SOURCE).set(getSNode());
              view.prop(JetpadUtils.ID).set(id);
              view.focusable().set(true);
              return view;
            }
          }));
          configuration.add(Synchronizers.forObservableRole(this, myOutputPorts, getTarget().outputs.children(), new MapperFactory<SNode, View>() {
            public Mapper<? extends SNode, ? extends View> createMapper(SNode id) {
              return new Mapper<SNode, RectView>(id, createPortView(id)) {
                @Override
                protected void registerSynchronizers(Mapper.SynchronizersConfiguration configuration) {
                  super.registerSynchronizers(configuration);
                  getTarget().background().set(Color.GRAY);
                  getTarget().dimension().set(new Vector(10, 10));
                  getTarget().prop(JetpadUtils.CONNECTION_SOURCE).set(Boolean.TRUE);
                  getTarget().prop(JetpadUtils.CONNECTABLE).set(Boolean.TRUE);
                  configuration.add(Synchronizers.forProperty(getTarget().focused(), new WritableProperty<Boolean>() {
                    public void set(Boolean isFocused) {
                      if (isFocused && !(isSelected())) {
                        SelectionUtil.selectCell(getContext(), getSNode(), getCellId());
                      }
                    }
                  }));

                }
              };
            }
            private RectView createPortView(SNode id) {
              RectView view = new RectView();
              view.prop(JetpadUtils.SOURCE).set(getSNode());
              view.prop(JetpadUtils.ID).set(id);
              view.focusable().set(true);
              return view;
            }
          }));
          final DiagramNodeView diagramNodeView = getTarget();
          configuration.add(Synchronizers.forConstantRole(this, getContentViewMapperSource(), getTarget().contentView.children(), new MapperFactory<String, PolygonContentView>() {
            public Mapper<? extends String, ? extends PolygonContentView> createMapper(String block) {
              return new Mapper<String, PolygonContentView>(block, new PolygonContentView()) {
                @Override
                protected void registerSynchronizers(Mapper.SynchronizersConfiguration configuration) {
                  super.registerSynchronizers(configuration);
                  configuration.add(Synchronizers.forProperty(getTarget().prop(MovableContentView.POSITION_X), () -> updatePositionsFromModel(getTarget(), diagramNodeView)));
                  configuration.add(Synchronizers.forProperty(getTarget().prop(MovableContentView.POSITION_Y), () -> updatePositionsFromModel(getTarget(), diagramNodeView)));
                  configuration.add(Synchronizers.forProperty(getTarget().bounds(), (Rectangle bounds) -> {
                    getTarget().prop(MovableContentView.POSITION_X).set(bounds.origin.x);
                    getTarget().prop(MovableContentView.POSITION_Y).set(bounds.origin.y);
                  }));
                  myPropertyCell_79747v_a0a.registerSynchronizers(configuration, getTarget().text());
                  configuration.add(Synchronizers.forProperty(myProperty_79747v_a1a, getTarget().metaText()));
                  myPropertyCell_79747v_a2a.registerSynchronizers(configuration, getTarget().prop(MovableContentView.POSITION_X));
                  myPropertyCell_79747v_a3a.registerSynchronizers(configuration, getTarget().prop(MovableContentView.POSITION_Y));
                  myPropertyCell_79747v_a4a.registerSynchronizers(configuration, getTarget().contentWidth);
                  myPropertyCell_79747v_a5a.registerSynchronizers(configuration, getTarget().contentHeight);
                }
              };
            }
          }));
          final View targetView = this.getTarget();
          configuration.add(Synchronizers.forProperty(targetView.focused(), new WritableProperty<Boolean>() {
            public void set(Boolean isFocused) {
              if (isFocused && !(isSelected())) {
                SelectionUtil.selectCell(getContext(), getSNode(), getCellId());
              }
            }
          }));
          configuration.add(Synchronizers.forProperty(mySelectedItem, new WritableProperty<Boolean>() {
            public void set(Boolean isSelected) {
              if (isSelected) {
                for (View view : Sequence.fromIterable(JetpadUtils.getAllChildren(targetView))) {
                  if (view.focused().get()) {
                    return;
                  }
                }
                targetView.container().focusedView().set(targetView);
              } else if (!(isSelected) && targetView.focused().get()) {
                targetView.container().focusedView().set(null);
              }
            }
          }));
          configuration.add(Synchronizers.forProperty(targetView.bounds(), new WritableProperty<Rectangle>() {
            public void set(Rectangle rect) {
              DiagramCell diagramCell = getDiagramCell();
              if (diagramCell == null) {
                return;
              }
              setX(rect.origin.x + diagramCell.getX());
              setY(rect.origin.y + diagramCell.getY());
              setWidth(rect.dimension.x);
              setHeight(rect.dimension.y);
            }
          }));
        }
      };
    }
    public Mapper<SNode, NodeDecoratorView> createDecorationMapper() {
      return new Mapper<SNode, NodeDecoratorView>(getSNode(), new NodeDecoratorView()) {
        @Override
        protected void registerSynchronizers(Mapper.SynchronizersConfiguration configuration) {
          super.registerSynchronizers(configuration);
          DiagramCell diagramCell = getDiagramCell();
          if (diagramCell == null) {
            return;
          }
          final Mapper<SNode, DiagramNodeView> blockMapper = getBlockMapper();
          if (blockMapper == null) {
            return;
          }
          configuration.add(Synchronizers.forProperty(myErrorItem, getTarget().hasError));
          configuration.add(Synchronizers.forProperty(blockMapper.getTarget().focused(), getTarget().isSelected));
          final PolygonContentView contentView = (PolygonContentView) getContentView();
          configuration.add(Synchronizers.forProperty(contentView.bounds(), getTarget().bounds));
          configuration.add(Synchronizers.forProperty(Properties.constant(Boolean.TRUE), getTarget().resizable));
          configuration.add(Synchronizers.forProperty(getTarget().boundsDelta, (Rectangle delta) -> {
            if (delta == null) {
              return;
            }
            Vector positionDelta = delta.origin;
            Vector sizeDelta = delta.dimension;
            blockMapper.getTarget().move(positionDelta);
            contentView.prop(ResizableContentView.PREFERRED_SIZE).set(contentView.prop(ResizableContentView.PREFERRED_SIZE).get().add(sizeDelta));
          }));
          configuration.add(Synchronizers.forObservableRole(this, myInputPorts, getTarget().inputPortDecotatorView.children(), new MapperFactory<SNode, PortDecoratorView>() {
            public Mapper<? extends SNode, ? extends PortDecoratorView> createMapper(final SNode id) {
              return new Mapper<SNode, PortDecoratorView>(id, new PortDecoratorView()) {
                @Override
                protected void registerSynchronizers(Mapper.SynchronizersConfiguration configuration) {
                  DiagramCell diagramCell = getDiagramCell();
                  if (diagramCell == null) {
                    return;
                  }
                  Mapper<? super SNode, ?> blockDescendantMapper = getDiagramCell().getRootMapper().getDescendantMapper(getSNode());
                  if (blockDescendantMapper == null) {
                    return;
                  }
                  Set<Mapper<? super SNode, ?>> mappers = blockDescendantMapper.getMappingContext().getMappers(blockDescendantMapper, id);
                  if (mappers.isEmpty()) {
                    return;
                  }
                  final Mapper<SNode, View> descendantMapper = ((Mapper<SNode, View>) mappers.iterator().next());
                  configuration.add(Synchronizers.forProperty(((View) descendantMapper.getTarget()).focused(), getTarget().isSelected));
                  ReadableProperty<Rectangle> bounds = ((View) descendantMapper.getTarget()).bounds();
                  configuration.add(Synchronizers.forProperty(bounds, getTarget().bounds));

                }
              };
            }
          }));
          configuration.add(Synchronizers.forObservableRole(this, myOutputPorts, getTarget().outputPortDecotatorView.children(), new MapperFactory<SNode, PortDecoratorView>() {
            public Mapper<? extends SNode, ? extends PortDecoratorView> createMapper(final SNode id) {
              return new Mapper<SNode, PortDecoratorView>(id, new PortDecoratorView()) {
                @Override
                protected void registerSynchronizers(Mapper.SynchronizersConfiguration configuration) {
                  DiagramCell diagramCell = getDiagramCell();
                  if (diagramCell == null) {
                    return;
                  }
                  Mapper<? super SNode, ?> blockDescendantMapper = getDiagramCell().getRootMapper().getDescendantMapper(getSNode());
                  if (blockDescendantMapper == null) {
                    return;
                  }
                  Set<Mapper<? super SNode, ?>> mappers = blockDescendantMapper.getMappingContext().getMappers(blockDescendantMapper, id);
                  if (mappers.isEmpty()) {
                    return;
                  }
                  final Mapper<SNode, View> descendantMapper = ((Mapper<SNode, View>) mappers.iterator().next());
                  configuration.add(Synchronizers.forProperty(((View) descendantMapper.getTarget()).focused(), getTarget().isSelected));
                  ReadableProperty<Rectangle> bounds = ((View) descendantMapper.getTarget()).bounds();
                  configuration.add(Synchronizers.forProperty(bounds, getTarget().bounds));

                }
              };
            }
          }));

        }
      };
    }
  }

  private static final class PROPS {
    /*package*/ static final SProperty name$MnvL = MetaAdapterFactory.getProperty(0xceab519525ea4f22L, 0x9b92103b95ca8c0cL, 0x110396eaaa4L, 0x110396ec041L, "name");
    /*package*/ static final SProperty x$Gfxu = MetaAdapterFactory.getProperty(0x913a1d639e1948faL, 0xad03e33ecccd3814L, 0x20a804e2ec43f49dL, 0x20a804e2ec440489L, "x");
    /*package*/ static final SProperty y$GfKv = MetaAdapterFactory.getProperty(0x913a1d639e1948faL, 0xad03e33ecccd3814L, 0x20a804e2ec43f49dL, 0x20a804e2ec44048aL, "y");
    /*package*/ static final SProperty width$BHuw = MetaAdapterFactory.getProperty(0x913a1d639e1948faL, 0xad03e33ecccd3814L, 0x20a804e2ec43f49dL, 0x2c11e15d8177197eL, "width");
    /*package*/ static final SProperty height$ChnT = MetaAdapterFactory.getProperty(0x913a1d639e1948faL, 0xad03e33ecccd3814L, 0x20a804e2ec43f49dL, 0x2c11e15d817719aeL, "height");
  }

  private static final class LINKS {
    /*package*/ static final SReferenceLink metaBlock$G_NX = MetaAdapterFactory.getReferenceLink(0x913a1d639e1948faL, 0xad03e33ecccd3814L, 0x20a804e2ec43f49dL, 0x20a804e2ec4404a9L, "metaBlock");
    /*package*/ static final SContainmentLink inMetaPorts$BclS = MetaAdapterFactory.getContainmentLink(0x913a1d639e1948faL, 0xad03e33ecccd3814L, 0x20a804e2ec40c2c8L, 0x20a804e2ec410486L, "inMetaPorts");
    /*package*/ static final SContainmentLink outMetaPorts$BzNf = MetaAdapterFactory.getContainmentLink(0x913a1d639e1948faL, 0xad03e33ecccd3814L, 0x20a804e2ec40c2c8L, 0x2cd0b06754c8ec0cL, "outMetaPorts");
  }
}
