package jetbrains.mps.lang.editor.diagram.testLanguage.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.jetpad.mapper.Mapper;
import jetbrains.jetpad.projectional.diagram.view.DiagramNodeView;
import jetbrains.jetpad.mapper.Synchronizers;
import jetbrains.jetpad.mapper.MapperFactory;
import jetbrains.mps.lang.editor.figures.sandbox.MPSRectView;
import jetbrains.jetpad.projectional.view.View;
import jetbrains.jetpad.model.property.WritableProperty;
import jetbrains.mps.editor.runtime.selection.SelectionUtil;
import jetbrains.mps.internal.collections.runtime.Sequence;
import jetbrains.mps.nodeEditor.cells.jetpad.JetpadUtils;
import jetbrains.jetpad.geometry.Rectangle;
import jetbrains.mps.nodeEditor.cells.jetpad.DiagramCell;
import jetbrains.mps.lang.editor.diagram.runtime.jetpad.views.NodeDecoratorView;

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

  public RectangleNode_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_t2nwzx_a(getEditorContext(), myNode);
    editorCell.setCellId("DiagramNode_t2nwzx_a");
    editorCell.setBig(true);
    setCellContext(editorCell);
    DefaultDiagramElementActionMap_0.setCellActions(editorCell, myNode, getEditorContext());
    return editorCell;
  }
  private class BlockCellImpl_t2nwzx_a extends BlockCell {
    private BlockCellImpl_t2nwzx_a(EditorContext editorContext, final SNode node) {
      super(editorContext, node);
      synchronize();
    }
    public void synchronize() {
      super.synchronizeViewWithModel();
    }
    public Mapper<SNode, DiagramNodeView> createMapper() {
      return new Mapper<SNode, DiagramNodeView>(getSNode(), createDiagramNodeView()) {
        @Override
        protected void registerSynchronizers(Mapper.SynchronizersConfiguration configuration) {
          super.registerSynchronizers(configuration);
          final DiagramNodeView diagramNodeView = getTarget();
          configuration.add(Synchronizers.forConstantRole(this, getContentViewMapperSource(), getTarget().contentView.children(), new MapperFactory<String, MPSRectView>() {
            public Mapper<? extends String, ? extends MPSRectView> createMapper(String block) {
              return new Mapper<String, MPSRectView>(block, new MPSRectView()) {
                @Override
                protected void registerSynchronizers(Mapper.SynchronizersConfiguration configuration) {
                  super.registerSynchronizers(configuration);
                }
              };
            }
          }));
          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;
          }
          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));
          MPSRectView contentView = (MPSRectView) getContentView();
          configuration.add(Synchronizers.forProperty(contentView.bounds(), getTarget().bounds));

        }
      };
    }
  }
}
