package jetbrains.mps.vcs.diff.changes;

/*Generated by MPS */

import jetbrains.mps.annotations.GeneratedClass;
import jetbrains.mps.logging.Logger;
import org.jetbrains.annotations.NotNull;
import jetbrains.mps.vcs.diff.ChangeSet;
import org.jetbrains.mps.openapi.model.SModel;
import jetbrains.mps.extapi.model.ModelWithAttributes;

@GeneratedClass(node = "r:9b4a89e1-ec38-42c4-b1bd-96ab47ffcb3f(jetbrains.mps.vcs.diff.changes)/4241451198545478085", model = "r:9b4a89e1-ec38-42c4-b1bd-96ab47ffcb3f(jetbrains.mps.vcs.diff.changes)")
public class ModelAttributeChange extends MetadataChange {
  private static final Logger LOG = Logger.getLogger(ModelAttributeChange.class);
  private final String myKey;
  private final String myNewValue;

  public ModelAttributeChange(@NotNull ChangeSet changeSet, @NotNull String key, String newValue) {
    super(changeSet);
    myKey = key;
    myNewValue = newValue;
  }

  @Override
  public void apply(@NotNull SModel model, @NotNull NodeCopier nodeCopier) {
    if (model instanceof ModelWithAttributes) {
      ((ModelWithAttributes) model).setAttribute(myKey, myNewValue);
    } else {
      if (LOG.isWarningLevel()) {
        LOG.warning("trying to apply model attribute change to s model that is not capable to keep attributes");
      }
    }
  }

  @NotNull
  @Override
  protected ModelChange createOppositeChange() {
    // inspired by SetPropertyChange
    ChangeSet ocs = getChangeSet().getOppositeChangeSet();
    SModel m = ocs.getOldModel();
    String value = null;
    if (m instanceof ModelWithAttributes) {
      value = ((ModelWithAttributes) m).getAttribute(myKey);
    }
    return new ModelAttributeChange(ocs, myKey, value);
  }

  @NotNull
  @Override
  public ChangeType getType() {
    return ChangeType.CHANGE;
  }

  @Override
  public String toString() {
    return String.format("Change Model Attribute [%s]", myKey);
  }
}
