package messages.customization.constraints;

/*Generated by MPS */

import jetbrains.mps.core.aspects.constraints.rules.BaseRulesConstraintsDescriptor;
import org.jetbrains.mps.openapi.language.SAbstractConcept;
import jetbrains.mps.smodel.adapter.structure.MetaAdapterFactory;
import jetbrains.mps.core.aspects.constraints.rules.Rule;
import jetbrains.mps.core.aspects.constraints.rules.kinds.ContainmentContext;
import java.util.List;
import java.util.Collections;
import java.util.Arrays;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.mps.openapi.model.SNode;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SNodeOperations;
import jetbrains.mps.core.aspects.constraints.rules.BaseRule;
import org.jetbrains.mps.openapi.model.SNodeReference;
import org.jetbrains.mps.openapi.persistence.PersistenceFacade;
import jetbrains.mps.core.aspects.constraints.rules.RuleId;
import jetbrains.mps.core.aspects.constraints.rules.kinds.PredefinedRuleKinds;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SPropertyOperations;
import org.jetbrains.mps.openapi.language.SInterfaceConcept;
import org.jetbrains.mps.openapi.language.SProperty;

public final class TestConcept0_ConstraintRules extends BaseRulesConstraintsDescriptor {
  private static final SAbstractConcept CONCEPT = MetaAdapterFactory.getInterfaceConcept(0x7cf7c95bc81e4da9L, 0xa05645e480a7abd3L, 0x2807c18666f81300L, "messages.customization.structure.TestConcept0");

  public static final Rule<ContainmentContext> check_id2884486869351404323 = new Rule_parentHasTheSameName();

  private static final List<Rule<?>> RULES = Collections.unmodifiableList(Arrays.<Rule<?>>asList(check_id2884486869351404323));

  @NotNull
  @Override
  public List<Rule<?>> getDeclaredRules() {
    return RULES;
  }

  public static final class Def_Parent2884486869351404345 {
    /**
     * null is an allowed value
     * though null is also returned if the def is not defined
     */
    @Nullable
    public static SNode getValue(@NotNull ContainmentContext context) {
      if (!(isDefined(context))) {
        return null;
      }
      return (SNode) SNodeOperations.cast(context.getParentNode(), CONCEPTS.TestConcept0$b0);
    }

    public static boolean isDefined(@NotNull ContainmentContext context) {
      return SNodeOperations.isInstanceOf(context.getParentNode(), CONCEPTS.TestConcept0$b0);
    }
  }

  public static final class Rule_parentHasTheSameName extends BaseRule<ContainmentContext> {
    private static final SNodeReference SOURCE_NODE_REF = PersistenceFacade.getInstance().createNodeReference("r:5dbac061-aef9-4696-88ee-0f21fe5598f3(messages.customization.constraints)/2884486869351404323");
    public static final RuleId ID_parentHasTheSameName = new RuleId(2884486869351404323L, SOURCE_NODE_REF);

    public Rule_parentHasTheSameName() {
      super(CONCEPT, PredefinedRuleKinds.CAN_BE_CHILD, ID_parentHasTheSameName, SOURCE_NODE_REF);
    }

    @Override
    public boolean check(@NotNull ContainmentContext context) {
      return SPropertyOperations.getString(context.getChildNode(), PROPS.name$MnvL).startsWith(SPropertyOperations.getString(Def_Parent2884486869351404345.getValue(context), PROPS.name$MnvL));
    }

    @Override
    public boolean appliesTo(@NotNull ContainmentContext context) {
      if (!(Def_Parent2884486869351404345.isDefined(context))) {
        return false;
      }
      return isNotEmptyString(SPropertyOperations.getString(context.getChildNode(), PROPS.name$MnvL)) && isNotEmptyString(SPropertyOperations.getString(Def_Parent2884486869351404345.getValue(context), PROPS.name$MnvL));
    }

    private static boolean isNotEmptyString(String str) {
      return str != null && str.length() > 0;
    }
  }

  /*package*/ TestConcept0_ConstraintRules() {
    super(CONCEPT);
  }

  private static final class CONCEPTS {
    /*package*/ static final SInterfaceConcept TestConcept0$b0 = MetaAdapterFactory.getInterfaceConcept(0x7cf7c95bc81e4da9L, 0xa05645e480a7abd3L, 0x2807c18666f81300L, "messages.customization.structure.TestConcept0");
  }

  private static final class PROPS {
    /*package*/ static final SProperty name$MnvL = MetaAdapterFactory.getProperty(0xceab519525ea4f22L, 0x9b92103b95ca8c0cL, 0x110396eaaa4L, 0x110396ec041L, "name");
  }
}
