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

package com.hypixel.hytale.server.worldgen.loader.zone;

import javax.annotation.Nullable;
import java.util.Set;
import javax.annotation.Nonnull;
import com.google.gson.JsonArray;
import java.util.Iterator;
import com.google.gson.JsonObject;
import java.util.Objects;
import com.hypixel.hytale.server.worldgen.loader.util.ColorUtil;
import com.google.gson.JsonElement;
import java.nio.file.Path;
import com.hypixel.hytale.procedurallib.json.SeedString;
import com.hypixel.hytale.server.worldgen.zone.Zone;
import java.util.Map;
import com.hypixel.hytale.server.worldgen.zone.ZoneColorMapping;
import com.hypixel.hytale.server.worldgen.SeedStringResource;
import com.hypixel.hytale.procedurallib.json.JsonLoader;

public class ZoneColorMappingJsonLoader extends JsonLoader<SeedStringResource, ZoneColorMapping>
{
    protected final Map<String, Zone> zoneLookup;
    
    public ZoneColorMappingJsonLoader(final SeedString<SeedStringResource> seed, final Path dataFolder, final JsonElement json, final Map<String, Zone> zoneLookup) {
        super(seed, dataFolder, json);
        this.zoneLookup = zoneLookup;
    }
    
    @Nonnull
    @Override
    public ZoneColorMapping load() {
        final ZoneColorMapping colorMapping = new ZoneColorMapping();
        final JsonObject mappingObj = this.json.getAsJsonObject();
        for (final Map.Entry<String, JsonElement> entry : mappingObj.entrySet()) {
            final int rgb = ColorUtil.hexString(entry.getKey());
            if (entry.getValue().isJsonArray()) {
                final JsonArray arr = entry.getValue().getAsJsonArray();
                final Zone[] zoneArr = new Zone[arr.size()];
                for (int i = 0; i < zoneArr.length; ++i) {
                    final String zoneName = arr.get(i).getAsString();
                    final Zone zone = this.zoneLookup.get(zoneName);
                    if (zone == null) {
                        throw new IllegalArgumentException(String.format("Zone with name %s was not found for color %s!", zoneName, entry.getKey()));
                    }
                    Objects.requireNonNull(zone);
                    zoneArr[i] = zone;
                }
                colorMapping.add(rgb, zoneArr);
            }
            else {
                final String zoneName2 = entry.getValue().getAsString();
                final Zone zone2 = this.zoneLookup.get(zoneName2);
                if (zone2 == null) {
                    throw new IllegalArgumentException(String.format("Zone with name %s was not found for color %s!", zoneName2, entry.getKey()));
                }
                colorMapping.add(rgb, zone2);
            }
        }
        return colorMapping;
    }
    
    public static void collectZones(final Set<String> zoneSet, @Nullable final JsonElement json) {
        if (json == null || !json.isJsonObject()) {
            return;
        }
        final JsonObject mappingObj = json.getAsJsonObject();
        for (final Map.Entry<String, JsonElement> entry : mappingObj.entrySet()) {
            if (entry.getValue().isJsonArray()) {
                for (final JsonElement zoneName : entry.getValue().getAsJsonArray()) {
                    zoneSet.add(zoneName.getAsString());
                }
            }
            else {
                zoneSet.add(entry.getValue().getAsString());
            }
        }
    }
}
