package pl.jakubchmura.snmp.mib.psi.impl;

import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiWhiteSpace;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import pl.jakubchmura.snmp.mib.psi.SmiNameAndNumber;
import pl.jakubchmura.snmp.mib.psi.SmiNameValueString;

public class SmiV1MibNodeMixin extends SmiMibNodeImpl {

    @NotNull
    private final SmiNameAndNumber smiNameAndNumber;

    SmiV1MibNodeMixin(@NotNull SmiNameAndNumber smiNameAndNumber) {
        super(smiNameAndNumber.getNode());
        this.smiNameAndNumber = smiNameAndNumber;
    }

    @Override
    protected Long[] getIndex() {
        PsiElement numberLiteral = smiNameAndNumber.getNumberLiteral();
        if (numberLiteral == null) {
            throw new IllegalStateException("No number literal in SMIv1 mib node " + this);
        }
        String text = numberLiteral.getText();
        return new Long[]{Long.parseLong(text)};
    }

    @Nullable
    @Override
    public SmiMibNodeMixin getParentMibNode() {
        PsiElement prevSibling = getPreviousSibling(this);
        if (prevSibling instanceof SmiNameAndNumber) {
            return new SmiV1MibNodeMixin((SmiNameAndNumber) prevSibling);
        } else if (prevSibling instanceof SmiNameValueString) {
            SmiNameValueString smiNameValueString = (SmiNameValueString) prevSibling;
            String name = smiNameValueString.getName();
            if (IsoMibNode.ISO.equals(name)) {
                return new IsoMibNode(smiNameValueString.getNode());
            }
        }
        return null;
    }

    private static PsiElement getPreviousSibling(PsiElement psiElement) {
        PsiElement prevSibling = psiElement.getPrevSibling();
        if (prevSibling instanceof PsiWhiteSpace) {
            return getPreviousSibling(prevSibling);
        } else {
            return prevSibling;
        }
    }
}

@@ -303,16 +303,75 @@
 eString;
+%0Aimport pl.jakubchmura.snmp.mib.reference.MibNodeReference;
 %0A%0Apublic
@@ -1448,18 +1448,40 @@
     
-String nam
+MibNodeReference mibNodeReferenc
 e = 
@@ -1502,19 +1502,24 @@
 ring.get
-Nam
+Referenc
 e();%0A   
@@ -1535,35 +1535,32 @@
 if (
-IsoM
+m
 ibNode
-.ISO.equals(name)
+Reference != null
 ) %7B%0A
@@ -1579,60 +1579,186 @@
     
-return new IsoMibNode(smiNameValueString.getNode());
+PsiElement resolve = mibNodeReference.resolve();%0A                if (resolve instanceof SmiMibNodeMixin) %7B%0A                    return (SmiMibNodeMixin) resolve;%0A                %7D
 %0A   

Support other SMIv1 variations than ending with iso