/*++

NASM Assembly Language Plugin
Copyright (c) 2017-2018 Aidan Khoury. All rights reserved.

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

--*/

package com.nasmlanguage.psi.impl;

import com.intellij.lang.ASTNode;
import com.intellij.navigation.ItemPresentation;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiReference;
import com.intellij.psi.impl.source.resolve.reference.ReferenceProvidersRegistry;
import com.nasmlanguage.NASMIcons;
import com.nasmlanguage.NASMReference;
import com.nasmlanguage.psi.*;
import org.jetbrains.annotations.NotNull;

import javax.annotation.Nullable;
import javax.swing.*;

public class NASMPsiImplUtil {

    public static String getIncludeString(NASMInclude element) {
        ASTNode includeString = element.getNode().findChildByType(NASMTypes.STRING);
        if (includeString != null) {
            return includeString.getText()
                    .replace("\"", "")
                    .replace("'", "");
        }
        return null;
    }

    public static String getDefineIdentifierString(NASMDefine element) {
        ASTNode defineIdentifier = element.getNode().findChildByType(NASMTypes.IDENTIFIER);
        if (defineIdentifier != null)
            return defineIdentifier.getText();
        return null;
    }

    public static NASMIdentifier getDefineIdentifier(NASMDefine element) {
        ASTNode defineIdentifier = element.getNode().findChildByType(NASMTypes.IDENTIFIER);
        if (defineIdentifier != null)
            return (NASMIdentifier)defineIdentifier.getPsi();
        return null;
    }

    public static String getMacroIdentifier(NASMMacro element) {
        ASTNode macroIdentifier = element.getNode().findChildByType(NASMTypes.IDENTIFIER);
        if (macroIdentifier != null)
            return macroIdentifier.getText();
        return null;
    }

    @SuppressWarnings("ConstantConditions")
    public static String getLabelIdentifierString(NASMLabel element) {
        PsiElement labelDef = element.getLblDef();
        if (labelDef != null) {
            String labelDefString = labelDef.getText();
            return labelDefString.substring(0, labelDefString.indexOf(':')).trim();
        }
        return null;
    }

    @SuppressWarnings("ConstantConditions")
    public static String getConstantIdentifierString(NASMConstant element) {
        PsiElement identifier = element.getIdentifier().getId();
        if (identifier != null)
            return identifier.getText();
        return null;
    }

    public static String getName(NASMIdentifier element) {
        return element.getId().getText();
    }

    public static PsiElement setName(NASMIdentifier element, String newName) {
        ASTNode keyNode = element.getId().getNode();
        if (keyNode != null) {
            NASMIdentifier property = NASMElementFactory.createIdentifier(element.getProject(), newName);
            ASTNode newKeyNode = property.getFirstChild().getNode();
            element.getNode().replaceChild(keyNode, newKeyNode);
        }
        return element;
    }

    public static PsiElement getNameIdentifier(NASMIdentifier element) {
        return element.getId();
    }

    @NotNull
    public static PsiReference[] getReferences(@NotNull NASMIdentifier element) {
        return ReferenceProvidersRegistry.getReferencesFromProviders(element);
    }

    //@NotNull
    //public static PsiReference getReference(@NotNull NASMIdentifier element) {
    //    return new NASMReference( element.getId(), element.getId().getTextRange());
    //}

    public static ItemPresentation getPresentation(final NASMIdentifier element) {
        return new ItemPresentation() {
            @Nullable
            @Override
            public String getPresentableText() {
                return element.getName();
            }

            @Nullable
            @Override
            public String getLocationString() {
                return element.getContainingFile().getName();
            }

            @Nullable
            @Override
            public Icon getIcon(boolean unused) {
                return NASMIcons.ASM_FILE;
            }
        };
    }

    //public static String getLabelIdentifierString(NASMLabelInstruction element) {
    //    PsiElement labelIns = element.getLblIns();
    //    if (labelIns != null) {
    //        String labelInsString = labelIns.getText();
    //        return labelInsString.substring(0, labelInsString.indexOf(':')).trim();
    //    }
    //    return null;
    //}
    //public static String getLabelIdentifierString(NASMLabelData element) {
    //    PsiElement lblData = element.getLblData();
    //    if (lblData != null) {
    //        String lblDataString = lblData.getText();
    //        return lblDataString.substring(0, lblDataString.indexOf(':')).trim();
    //    }
    //    return null;
    //}


}

@@ -69,340 +69,605 @@
 oury
-. All rights reserved.%0A%0AThis program is free software: you can redistribute it and/or modify%0Ait under the terms of the GNU General Public License as published by%0Athe Free Software Foundation, either v
+%0A%0APermission is hereby granted, free of charge, to any person obtaining a copy%0Aof this software and associated documentation files (the %22Software%22), to deal%0Ain the Software without restriction, including without limitation the rights%0Ato use, copy, modify, merge, publish, distribute, sublicense, and/or sell%0Acopies of the Software, and to permit p
 ers
-i
 on
- 3 of the License, or%0A(at your option) any later version.
+s to whom the Software is%0Afurnished to do so, subject to the following conditions:
 %0A%0ATh
-is program is distributed in the hope that it will be useful,%0Abut
+e above copyright notice and this permission notice shall be included in all%0Acopies or substantial portions of the Software.%0A%0ATHE SOFTWARE IS PROVIDED %22AS IS%22,
  WIT
@@ -675,59 +675,97 @@
 OUT 
+WARR
 AN
+T
 Y 
-WARRANTY; without even the implied warranty of%0A
+OF ANY KIND, EXPRESS OR%0AIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
 MERC
@@ -775,20 +775,18 @@
 TABILITY
- or 
+,%0A
 FITNESS 
@@ -813,199 +813,287 @@
 POSE
-.  See the%0AGNU General Public License for more details.%0A%0AYou should have received a copy of the GNU General Public License%0Aalong with this program.  If not, see %3Chttp://www.gnu.org/licenses/%3E
+ AND NONINFRINGEMENT. IN NO EVENT SHALL THE%0AAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER%0ALIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,%0AOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE%0ASOFTWARE
 .%0A%0A-

Update copyright notice with MIT license