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

package com.hypixel.hytale.server.core.universe.world.path;

import com.hypixel.hytale.codec.KeyedCodec;
import java.util.function.Supplier;
import com.hypixel.hytale.codec.codecs.map.MapCodec;
import java.nio.file.Path;
import com.hypixel.hytale.codec.Codec;
import com.hypixel.hytale.codec.util.RawJsonReader;
import com.hypixel.hytale.logger.HytaleLogger;
import java.io.IOException;
import java.nio.file.CopyOption;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import org.bson.BsonValue;
import com.hypixel.hytale.server.core.util.BsonUtil;
import java.util.concurrent.CompletableFuture;
import com.hypixel.hytale.server.core.universe.world.World;
import javax.annotation.Nullable;
import com.hypixel.hytale.event.IEventDispatcher;
import com.hypixel.hytale.server.core.HytaleServer;
import java.util.Objects;
import javax.annotation.Nonnull;
import java.util.Collections;
import java.util.concurrent.ConcurrentHashMap;
import java.util.Map;
import com.hypixel.hytale.codec.builder.BuilderCodec;

public class WorldPathConfig
{
    public static final BuilderCodec<WorldPathConfig> CODEC;
    protected Map<String, WorldPath> paths;
    
    public WorldPathConfig() {
        this.paths = new ConcurrentHashMap<String, WorldPath>();
    }
    
    public WorldPath getPath(final String name) {
        return this.paths.get(name);
    }
    
    @Nonnull
    public Map<String, WorldPath> getPaths() {
        return Collections.unmodifiableMap((Map<? extends String, ? extends WorldPath>)this.paths);
    }
    
    @Nullable
    public WorldPath putPath(@Nonnull final WorldPath worldPath) {
        Objects.requireNonNull(worldPath);
        final IEventDispatcher<WorldPathChangedEvent, WorldPathChangedEvent> dispatcher = HytaleServer.get().getEventBus().dispatchFor((Class<? super WorldPathChangedEvent>)WorldPathChangedEvent.class);
        if (dispatcher.hasListener()) {
            dispatcher.dispatch(new WorldPathChangedEvent(worldPath));
        }
        return this.paths.put(worldPath.getName(), worldPath);
    }
    
    public WorldPath removePath(final String path) {
        Objects.requireNonNull(path);
        return this.paths.remove(path);
    }
    
    @Nonnull
    public CompletableFuture<Void> save(final World world) {
        final BsonValue bsonValue = WorldPathConfig.CODEC.encode(this);
        return BsonUtil.writeDocument(world.getSavePath().resolve("paths.json"), bsonValue.asDocument());
    }
    
    @Nonnull
    public static CompletableFuture<WorldPathConfig> load(final World world) {
        final Path oldPath = world.getSavePath().resolve("paths.bson");
        final Path path = world.getSavePath().resolve("paths.json");
        if (Files.exists(oldPath, new LinkOption[0]) && !Files.exists(path, new LinkOption[0])) {
            try {
                Files.move(oldPath, path, new CopyOption[0]);
            }
            catch (final IOException ex) {}
        }
        return CompletableFuture.supplyAsync(() -> {
            final WorldPathConfig config = RawJsonReader.readSyncWithBak(path, WorldPathConfig.CODEC, HytaleLogger.getLogger());
            WorldPathConfig worldPathConfig = null;
            if (config != null) {
                worldPathConfig = config;
            }
            else {
                new(com.hypixel.hytale.server.core.universe.world.path.WorldPathConfig.class)();
                new WorldPathConfig();
            }
            return worldPathConfig;
        });
    }
    
    @Nonnull
    @Override
    public String toString() {
        return "WorldPathConfig{paths=" + String.valueOf(this.paths);
    }
    
    static {
        CODEC = BuilderCodec.builder(WorldPathConfig.class, WorldPathConfig::new).addField(new KeyedCodec<Map>("Paths", (Codec<Map>)new MapCodec((Codec<Object>)WorldPath.CODEC, (Supplier<Map>)ConcurrentHashMap::new, false)), (config, paths) -> config.paths = paths, config -> config.paths).build();
    }
}
