// 
// Decompiled by Procyon v0.6.0
// 

package com.hypixel.hytale.builtin.asseteditor;

import java.util.List;
import com.hypixel.hytale.protocol.packets.asseteditor.AssetEditorAssetType;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import com.hypixel.hytale.protocol.Packet;
import com.hypixel.hytale.protocol.packets.asseteditor.AssetEditorPopupNotificationType;
import com.hypixel.hytale.server.core.Message;
import java.util.logging.Level;
import javax.annotation.Nullable;
import java.util.Iterator;
import com.hypixel.hytale.common.util.PathUtil;
import java.nio.file.Path;
import javax.annotation.Nonnull;
import java.util.Map;
import com.hypixel.hytale.protocol.packets.asseteditor.AssetEditorSetupAssetTypes;
import com.hypixel.hytale.builtin.asseteditor.assettypehandler.AssetTypeHandler;
import java.util.concurrent.ConcurrentHashMap;
import com.hypixel.hytale.logger.HytaleLogger;

public class AssetTypeRegistry
{
    private static final HytaleLogger LOGGER;
    private final ConcurrentHashMap<String, AssetTypeHandler> assetTypeHandlers;
    private AssetEditorSetupAssetTypes setupPacket;
    
    public AssetTypeRegistry() {
        this.assetTypeHandlers = new ConcurrentHashMap<String, AssetTypeHandler>();
    }
    
    @Nonnull
    public Map<String, AssetTypeHandler> getRegisteredAssetTypeHandlers() {
        return this.assetTypeHandlers;
    }
    
    public void registerAssetType(@Nonnull final AssetTypeHandler assetType) {
        if (this.assetTypeHandlers.putIfAbsent(assetType.getConfig().id, assetType) != null) {
            throw new IllegalArgumentException("An asset type with id '" + assetType.getConfig().id + "' is already registered");
        }
    }
    
    public void unregisterAssetType(@Nonnull final AssetTypeHandler assetType) {
        this.assetTypeHandlers.remove(assetType.getConfig().id);
    }
    
    public AssetTypeHandler getAssetTypeHandler(final String id) {
        return this.assetTypeHandlers.get(id);
    }
    
    @Nullable
    public AssetTypeHandler getAssetTypeHandlerForPath(@Nonnull final Path path) {
        final String extension = PathUtil.getFileExtension(path);
        if (extension.isEmpty()) {
            return null;
        }
        for (final AssetTypeHandler handler : this.assetTypeHandlers.values()) {
            if (handler.getConfig().fileExtension.equalsIgnoreCase(extension) && path.startsWith(handler.getConfig().path)) {
                return handler;
            }
        }
        return null;
    }
    
    public boolean isPathInAssetTypeFolder(@Nonnull final Path path) {
        for (final AssetTypeHandler assetTypeHandler : this.assetTypeHandlers.values()) {
            if (path.startsWith(assetTypeHandler.getRootPath()) && !path.equals(assetTypeHandler.getRootPath())) {
                return true;
            }
        }
        return false;
    }
    
    @Nullable
    public AssetTypeHandler tryGetAssetTypeHandler(@Nonnull final Path assetPath, @Nonnull final EditorClient editorClient, final int requestToken) {
        final AssetTypeHandler assetTypeHandler = this.getAssetTypeHandlerForPath(assetPath);
        if (assetTypeHandler == null) {
            AssetTypeRegistry.LOGGER.at(Level.WARNING).log("Invalid asset type for %s", assetPath);
            if (requestToken != -1) {
                editorClient.sendFailureReply(requestToken, Message.translation("server.assetEditor.messages.invalidAssetType"));
            }
            else {
                editorClient.sendPopupNotification(AssetEditorPopupNotificationType.Error, Message.translation("server.assetEditor.messages.invalidAssetType"));
            }
            return null;
        }
        return assetTypeHandler;
    }
    
    public void sendPacket(@Nonnull final EditorClient editorClient) {
        editorClient.getPacketHandler().write(this.setupPacket);
    }
    
    public void setupPacket() {
        final List<AssetEditorAssetType> types = new ObjectArrayList<AssetEditorAssetType>();
        for (final AssetTypeHandler assetTypeHandler : this.assetTypeHandlers.values()) {
            types.add(assetTypeHandler.getConfig());
        }
        this.setupPacket = new AssetEditorSetupAssetTypes(types.toArray(AssetEditorAssetType[]::new));
    }
    
    static {
        LOGGER = HytaleLogger.forEnclosingClass();
    }
}
