package com.cedricziel.idea.typo3.util;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException;
import com.intellij.openapi.vfs.VirtualFile;
import com.jetbrains.php.composer.ComposerConfigUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.IOException;

public class ComposerUtil {
    @Nullable
    public static String extensionKeyFromPackageName(JsonElement jsonElement) {
        JsonElement nameElement = jsonElement.getAsJsonObject().get("name");
        if (nameElement != null) {
            String name = nameElement.getAsString();
            if (name != null) {
                if (name.contains("/")) {
                    return name.split("/")[1].replaceAll("-", "_");
                }
            }
        }

        return null;
    }

    public static String extensionKeyFromInstallerName(JsonElement jsonElement) {

        JsonObject asJsonObject = jsonElement.getAsJsonObject();
        if (!asJsonObject.has("extra")) {
            return null;
        }

        JsonObject extra = asJsonObject.get("extra").getAsJsonObject();
        if (!extra.has("installerName")) {
            return null;
        }

        return extra.getAsJsonObject().getAsJsonPrimitive("installerName").getAsString();
    }

    public static String extensionKeyFromExtraExtensionKey(JsonElement jsonElement) {

        JsonObject asJsonObject = jsonElement.getAsJsonObject();
        if (!asJsonObject.has("extra")) {
            return null;
        }

        JsonObject extra = asJsonObject.get("extra").getAsJsonObject();
        if (!extra.has("typo3/cms")) {
            return null;
        }

        JsonObject typo3Extras = extra.getAsJsonObject("typo3/cms");
        if (!typo3Extras.has("extension-key")) {
            return null;
        }

        return typo3Extras.getAsJsonPrimitive("extension-key").getAsString();
    }

    public static String packageType(JsonElement jsonElement) {

        JsonObject asJsonObject = jsonElement.getAsJsonObject();
        if (!asJsonObject.has("type")) {
            return null;
        }

        return asJsonObject.get("type").getAsJsonPrimitive().getAsString();
    }

    public static boolean isTYPO3ExtensionManifest(JsonElement jsonElement) {

        String packageType = packageType(jsonElement);
        if (packageType == null) {
            return false;
        }

        return packageType.equals("typo3-cms-extension");
    }

    @Nullable
    public static String findExtensionKey(@NotNull VirtualFile composerJsonFile) {
        try {
            JsonElement jsonElement = ComposerConfigUtils.parseJson(composerJsonFile);

            if (!isTYPO3ExtensionManifest(jsonElement)) {
                return null;
            }

            // 1.1 find extra.typo3/cms.extension-key
            String extensionKeyFromExtrasTYPO3 = extensionKeyFromExtraExtensionKey(jsonElement);
            if (extensionKeyFromExtrasTYPO3 != null) {
                return extensionKeyFromExtrasTYPO3;
            }

            // 1.2 find extra.installerName
            String extensionKeyFromInstallerName = extensionKeyFromInstallerName(jsonElement);
            if (extensionKeyFromInstallerName != null) {
                return extensionKeyFromInstallerName;
            }

            // 1.3 last resort: package-name
            String extensionKeyFromPackageName = extensionKeyFromPackageName(jsonElement);
            if (extensionKeyFromPackageName != null) {
                return extensionKeyFromPackageName;
            }


        } catch (IOException e) {
            e.printStackTrace();
        } catch (JsonSyntaxException | IllegalStateException e) {
            // nothing, yo
        }

        return null;
    }
}

@@ -362,16 +362,64 @@
 ception;
+%0Aimport java.util.Map;%0Aimport java.util.Objects;
 %0A%0Apublic
@@ -3858,11 +3858,1174 @@
 l;%0A    %7D
+%0A%0A    @NotNull%0A    public static String%5B%5D findNamespaces(@NotNull VirtualFile composerJsonFile) %7B%0A        try %7B%0A            JsonElement jsonElement = ComposerConfigUtils.parseJson(composerJsonFile);%0A%0A            if (!isTYPO3ExtensionManifest(jsonElement)) %7B%0A                return new String%5B0%5D;%0A            %7D%0A%0A            return psr4Roots(jsonElement);%0A        %7D catch (IOException e) %7B%0A            e.printStackTrace();%0A        %7D%0A%0A        return new String%5B0%5D;%0A    %7D%0A%0A    @NotNull%0A    private static String%5B%5D psr4Roots(JsonElement composerJsonFile) %7B%0A        JsonObject asJsonObject = composerJsonFile.getAsJsonObject();%0A        if (!asJsonObject.has(%22autoload%22)) %7B%0A            return new String%5B0%5D;%0A        %7D%0A%0A        JsonObject autoloadHash = asJsonObject.get(%22autoload%22).getAsJsonObject();%0A        if (!autoloadHash.has(%22psr-4%22)) %7B%0A            return new String%5B0%5D;%0A        %7D%0A%0A        JsonObject psr4Hash = autoloadHash.get(%22psr-4%22).getAsJsonObject();%0A%0A        return psr4Hash%0A                .entrySet()%0A                .stream()%0A                .filter(Objects::nonNull)%0A                .map(Map.Entry::getKey)%0A                .toArray(String%5B%5D::new);%0A    %7D
 %0A%7D%0A

Implement resolving PSR-4 roots from composer json files